Protobuf

    The druid-protobuf-extensions provides the Protobuf Parser for . See corresponding docs for details.

    This example demonstrates how to load Protobuf messages from Kafka. Please read the Load from Kafka tutorial first, and see documentation for more details.

    The files used in this example are found at ./examples/quickstart/protobuf in your Druid directory.

    For this example:

    • Kafka broker host is localhost:9092
    • Kafka topic is metrics_pb
    • Datasource name is metrics-protobuf

    Here is a JSON example of the ‘metrics’ data schema used in the example.

    1. syntax = "proto3";
    2. message Metrics {
    3. string unit = 1;
    4. string http_method = 2;
    5. string timestamp = 4;
    6. string http_code = 5;
    7. string page = 6;
    8. string metricType = 7;
    9. string server = 8;
    10. }

    Next, we use the protoc Protobuf compiler to generate the descriptor file and save it as metrics.desc. The descriptor file must be either in the classpath or reachable by URL. In this example the descriptor file was saved at /tmp/metrics.desc, however this file is also available in the example files. From your Druid install directory:

    Below is the complete Supervisor spec JSON to be submitted to the Overlord. Make sure these keys are properly configured for successful ingestion.

    Important supervisor properties

    • descriptor for the descriptor file URL
    • protoMessageType from the proto definition
    • parser should have type set to protobuf, but note that the format of the parseSpec must be json

    If necessary, from your Kafka installation directory run the following command to create the Kafka topic

    1. ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic metrics_pb

    This example script requires protobuf and kafka-python modules. With the topic in place, messages can be inserted running the following command from your Druid installation directory

    1. ./bin/generate-example-metrics | ./quickstart/protobuf/pb_publisher.py

    which should print messages like this

    1. millisecondsGETR"2017-04-06T03:23:56Z*2002/list:request/latencyBwww1.example.com

    If your supervisor created in the previous step is running, the indexing tasks should begin producing the messages and the data will soon be available for querying in Druid.

    The files provided in the example quickstart can be generated in the following manner starting with only metrics.proto.

    The descriptor file is generated using protoc Protobuf compiler. Given a .proto file, a .desc file can be generated like so.

    1. protoc -o metrics.desc metrics.proto

    metrics_pb2.py is also generated with protoc

    1. #!/usr/bin/env python
    2. import json
    3. from kafka import KafkaProducer
    4. from metrics_pb2 import Metrics
    5. producer = KafkaProducer(bootstrap_servers='localhost:9092')
    6. topic = 'metrics_pb'
    7. for row in iter(sys.stdin):
    8. d = json.loads(row)
    9. metrics = Metrics()
    10. for k, v in d.items():
    11. setattr(metrics, k, v)
    12. pb = metrics.SerializeToString()
    13. producer.send(topic, pb)
    14. producer.flush()