示例#1
0
    if not ADDRESS:
        raise ValueError("No EventHubs URL supplied.")

    # Create Event Hubs client
    client = EventHubClient(ADDRESS, debug=False, username=USER, password=KEY)
    sender = client.add_sender(partition="0")
    client.run()
    try:
        start_time = time.time()
        for i in range(100):
            print("Sending message: {}".format(i))
            writer = DatumWriter(SCHEMA)
            bytes_writer = io.BytesIO()
            encoder = avro.io.BinaryEncoder(bytes_writer)
            writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), SCHEMA)
            writer.append({"name": "Alyssa", "favorite_number": 256})
            writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
            writer.close()
            writer.write({"name": "123", "favorite_number": 10}, encoder)
            raw_bytes = bytes_writer.getvalue()
            sender.send(EventData(raw_bytes))
    except:
        raise
    finally:
        end_time = time.time()
        client.stop()
        run_time = end_time - start_time
        logger.info("Runtime: {} seconds".format(run_time))

except KeyboardInterrupt:
    pass
示例#2
0
KAFKA = KafkaClient('3.0.166.133:9995')
PRODUCER = SimpleProducer(KAFKA)

# Kafka topic
TOPIC = "nextiot"

# Path to user.avsc avro schema
SCHEMA_PATH = "schema.avsc"
SCHEMA = avro.schema.parse(open(SCHEMA_PATH).read())

for i in xrange(10):
    writer = DatumWriter(SCHEMA)
    bytes_writer = io.BytesIO()
    encoder = avro.io.BinaryEncoder(bytes_writer)
    writer.write({"deviceid":"010001","latitude": { "float": 32.34},"longitude": {"float" :23.33}}, encoder)
    raw_bytes = bytes_writer.getvalue()
    PRODUCER.send_messages(TOPIC, raw_bytes)


import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
 
schema = avro.schema.parse(open("schema.avsc").read())  # need to know the schema to write
 
writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
writer.append({"deviceid":"010098"})
writer.append({"latitude": { "float": 2234.34 }, "longitude": { "float": 334.4}})
writer.close()

# {"deviceid":"010098", "latitude": { "float": 2234.34 }, "longitude": { "float": 334.4}}