简体   繁体   中英

Spark Streaming: Print JavaInputDStream

currently I am working with Spark Streaming and the possibility to read messages from Kafka. With a Kafka Producer I send messages to a topic and would like to read this topic with the help of Spark Streaming.

I use the following Java code to query the messages:

package apache_spark_streaming;

import java.util.*;
import org.apache.spark.SparkConf;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.kafka010.*;
import org.apache.kafka.clients.consumer.ConsumerRecord;

public final class Spark_Kafka_Example {

private static final String BOOTSTRAP_SERVERS_CONNECTION = "XXXXX";
private static final String SPARK_CONNECTION = "spark://XXXXX:7077";
private static final String TOPIC_NAME = "KafkaTesting1";
private static final Set<String> TOPIC_1 = new HashSet<>(Arrays.asList(TOPIC_NAME.split(",")));

public static Map<String, Object> getProperties() {

    try {
        Map<String, Object> kafkaParams = new HashMap<>();
        kafkaParams.put("bootstrap.servers", BOOTSTRAP_SERVERS_CONNECTION);
        kafkaParams.put("key.deserializer", org.apache.kafka.common.serialization.StringDeserializer.class.getName());
        kafkaParams.put("value.deserializer", org.apache.kafka.common.serialization.StringDeserializer.class.getName());
        kafkaParams.put("group.id", "Stream Testing");
        kafkaParams.put("auto.offset.reset", "earliest");
        kafkaParams.put("enable.auto.commit", false);
        return kafkaParams;
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
public static void main(String[] args) throws Exception {

    // Create context with a 2 seconds batch interval
    SparkConf sparkConf = new SparkConf().setAppName("Kafka Example").setMaster(SPARK_CONNECTION);   
    JavaStreamingContext sc = new JavaStreamingContext(sparkConf, Durations.seconds(2));

    JavaInputDStream<ConsumerRecord<String, String>> stream = KafkaUtils.createDirectStream(
           sc
           , LocationStrategies.PreferConsistent()
           , ConsumerStrategies.Subscribe(TOPIC_1, getProperties())
    );    

    stream.print();
    sc.start(); 
    sc.awaitTermination();
  }
}

My Problem is that I don't know how to output the messages on the command line. Maybe I have only a understanding problem how to use JavaInputDStreams correctly.

Currently I get only this as Output with the print() function:

17/07/10 16:59:20 INFO JobScheduler: Added jobs for time 1499698760000 ms

I hope you can help me with this "issue".

Updated I tried as

stream.foreachRDD(consumerRecordJavaRDD -> {
   consumerRecordJavaRDD.foreach(stringStringConsumerRecord -> {
   //.to get topic name: stringStringConsumerRecord.topic()
   //To get value : stringStringConsumerRecord.value()
  } }

After you create the stream you will then extract the content and use filters or maps to process it.

Take a look at the streaming examples directory for Spark/java: https://github.com/apache/spark/tree/master/examples/src/main/java/org/apache/spark/examples/streaming

JavaQueueStream gives an example.

You directly tying to print input stream which is I not possible to print actual value of stream.

Use below code to print your input stream, which is produced from your Kafka producer.

  JavaDStream<String> data = stream.map(v -> {
        return v.value();    // mapping to convert into spark D-Stream 
  });
  
  data.print();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM