spark 消費kafka的數據


通過flume將日志數據讀取到kafka中,然后再利用spark去消費kafka的數據,

1.保證zookeeper服務一直開啟

2.配置flume文件,其配置信息如下

a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /opt/data/wfbmall/16/wfbmall.log

# Describe the sink
#a1.sinks.k1.type = logger
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.topic = test
a1.sinks.k1.brokerList = master:9092
a1.sinks.k1.requiredAcks = 1
a1.sinks.k1.batchSize = 20

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 10000
a1.channels.c1.byteCapacityBufferPercentage = 20
a1.channels.c1.byteCapacity = 800000
a1.channels.c1.transactionCapacity = 100
a1.channels.c1.keep-alive = 60   
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

可以通過 來查看kafka里的數據

[root@master ~]# /opt/soft/kafka_2.11-0.11/bin/kafka-console-consumer.sh --bootstrap-server master:9092 --from-beginning --topic test

4.最后可以配置saprk 進行實時統計數據

# coding=UTF-8
from __future__ import print_function 
import sys 
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils


sc = SparkContext(appName="streamingkafka")
sc.setLogLevel("WARN") # 減少shell打印日志
ssc = StreamingContext(sc, 5) # 5秒的計算窗口
brokers='master:9092'
topic = 'test'
kafka_streaming_rdd = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": brokers})
lines_rdd = kafka_streaming_rdd.map(lambda x: x[1]).filter(lambda x: "login, start" in x).map(lambda x: ("login_start", 1)).reduceByKey(lambda a, b : a + b)

lines_rdd.pprint()
ssc.start()
ssc.awaitTermination()

5.進行調用文件

[root@master spark-2.4.5]# bin/spark-submit --jars jars/spark-streaming-kafka-0-8_2.11-2.3.3.jar bin/kafka-spark_20210401.py

其顯示結果為

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM