错误 运行 消费者时出现未知错误:org.apache.kafka.common.errors.SerializationException:未知魔法字节

ERROR Unknown error when running consumer: org.apache.kafka.common.errors.SerializationException: Unknown magic byte

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

代码:

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!

错误似乎认为它正在获取 Proto 消息

Error deserializing Protobuf message

但是你正在使用 kafka-avro-console-consumer


同时,您的代码正在发送 utf-8 编码的字符串,因此您不需要任何 Confluent 工具,只需 kafka-console-consumer