writer = avro.io.DatumWriter(schema)
bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)
writer.write({"name": "123", "favorite_color": "111", "favorite_number": random.randint(0,10)}, encoder)
raw_bytes = bytes_writer.getvalue()
producer.send_messages(topic, raw_bytes)
in this way, the raw_bytes got in program not right, lack schema info. the avro python give example ,that's ok, but it through file, it is not what i want .
schema = avro.schema.parse('{"namespace": "example.avro","type": "record","name": "User","fields": [{"name": "name", "type": "string"},{"name": "favorite_number", "type": ["int", "null"]},{"name": "favorite_color", "type": ["string", "null"]}]}')
writer = DataFileWriter(open("/tmp/users.avro", "w"), DatumWriter(), schema)
s=json.loads('{"name":"china","favorite_number":256}')
writer.append(s)
writer.append({"name":"mjx","favorite_number":256})
writer.close()
producer = KafkaProducer(bootstrap_servers='192.168.10.247:9092')
with open('/tmp/users.avro', 'rb') as f:
for line in f.readlines():
producer.send('my-topic2', line)
break;
in this way, the raw_bytes got in program not right, lack schema info. the avro python give example ,that's ok, but it through file, it is not what i want .
schema = avro.schema.parse('{"namespace": "example.avro","type": "record","name": "User","fields": [{"name": "name", "type": "string"},{"name": "favorite_number", "type": ["int", "null"]},{"name": "favorite_color", "type": ["string", "null"]}]}')
writer = DataFileWriter(open("/tmp/users.avro", "w"), DatumWriter(), schema)
s=json.loads('{"name":"china","favorite_number":256}')
writer.append(s)
writer.append({"name":"mjx","favorite_number":256})
writer.close()
producer = KafkaProducer(bootstrap_servers='192.168.10.247:9092')
with open('/tmp/users.avro', 'rb') as f:
for line in f.readlines():
producer.send('my-topic2', line)
break;