Kafka基于Receiver的开发

2023-02-25 15:59:36 浏览数 (2)

基于Receiver的方式 这种方式使用Receiver来获取数据。Receiver是使用Kafka的高层次Consumer API来实现的。receiver从Kafka中获取的数据都是存储在Spark Executor的内存中的,然后Spark Streaming启动的job会去处理那些数据。

然而,在默认的配置下,这种方式可能会因为底层的失败而丢失数据。如果要启用高可靠机制,让数据零丢失,就必须启用Spark Streaming的预写日志机制(Write Ahead Log,WAL)。该机制会同步地将接收到的Kafka数据写入分布式文件系统(比如HDFS)上的预写日志中。所以,即使底层节点出现了失败,也可以使用预写日志中的数据进行恢复。

如何进行Kafka数据源连接

1、在maven添加依赖 groupId = org.apache.spark artifactId = spark-streaming-kafka_2.10 version = 1.5.1

2、使用第三方工具类创建输入DStream JavaPairReceiverInputDStream<String, String> kafkaStream = KafkaUtils.createStream(streamingContext, [ZK quorum], [consumer group id], [per-topic number of Kafka partitions to consume]);

Kafka命令 bin/kafka-topics.sh --zookeeper 192.168.1.107:2181,192.168.1.108:2181,192.168.1.109:2181 --topic TestTopic --replication-factor 1 --partitions 1 --create

bin/kafka-console-producer.sh --broker-list 192.168.1.107:9092,192.168.1.108:9092,192.168.1.109:9092 --topic TestTopic

192.168.1.191:2181,192.168.1.192:2181,192.168.1.193:2181

代码语言:javascript复制
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; 
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaPairReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.apache.spark.streaming.kafka.KafkaUtils;
import scala.Tuple2;

public class KafkaReceiverWordCount {
​public static void main(String[] args) {
SparkConf conf = new SparkConf()​​.setMaster("local[2]")​​.setAppName("KafkaWordCount");  
JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(5));
// 使用KafkaUtils.createStream()方法,创建针对Kafka的输入数据流
Map<String, Integer> topicThreadMap = new HashMap<String, Integer>();

topicThreadMap.put("WordCount", 1);

JavaPairReceiverInputDStream<String, String> lines = KafkaUtils.createStream(jssc,​​"spark1:2181,spark2:2181,spark3:2181", ​​"DefaultConsumerGroup",​​topicThreadMap);

 // 然后开发wordcount逻辑
JavaDStream<String> words = lines.flatMap(

new FlatMapFunction<Tuple2<String,String>, String>() {

private static final long serialVersionUID = 1L;

​​​@Override
​​​public Iterable<String> call(Tuple2<String, String> tuple) ​​​​​throws Exception {
​​​​return Arrays.asList(tuple._2.split(" "));  
​​​}
​​});

JavaPairDStream<String, Integer> pairs = words.mapToPair(

new PairFunction<String, String, Integer>() {

​​​private static final long serialVersionUID = 1L;

​​​@Override
​​​public Tuple2<String, Integer> call(String word) ​​​​​throws Exception {
​​​​return new Tuple2<String, Integer>(word, 1);
​​​}
​​});
JavaPairDStream<String, Integer> wordCounts = pairs.reduceByKey(

​​new Function2<Integer, Integer, Integer>() {
private static final long serialVersionUID = 1L;
  ​​​@Override
​​​public Integer call(Integer v1, Integer v2) throws Exception {
​​​​return v1   v2;
​​​}
​​});

  wordCounts.print();  

 jssc.start();
 jssc.awaitTermination();
 jssc.close();
​}
} 

0 人点赞