运行consumer时出现错误未知错误:org.apache.kafka.common.errors.serializationexception:未知幻码字节

vcudknz3  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(516)

我正在尝试从python向kafka消费者发送消息。但我也犯了错误 ERROR Unknown error when running consumer: org.apache.kafka.common.errors.SerializationException: Unknown magic byte! python正确地从twitterapi获取数据,但是无法将消息发送给消费者。任何建议都会有帮助。
代码:

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from kafka import KafkaProducer

access_token = "xxxx"
access_token_secret = "xxxx"
consumer_key = "xxxx"
consumer_secret = "xxxx"

class StdOutListener(StreamListener):
    def on_data(self, data):
        producer.send("twitter", data.encode('utf-8'))
        print (data)
        return True
    def on_error(self, status):
        print (status)

producer = KafkaProducer(bootstrap_servers='127.0.0.1:9092')
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track="Bitcoin")

消费者:

kafka-avro-console-consumer --bootstrap-server 127.0.0.1:9092 --topic twitter

我也试过提供房产,

afka-avro-console-consumer --bootstrap-server 127.0.0.1:9092 --topic twitter --property print.key=true --property print.value=true --value-deserializer io.confluent.kafka.serializers.KafkaAvroDeserializer --key-deserializer org.apache.kafka.common.serialization.StringDeserializer

错误:

[2020-08-14 07:23:52,305] ERROR Unknown error when running consumer:  (kafka.tools.ConsoleConsumer$:76)
org.apache.kafka.common.errors.SerializationException: Error deserializing Protobuf message for id -1
Caused by: org.apache.kafka.common.errors.SerializationException: Unknown magic byte!
0lvr5msh

0lvr5msh1#

错误似乎认为它得到了原始信息
反序列化protobuf消息时出错
但你用的是 kafka-avro-console-consumer 同时,您的代码正在发送utf-8编码的字符串,因此您不需要任何合流工具,只要 kafka-console-consumer

相关问题