Skip to content

Instantly share code, notes, and snippets.

@qxj
Forked from jakekdodd/README.md
Last active August 29, 2015 14:17
Show Gist options
  • Save qxj/d8250bcb6d782d588ad4 to your computer and use it in GitHub Desktop.
Save qxj/d8250bcb6d782d588ad4 to your computer and use it in GitHub Desktop.
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)
{"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