我正在尝试使用 python 2.7 和 Apache Avro(python 客户端)通过 kafka 代理交换序列化消息。我想知道是否有一种方法可以在不创建架构的情况下交换消息。
这是代码(使用模式,sensor.avsc,我想避免的事情):
from kafka import SimpleProducer, KafkaClient
import avro.schema
import io, random
from avro.io import DatumWriter
# To send messages synchronously
kafka = KafkaClient('localhost:9092')
producer = SimpleProducer(kafka, async = False)
# Kafka topic
topic = "sensor_network_01"
# Path to user.avsc avro schema that i don't want
schema_path="sensor.avsc"
schema = avro.schema.parse(open(schema_path).read())
for i in xrange(100):
writer = avro.io.DatumWriter(schema)
bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)
# creation of random data
writer.write({"sensor_network_name": "Sensor_1", "value": random.randint(0,10), "threshold_value":10 }, encoder)
raw_bytes = bytes_writer.getvalue()
producer.send_messages(topic, raw_bytes)
这是 sensor.avsc 文件:
{
"namespace": "sensors.avro",
"type": "record",
"name": "Sensor",
"fields": [
{"name": "sensor_network_name", "type": "string"},
{"name": "value", "type": ["int", "null"]},
{"name": "threshold_value", "type": ["int", "null"]}
]
}