This Apache Druid extension enables Druid to ingest and understand the Protobuf data format. Make sure to include druid-protobuf-extensions in the extensions load list.

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

Example: Load Protobuf messages from Kafka

This example demonstrates how to load Protobuf messages from Kafka. Please read the Load from Kafka tutorial first, and see Kafka Indexing Service 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. {
  2. "unit": "milliseconds",
  3. "http_method": "GET",
  4. "value": 44,
  5. "timestamp": "2017-04-06T02:36:22Z",
  6. "http_code": "200",
  7. "page": "/",
  8. "metricType": "request/latency",
  9. "server": "www1.example.com"
  10. }

Proto file

The corresponding proto file for our ‘metrics’ dataset looks like this. You can use Protobuf inputFormat with a proto file or Confluent Schema Registry.

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

When using a descriptor file

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:

  1. protoc -o /tmp/metrics.desc ./quickstart/protobuf/metrics.proto

When using Schema Registry

Make sure your Schema Registry version is later than 5.5. Next, we can post a schema to add it to the registry:

  1. POST /subjects/test/versions HTTP/1.1
  2. Host: schemaregistry.example1.com
  3. Accept: application/vnd.schemaregistry.v1+json, application/vnd.schemaregistry+json, application/json
  4. {
  5. "schemaType": "PROTOBUF",
  6. "schema": "syntax = \"proto3\";\nmessage Metrics {\n string unit = 1;\n string http_method = 2;\n int32 value = 3;\n string timestamp = 4;\n string http_code = 5;\n string page = 6;\n string metricType = 7;\n string server = 8;\n}\n"
  7. }

This feature uses Confluent’s Protobuf provider which is not included in the Druid distribution and must be installed separately. You can fetch it and its dependencies from the Confluent repository and Maven Central at:

Copy or symlink those files inside the folder extensions/protobuf-extensions under the distribution root directory.

Create Kafka Supervisor

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

When using a descriptor file

Important supervisor properties

  • protoBytesDecoder.descriptor for the descriptor file URL
  • protoBytesDecoder.protoMessageType from the proto definition
  • protoBytesDecoder.type set to file, indicate use descriptor file to decode Protobuf file
  • inputFormat should have type set to protobuf
  1. {
  2. "type": "kafka",
  3. "spec": {
  4. "dataSchema": {
  5. "dataSource": "metrics-protobuf",
  6. "timestampSpec": {
  7. "column": "timestamp",
  8. "format": "auto"
  9. },
  10. "dimensionsSpec": {
  11. "dimensions": [
  12. "unit",
  13. "http_method",
  14. "http_code",
  15. "page",
  16. "metricType",
  17. "server"
  18. ],
  19. "dimensionExclusions": [
  20. "timestamp",
  21. "value"
  22. ]
  23. },
  24. "metricsSpec": [
  25. {
  26. "name": "count",
  27. "type": "count"
  28. },
  29. {
  30. "name": "value_sum",
  31. "fieldName": "value",
  32. "type": "doubleSum"
  33. },
  34. {
  35. "name": "value_min",
  36. "fieldName": "value",
  37. "type": "doubleMin"
  38. },
  39. {
  40. "name": "value_max",
  41. "fieldName": "value",
  42. "type": "doubleMax"
  43. }
  44. ],
  45. "granularitySpec": {
  46. "type": "uniform",
  47. "segmentGranularity": "HOUR",
  48. "queryGranularity": "NONE"
  49. }
  50. },
  51. "tuningConfig": {
  52. "type": "kafka",
  53. "maxRowsPerSegment": 5000000
  54. },
  55. "ioConfig": {
  56. "topic": "metrics_pb",
  57. "consumerProperties": {
  58. "bootstrap.servers": "localhost:9092"
  59. },
  60. "inputFormat": {
  61. "type": "protobuf",
  62. "protoBytesDecoder": {
  63. "type": "file",
  64. "descriptor": "file:///tmp/metrics.desc",
  65. "protoMessageType": "Metrics"
  66. },
  67. "flattenSpec": {
  68. "useFieldDiscovery": true
  69. },
  70. "binaryAsString": false
  71. },
  72. "taskCount": 1,
  73. "replicas": 1,
  74. "taskDuration": "PT1H",
  75. "type": "kafka"
  76. }
  77. }
  78. }

To adopt to old version. You can use old parser style, which also works.

  1. {
  2. "parser": {
  3. "type": "protobuf",
  4. "descriptor": "file:///tmp/metrics.desc",
  5. "protoMessageType": "Metrics"
  6. }
  7. }

When using Schema Registry

Important supervisor properties

  • protoBytesDecoder.url for the schema registry URL with single instance.
  • protoBytesDecoder.urls for the schema registry URLs with multi instances.
  • protoBytesDecoder.capacity capacity for schema registry cached schemas.
  • protoBytesDecoder.config to send additional configurations, configured for Schema Registry.
  • protoBytesDecoder.headers to send headers to the Schema Registry.
  • protoBytesDecoder.type set to schema_registry, indicate use schema registry to decode Protobuf file.
  • parser should have type set to protobuf, but note that the format of the parseSpec must be json.
  1. {
  2. "parser": {
  3. "type": "protobuf",
  4. "protoBytesDecoder": {
  5. "urls": ["http://schemaregistry.example1.com:8081","http://schemaregistry.example2.com:8081"],
  6. "type": "schema_registry",
  7. "capacity": 100,
  8. "config" : {
  9. "basic.auth.credentials.source": "USER_INFO",
  10. "basic.auth.user.info": "fred:letmein",
  11. "schema.registry.ssl.truststore.location": "/some/secrets/kafka.client.truststore.jks",
  12. "schema.registry.ssl.truststore.password": "<password>",
  13. "schema.registry.ssl.keystore.location": "/some/secrets/kafka.client.keystore.jks",
  14. "schema.registry.ssl.keystore.password": "<password>",
  15. "schema.registry.ssl.key.password": "<password>",
  16. ...
  17. },
  18. "headers": {
  19. "traceID" : "b29c5de2-0db4-490b-b421",
  20. "timeStamp" : "1577191871865",
  21. ...
  22. }
  23. }
  24. }
  25. }

Adding Protobuf messages to Kafka

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 | python /quickstart/protobuf/pb_publisher.py

You can confirm that data has been inserted to your Kafka topic using the following command from your Kafka installation directory

  1. ./bin/kafka-console-consumer --zookeeper localhost --topic metrics_pb

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.

Generating the example files

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

metrics.desc

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

metrics_pb2.py is also generated with protoc

  1. protoc -o metrics.desc metrics.proto --python_out=.

pb_publisher.py

After metrics_pb2.py is generated, another script can be constructed to parse JSON data, convert it to Protobuf, and produce to a Kafka topic

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