python kafka库的编码/格式化问题

时间:2016-09-14 11:46:52

标签: python apache-kafka kafka-python

我一直在尝试使用python kafka库,但无法让生产者工作。

经过一番研究后,我发现kafka向消费者发送了一个额外的5字节头(一个0字节,一个包含schema-registry模式ID的长度)(并且我猜也是这样)。我设法通过简单地剥离第一个字节来让消费者工作。

我是否应该在撰写制作人时添加类似的标题?

在出现的异常之下:

    [2016-09-14 13:32:48,684] ERROR Task hdfs-sink-0 threw an uncaught and unrecoverable exception (org.apache.kafka.connect.runtime.WorkerTask:142)
org.apache.kafka.connect.errors.DataException: Failed to deserialize data to Avro: 
    at io.confluent.connect.avro.AvroConverter.toConnectData(AvroConverter.java:109)
    at org.apache.kafka.connect.runtime.WorkerSinkTask.convertMessages(WorkerSinkTask.java:357)
    at org.apache.kafka.connect.runtime.WorkerSinkTask.poll(WorkerSinkTask.java:226)
    at org.apache.kafka.connect.runtime.WorkerSinkTask.iteration(WorkerSinkTask.java:170)
    at org.apache.kafka.connect.runtime.WorkerSinkTask.execute(WorkerSinkTask.java:142)
    at org.apache.kafka.connect.runtime.WorkerTask.doRun(WorkerTask.java:140)
    at org.apache.kafka.connect.runtime.WorkerTask.run(WorkerTask.java:175)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: org.apache.kafka.common.errors.SerializationException: Error deserializing Avro message for id -1
    Caused by: org.apache.kafka.common.errors.SerializationException: Unknown magic byte!

我正在使用kafka和python-kafka的最新稳定版本。

修改

消费

from kafka import KafkaConsumer
import avro.io
import avro.schema
import io
import requests
import struct

# To consume messages
consumer = KafkaConsumer('hadoop_00',
                         group_id='my_group',
                         bootstrap_servers=['hadoop-master:9092'])

schema_path = "resources/f1.avsc"
for msg in consumer:
    value = bytearray(msg.value)
    schema_id = struct.unpack(">L", value[1:5])[0]
    response = requests.get("http://hadoop-master:8081/schemas/ids/" + str(schema_id))
    schema = response.json()["schema"]
    schema = avro.schema.parse(schema)
    bytes_reader = io.BytesIO(value[5:])
    # bytes_reader = io.BytesIO(msg.value)
    decoder = avro.io.BinaryDecoder(bytes_reader)
    reader = avro.io.DatumReader(schema)
    temp = reader.read(decoder)
    print(temp)

生产者

from kafka import KafkaProducer
import avro.schema
import io
from avro.io import DatumWriter

producer = KafkaProducer(bootstrap_servers="hadoop-master")

# Kafka topic
topic = "hadoop_00"

# Path to user.avsc avro schema
schema_path = "resources/f1.avsc"
schema = avro.schema.parse(open(schema_path).read())
range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range:
    producer.send(topic, b'{"f1":"value_' + str(i))

2 个答案:

答案 0 :(得分:1)

我可以让我的python生成器向Schema-Registry发送消息给Kafka-Connect:

...
import avro.datafile
import avro.io
import avro.schema
from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='kafka:9092')
with open('schema.avsc') as f:
    schema = avro.schema.Parse(f.read())

def post_message():
    bytes_writer = io.BytesIO()
    # Write the Confluent "Magic Byte"
    bytes_writer.write(bytes([0]))
    # Should get or create the schema version with Schema-Registry
    ...
    schema_version = 1
    bytes_writer.write(
        int.to_bytes(schema_version, 4, byteorder='big'))

    # and then the standard Avro bytes serialization
    writer = avro.io.DatumWriter(schema)
    encoder = avro.io.BinaryEncoder(bytes_writer)
    writer.write({'key': 'value'}, encoder)
    producer.send('topic', value=bytes_writer.getvalue())

有关“魔术字节”的文档: https://github.com/confluentinc/schema-registry/blob/master/docs/serializer-formatter.rst

答案 1 :(得分:0)

由于您正在使用BinaryDecoder和DatumReader进行读取,如果您反向发送数据(使用带有BinaryEncoder的DatumWriter作为编码器),我想您的消息就可以了。

这样的事情:

<强>生产者

from kafka import KafkaProducer
import avro.schema
import io
from avro.io import DatumWriter, BinaryEncoder
producer = KafkaProducer(bootstrap_servers="hadoop-master")

# Kafka topic
topic = "hadoop_00"

# Path to user.avsc avro schema
schema_path = "resources/f1.avsc"
schema = avro.schema.parse(open(schema_path).read())
# range is a bad variable name. I changed it here
value_range = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in value_range:
    datum_writer = DatumWriter(schema)
    byte_writer = io.BytesIO()
    datum_encoder = BinaryEncoder(byte_writer)
    datum_writer.write({"f1" : "value_%d" % (i)}, datum_encoder)
    producer.send(topic, byte_writer.getvalue())

我所做的一些改变是:

  • 使用DatumWriter和BinaryEncoder
  • 而不是json,我在字节流中发送一个字典(你可能需要用普通字典检查你的代码,它也可以工作;但我不确定)
  • 使用字节流将消息发送到kafka主题(对我而言,有时它失败了,在这种情况下,我将.getvalue方法分配给变量并使用producer.send中的变量。我不知道失败的原因,但分配给变量始终有效)

我无法测试我添加的代码。但这是我之前使用avro时编写的代码片段。如果它不适合您,请在评论中告诉我。这可能是因为我生锈的记忆。一旦我到达我家可以测试代码,我将用一个工作的答案更新这个答案。