Kafka Consumer没有收到其制作人的任何消息

时间:2016-09-13 03:55:26

标签: python apache-spark apache-kafka spark-streaming kafka-python

以下是我对kafka制作人的python编码,我不确定能否发布给Kafka Broker的消息。因为消费者方面没有收到任何消息。我的Consumer python程序工作正常,而我使用producer console命令测试它。

from __future__ import print_function

import sys
from pyspark import SparkContext
from kafka import KafkaClient, SimpleProducer

if __name__ == "__main__":

if len(sys.argv) != 2:
    print("Usage:spark-submit producer1.py <input file>", file=sys.stderr)
    exit(-1)

sc = SparkContext(appName="PythonRegression")

def sendkafka(messages):
    ## Set broker port
    kafka = KafkaClient("localhost:9092")
    producer = SimpleProducer(kafka, async=True, batch_send_every_n=5,  
batch_send_every_t=10)
    send_counts = 0
    for message in messages:
        try:
            print(message)
            ## Set topic name and push messages to the Kafka Broker
            yield producer.send_messages('test', message.encode('utf-8'))
        except Exception, e:
            print("Error: %s" % str(e))
        else:
            send_counts += 1
    print("The count of prediction results which were sent IN THIS PARTITION 
is %d.\n" % send_counts)

## Connect and read the file.    
rawData = sc.textFile(sys.argv[1])

## Find and skip the first row
dataHeader = rawData.first()
data =  rawData.filter(lambda x: x != dataHeader)

## Collect the RDDs.
sentRDD = data.mapPartitions(sendkafka) 
sentRDD.collect()

## Stop file connection
sc.stop()

这是我的“消费者”python编码

from __future__ import print_function
import sys
from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils

if len(sys.argv) < 3:
print ("Program to pulls the messages from kafka brokers.")
print("Usage: consume.py <zk> <topic>", file=sys.stderr)

else:
## Flow
## Loads settings from system properties, for launching of spark-submit.
sc = SparkContext(appName="PythonStreamingKafkaWordCount")

## Create a StreamingContext using an existing SparkContext.
ssc = StreamingContext(sc, 10)

## Get everything after the python script name
zkQuorum, topic = sys.argv[1:]

## Create an input stream that pulls messages from Kafka Brokers.
kvs = KafkaUtils.createStream(ssc, zkQuorum, "spark-streaming-consumer", 
{topic: 1})

## 
lines = kvs.map(lambda x: x[1])

## Print the messages pulled from Kakfa Brokers
lines.pprint()

## Save the pulled messages as file
## lines.saveAsTextFiles("OutputA")

## Start receiving data and processing it
ssc.start()

## Allows the current process to wait for the termination of the context 
ssc.awaitTermination()

3 个答案:

答案 0 :(得分:0)

我通常使用kafka-console-consumer(Apache Kafka的一部分)调试此类问题,以便从您尝试生成的主题中使用。如果控制台消费者收到消息,你知道他们到达了Kafka。

如果你第一次运行生产者,让它完成,然后启动消费者,那么问题可能是消费者从日志的末尾开始并正在等待其他消息。要么确保首先启动使用者,要么将其配置为在开始时自动启动(抱歉,不知道如何使用Python客户端执行此操作)。

答案 1 :(得分:0)

如果使用Produce请求增加消息,您可以检查主题中的消息数:

./bin/kafka-run-class.sh kafka.tools.GetOffsetShell \
--broker-list <Kafka_broker_hostname>:<broker_port> --topic Que1 \ 
--time -1 --offsets 1 | awk -F  ":" '{sum += $3} END {print sum}'

如果消息数量增加,则表示生产者工作正常。

答案 2 :(得分:0)

好吧我认为我的本地Zookeeper或Kafka有问题,因为我在另一台服务器上测试它完美无缺。但是,感谢那些回复我的人;)