標籤:spark streaming flume kafka direct java
王家林老師的課程:2016年大資料Spark“蘑菇雲”行動之spark streaming消費flume採集的kafka資料Directf方式作業。
一、基本背景
Spark-Streaming擷取kafka資料的兩種方式Receiver與Direct的方式,本文介紹Direct的方式。具體的流程是這樣的:
1、Direct方式是直接連接到kafka的節點上擷取資料了。
2、基於Direct的方式:周期性地查詢Kafka,來獲得每個topic+partition的最新的offset,從而定義每個batch的offset的範圍。
3、當處理資料的job啟動時,就會使用Kafka的簡單consumer api來擷取Kafka指定offset範圍的資料。
這種方式有如下優點:
1、簡化並行讀取:如果要讀取多個partition,不需要建立多個輸入DStream然後對它們進行union操作。Spark會建立跟Kafka partition一樣多的RDD partition,並且會並行從Kafka中讀取資料。所以在Kafka partition和RDD partition之間,有一個一對一的映射關係。;
2、高效能:不需要開啟WAL機制,只要Kafka中作了資料的複製,那麼就可以通過Kafka的副本進行恢複;
3、一次且僅一次的事務機制:Spark Streaming自己就負責追蹤消費的offset,並儲存在checkpoint中。
Spark自己一定是同步的,因此可以保證資料是消費一次且僅消費一次。
二、設定檔及編碼
flume版本:1.6.0,此版本直接支援到kafka,不用在單獨安裝外掛程式。
kafka版本2.10-0.8.2.1,必須是0.8.2.1,剛開始我用的是0.10,結果出現了下
四、各類錯誤大全的第2個錯誤。
spark版本:1.6.1。
kafka配檔案:producer.properties,紅色文字為特別要注意的配置坑,呵呵
#agentsection
producer.sources= s
producer.channels= c
producer.sinks= r
#sourcesection
producer.sources.s.type= exec
producer.sources.s.command= tail -f -n+1 /opt/test/test.log
producer.sources.s.channels= c
# Eachsink‘s type must be defined
producer.sinks.r.type= org.apache.flume.plugins.KafkaSink
producer.sinks.r.metadata.broker.list=192.168.0.10:9092
producer.sinks.r.partition.key=0
producer.sinks.r.partitioner.class=org.apache.flume.plugins.SinglePartition
producer.sinks.r.serializer.class=kafka.serializer.StringEncoder
producer.sinks.r.request.required.acks=0
producer.sinks.r.max.message.size=1000000
producer.sinks.r.producer.type=sync
producer.sinks.r.custom.encoding=UTF-8
producer.sinks.r.custom.topic.name=flume2kafka2streaming930
#Specifythe channel the sink should use
producer.sinks.r.channel= c
# Eachchannel‘s type is defined.
producer.channels.c.type= memory
producer.channels.c.capacity= 1000
producer.channels.c.transactionCapacity= 100
核心代碼如下:
SparkConf conf = SparkConf().setMaster(). setAppName() .setJars(String[] { })Map<StringString> kafkaParameters = HashMap<StringString>()kafkaParameters.put()Set<String> topics = HashSet<String>()topics.add()JavaPairInputDStream<StringString> lines = KafkaUtils.(jscString.String.StringDecoder.StringDecoder.kafkaParameterstopics)JavaDStream<String> words = lines.flatMap(FlatMapFunction<Tuple2<StringString>String>() { Iterable<String> (Tuple2<StringString> tuple) Exception { Arrays.(tuple..split())} })JavaPairDStream<StringInteger> pairs = words.mapToPair(PairFunction<StringStringInteger>() { Tuple2<StringInteger> (String word) Exception { Tuple2<StringInteger>(word)} })JavaPairDStream<StringInteger> wordsCount = pairs.reduceByKey(Function2<IntegerIntegerInteger>() { Integer (Integer v1Integer v2) Exception { v1 + v2} })wordsCount.print()jsc.start()jsc.awaitTermination()jsc.close()
三、啟動指令碼
啟動zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties &
啟動kafka broker
bin/kafka-server-start.sh config/server.properties &
建立topic
bin/kafka-topics.sh --create --zookeeper 192.168.0.10:2181 --replication-factor 1 --partitions 1 --topic flume2kafka2streaming930
啟動flume
bin/flume-ng agent --conf conf/ -f conf/producer.properties -n producer -Dflume.root.logger=INFO,console
bin/spark-submit --class com.dt.spark.sparkstreaming.SparkStreamingOnKafkaDirected --jars /lib/kafka_2.10-0.8.2.1/kafka-clients-0.8.2.1.jar,/lib/kafka_2.10-
0.8.2.1/kafka_2.10-0.8.2.1.jar,/lib/kafka_2.10-0.8.2.1/metrics-core-2.2.0.jar,/lib/spark-1.6.1/spark-streaming-kafka_2.10-1.6.1.jar --master local[5] SparkApps.jar
echo "hadoop spark hive storm spark hadoop hdfs" >> /opt/test/test.log
echo "hive storm " >> /opt/test/test.log
echo "hdfs" >> /opt/test/test.log
echo "hadoop spark hive storm spark hadoop hdfs" >> /opt/test/test.log
輸出結果如下:
* 結果如下:
* -------------------------------------------
* Time: 1475282360000 ms
* -------------------------------------------
*(spark,8)
*(storm,4)
*(hdfs,4)
*(hive,4)
*(hadoop,8)
四、各類錯誤大全
1、Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/kafka/KafkaUtils
at com.dt.spark.SparkApps.SparkStreaming.SparkStreamingOnKafkaDirected.main
一概是沒有提交jar包,一概會報錯,無法執行,一概在submit指令碼裡添加:
bin/spark-submit --class com.dt.spark.sparkstreaming.SparkStreamingOnKafkaDirected --jars /lib/kafka_2.10-0.8.2.1/kafka-clients-0.8.2.1.jar,/lib/kafka_2.10-
0.8.2.1/kafka_2.10-0.8.2.1.jar,/lib/kafka_2.10-0.8.2.1/metrics-core-2.2.0.jar,/lib/spark-1.6.1/spark-streaming-kafka_2.10-1.6.1.jar --master local[5] SparkApps.jar
2、Exception in thread "main" java.lang.ClassCastException: kafka.cluster.BrokerEndPoint cannot be cast to kafka.cluster.Broker。
上stackoverflow.com及spark官網查詢,這個是因為版本不相容引起。官網提供的版本:Spark Streaming 1.6.1 is compatible with Kafka 0.8.2.1
王家林_DT大資料夢工廠
簡介: 王家林:DT大資料夢工廠創始人和首席專家.公眾號DT_Spark .
聯絡郵箱[email protected]
電話:18610086859
號:18610086859
微博為:http://weibo.com/ilovepains
2016年大資料Spark“蘑菇雲”行動之spark streaming消費flume採集的kafka資料Directf方式