首页 文章

制片人/消费者Kafka Spark Streaming

提问于
浏览
0

我正在尝试使用Kafka和Spark Streaming和Python编写Producer和Consumer的代码;场景如下:有一个关于Json格式的odometry的随机消息的 生产环境 者,它使用线程在主题上每隔3秒发布一次消息:

from kafka import KafkaProducer
from kafka.errors import KafkaError import threading
from random import randint import random
import json
import math

def sendMessage():

    #the function is called every 3 seconds, then a message is sent every 3 seconds
    threading.Timer(3.0, sendMessage).start()

    #connection with message broker
    producer = KafkaProducer(bootstrap_servers=['localhost:9092'], value_serializer=lambda m: json.dumps(m).encode('ascii'))    

    #the id is initially fixed to 1, but there could be more robots
    robotId = 1
    #generation of random int
    deltaSpace = randint(1, 9) #.encode()
    thetaTwist = random.uniform(0, math.pi*2) #.encode()


    future = producer.send('odometry', key=b'message', value={'robotId': robotId, 'deltaSpace': deltaSpace, 'thetaTwist': thetaTwist}).add_callback(on_send_success).add_errback(on_send_error)

    # Block for 'synchronous' sends
    try:
        record_metadata = future.get(timeout=10)
    except KafkaError:
    # Decide what to do if produce request failed...
        log.exception()
        pass

    producer.flush()

def on_send_success(record_metadata):
    print ("topic name: " + record_metadata.topic)
    print ("number of partitions: " + str(record_metadata.partition))
    print ("offset: " + str(record_metadata.offset))

def on_send_error(excp):
    log.error('I am an errback', exc_info=excp)
    # handle exception

sendMessage()

然后有一个消费者在同一个主题上每隔3秒消耗一次消息并用Spark Streaming处理它们;这是代码:

from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
import json 

# Create a local StreamingContext with two working thread and batch interval of 3 second
sc = SparkContext("local[2]", "OdometryConsumer")
ssc = StreamingContext(sc, 3)

kafkaStream = KafkaUtils.createDirectStream(ssc, ['odometry'], {'metadata.broker.list': 'localhost:9092'})

parsed = kafkaStream.map(lambda v: json.loads(v))

def f(x): 
    print(x)

fore = parsed.foreachRDD(f) 


ssc.start()             # Start the computation
ssc.awaitTermination()  # Wait for the computation to terminate

要运行该应用程序,我在端口2181上启动zookeeper服务器

sudo /opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties

然后我在端口9092上启动Kafka的服务器/代理

sudo /opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties

然后我启动制作人和消费者

python3 Producer.py

./spark-submit --jars spark-streaming-kafka-0-8-assembly_2.11-2.3.1.jar /home/erca/Scrivania/proveTesi/SparkConsumer.py

应用程序运行没有错误,但我不确定消息是否真正被消耗;我该怎么做才能验证?谢谢所有帮助我的人!

1 回答

  • 0

    ssc.start() 之前使用 parsed.pprint() 它将在控制台上打印记录

相关问题