-
-
Save qxj/d8250bcb6d782d588ad4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import avro.schema | |
import avro.io | |
import lipsum | |
import random | |
from kafka.client import KafkaClient | |
from kafka.producer import SimpleProducer, KeyedProducer | |
g = lipsum.Generator() | |
kafka = KafkaClient("localhost:9092") | |
producer = SimpleProducer(kafka) | |
# Path to user.avsc avro schema | |
schema_path="/Your/schema/path/user.avsc" | |
# Kafka topic | |
topic = "test" | |
schema = avro.schema.parse(open(schema_path).read()) | |
for i in xrange(2000): | |
writer = avro.io.DatumWriter(schema) | |
bytes_writer = io.BytesIO() | |
encoder = avro.io.BinaryEncoder(bytes_writer) | |
writer.write({"name": g.generate_sentence(), "favorite_color": g.generate_sentence(), "favorite_number": random.randint(0,10)}, encoder) | |
raw_bytes = bytes_writer.getvalue() | |
producer.send_messages(topic, raw_bytes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"namespace": "example.avro", | |
"type": "record", | |
"name": "User", | |
"fields": [ | |
{"name": "name", "type": "string"}, | |
{"name": "favorite_number", "type": ["int", "null"]}, | |
{"name": "favorite_color", "type": ["string", "null"]} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment