嗨,我是Spark Streaming的新手。我正在尝试读取xml文件并将其发送到kafka主题。这是我的Kafka代码,它将数据发送给Kafka-console-consumer。
代码:
package org.apache.kafka.Kafka_Producer;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutionException;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
@SuppressWarnings("unused")
public class KafkaProducer {
private static String sCurrentLine;
public static void main(String args[]) throws InterruptedException, ExecutionException{
try (BufferedReader br = new BufferedReader(new FileReader("/Users/sreeharsha/Downloads/123.txt")))
{
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
kafka(sCurrentLine);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
}
public static void kafka(String sCurrentLine) {
Properties props = new Properties();
props.put("metadata.broker.list", "localhost:9092");
props.put("serializer.class", "kafka.serializer.StringEncoder");
props.put("partitioner.class","kafka.producer.DefaultPartitioner");
props.put("request.required.acks", "1");
ProducerConfig config = new ProducerConfig(props);
Producer<String, String> producer = new Producer<String, String>(config);
producer.send(new KeyedMessage<String, String>("sample",sCurrentLine));
producer.close();
}
}
我可以在Kafka-Console-Consumer中收到数据。在下面的屏幕截图中,您可以看到我发送给该主题的数据。
现在我需要使用Spark-Streaming流式传输给kafka-console-consumer的数据。这是代码。
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.streaming.Duration;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
public class SparkStringConsumer {
public static void main(String[] args) {
SparkConf conf = new SparkConf()
.setAppName("kafka-sandbox")
.setMaster("local[*]");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaStreamingContext ssc = new JavaStreamingContext(sc, new Duration(2000));
Map<String, String> kafkaParams = new HashMap<>();
kafkaParams.put("metadata.broker.list", "localhost:9092");
Set<String> topics = Collections.singleton("sample");
JavaPairInputDStream<String, String> directKafkaStream = KafkaUtils.createDirectStream(ssc,
String.class, String.class, StringDecoder.class, StringDecoder.class, kafkaParams, topics);
directKafkaStream.foreachRDD(rdd -> {
System.out.println("--- New RDD with " + rdd.partitions().size()
+ " partitions and " + rdd.count() + " records");
rdd.foreach(record -> System.out.println(record._2));
});
ssc.start();
ssc.awaitTermination();
}
}
在提交我的工作时获取空白:
./spark-submit --class org.apache.spark_streaming.Spark_Kafka_Streaming.SparkStringConsumer --master local[4] Spark_Kafka_Streaming-0.0.1-SNAPSHOT.jar
您可以在下面看到数据接收方式的屏幕截图:
使用以下版本:
Spark - 2.0.0
Zookeeper -3.4.6
卡夫卡 - 0.8.2.1
请提出任何建议,
答案 0 :(得分:1)
最后在网上冲浪后我发现了这些解决方案。
不要使用&#34; Spark-Submit&#34;和#34; SetMaster&#34;在同一时间。
还有一件事先运行/提交你的火花罐,然后将数据发送给Kafka-Console-Consumer
工作正常。