You need to enable JavaScript to run this app.
Torch Log Service

Torch Log Service

Copy page
Download PDF
Consumption and delivery
Consume logs using Spark Streaming
Copy page
Download PDF
Consume logs using Spark Streaming
TLS provides Kafka protocol consumption functionality. You can use the spark-streaming-kafka component to connect to TLS and consume data from TLS to downstream big data components or data warehouses.​
Scenario overview​
Spark Streaming is a real-time computing framework built on Spark, providing scalable, high-throughput, and fault-tolerant stream processing capabilities based on Spark. Spark Streaming can integrate with multiple data sources. For example, it can integrate with Kafka using the spark-streaming-kafka component to enable consumption of Kafka messages. TLS supports enabling Kafka protocol consumption functionality for specified log topics. Once enabled, Spark Streaming can consume log topics as Kafka Topics, such as consuming to downstream big data components or data warehouses. This is suitable for stream processing or big data storage scenarios.​
Prerequisites​
  • A log project and log topic are created. For detailed instructions, see .​
  • The Kafka protocol consumption functionality has been enabled for the specified log topic, and you have obtained the Kafka protocol consumption topic ID. For more details, see .​
  • We recommend using IAM users for access authorization. Before using an IAM user, ensure that the Volcanoengine primary account has created an IAM user and granted consumption-related permissions. For more details, see .​
  • Access Key for the current login account has been obtained. For more information, see Create Access Key.​
Configuration steps​
1. Add Maven dependencies​
When consuming log data from TLS using Spark Streaming, you need to use spark-streaming-kafka-0-10 provided by Spark Streaming.​
Add the relevant spark-streaming-kafka Maven dependencies to your project. Dependency details are as follows:​
Tip​
The TLS Kafka consumption functionality only supports Kafka protocol version 0.11.0 and above. You need to manually specify the Kafka client version for Spark as 0.11.0 or above.​
​
<dependency>​
<groupId>org.apache.spark</groupId>​
<artifactId>spark-streaming-kafka-0-10_2.11</artifactId>​
<version>${spark.version}</version>​
<exclusions>​
<exclusion>​
<groupId>org.apache.kafka</groupId>​
<artifactId>kafka-clients</artifactId>​
</exclusion>​
</exclusions>​
</dependency>​
<dependency>​
<groupId>org.apache.kafka</groupId>​
<artifactId>kafka-clients</artifactId>​
<version>1.0.0</version>​
</dependency>​
​
2. Configure Spark input stream​
Refer to the following sample code to complete the configuration of the Spark input stream. For detailed configuration instructions, see Spark official documentation. For parameter descriptions, see the table below.​
The following example demonstrates how to build a Spark input stream to consume log data from the log topic 0fdaa6b6-3c9f-424c-8664-fc0d222c****.​
​
// Build Spark Streaming context​
SparkConf conf = new SparkConf().setAppName("TlsDemo").setMaster("local").set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");​
conf.registerKryoClasses((Class<?>[]) Arrays.asList(ConsumerRecord.class).toArray());​
​
// Every 5 seconds, the SparkStreaming job collects data received from the data source within the past 5 seconds​
JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(5));​
​
// Build the Kafka parameter map​
// The main requirement is to specify the address of the Kafka cluster to connect to (the broker cluster address list)​
Map<String, Object> kafkaParams = new HashMap<>();​
// Initial cluster connection address. The format is service address:port number, for example tls-cn-beijing.ivolces.com:9093, where:​
// The service address is the TLS service address for the current region. Select the correct service endpoint based on region and network type. For details, see Service address.​
// The port number is fixed at 9093.​
kafkaParams.put("bootstrap.servers", tlsEndConsumePoint);​
// Specify the data type and encoding format for the Kafka output key (default is string type with UTF-8 encoding)​
kafkaParams.put("key.deserializer", StringDeserializer.class);​
// Specify the data type and encoding format for the Kafka output value (default is string type with UTF-8 encoding)​
kafkaParams.put("value.deserializer", StringDeserializer.class);​
// Consumer group ID, can be specified arbitrarily​
kafkaParams.put("group.id", consumeGroupID);​
kafkaParams.put("auto.offset.reset", "latest");​
kafkaParams.put("enable.auto.commit", false);​
// Security mode, TLS only supports SASL_SSL​
kafkaParams.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_SSL");​
// Authentication mechanism, TLS only supports PLAIN​
kafkaParams.put(SaslConfigs.SASL_MECHANISM, "PLAIN");​
// Account and password settings:​
// Username: projectId: the ProjectID of the Topic's project. net: Enter public for public network access, private for private network access.​
String userName = "${projectId}#${net}";​
// Password: should be configured as the Volcano Engine account key​
// Format is ${access-key-id}#${access-key-secret}, where:​
// ${access-key-id} should be replaced with your AccessKey ID.​
// ${access-key-secret} should be replaced with your AccessKey Secret.​
String passWord = "${access-key-id}#${access-key-secret}";​
// Account and password settings​
kafkaParams.put(SaslConfigs.SASL_JAAS_CONFIG,"org.apache.kafka.common.security.plain.PlainLoginModule " +"required username=\"" + userName + "\" password=\"" + passWord + "\";");​
​
​
// Build the topic set​
Collection<String> topics = new HashSet<>();​
// The topic to consume, format is out-xxxxxxxxx​
topics.add("out-0fdaa6b6-3c9f-424c-8664-fc0d222c****");​
​
try {​
// Retrieve data from Kafka​
final JavaInputDStream<ConsumerRecord<String, String>> stream = KafkaUtils.createDirectStream(jssc, LocationStrategies.PreferConsistent(),​
ConsumerStrategies.Subscribe(topics, kafkaParams));​
​
// todo: Consume to downstream big data components​
stream.print();​
jssc.start();​
jssc.awaitTermination();​
jssc.close();​
​
} catch (Exception e) {​
e.printStackTrace();​
}​
​
The required settings are as follows:​
​
Configuration item​
Description​
bootstrap.servers​
Initial cluster connection address. The format is service address:port number, for example tls-cn-beijing.ivolces.com:9093.​
  • The service address is the entry point for TLS in the current region. Please select the correct service address based on region and network type. For details, refer to Endpoints.​
  • The port number is fixed at 9093.​
consumeTopic​
The Kafka Topic to subscribe to. This should be configured as the TLS Kafka protocol consumption topic ID, in the format out-topic ID, for example out-0fdaa6b6-3c9f-424c-8664-fc0d222c****. You can view and copy the Kafka protocol consumption topic ID on the Topic details page in the TLS console.​
SECURITY_PROTOCOL_CONFIG​
Security mode, should be specified as SASL_SSL.​
SASL_MECHANISM​
Authentication mechanism, specify as PLAIN.​
SASL_JAAS_CONFIG​
Authentication information for the current logged-in user. Note:​
  • useranme:​
  • Kafka SASL username, format is ${project-id}#${net}.​
  • ${project-id}: Replace with the project ID of the TLS topic to be accessed.​
  • ${net}: Replace with the specific network type.​
  • public: Public network type, such as *. volces.com.​
  • private: Network type for private network environments, such as *. ivolces.com.​
  • password:​
  • Kafka SASL user password, formatted as ${access-key-id}#${access-key-secret}.​
  • ${access-key-id}: Replace with the AccessKey ID of the Volcano Engine account.​
  • ${access-key-secret}: Replace with the AccessKey Secret of the Volcano Engine account.​
​
​
Last updated: 2026.10.29 16:57:56