Apache Kafka ingestion

When you enable the Kafka indexing service, you can configure supervisors on the Overlord to manage the creation and lifetime of Kafka indexing tasks.

Kafka indexing tasks read events using Kafka’s own partition and offset mechanism to guarantee exactly-once ingestion. The supervisor oversees the state of the indexing tasks to:

  • coordinate handoffs
  • manage failures
  • ensure that scalability and replication requirements are maintained.

This topic covers how to submit a supervisor spec to ingest event data, also known as message data, from Kafka. See the following for more information:

Kafka support

The Kafka indexing service supports transactional topics introduced in Kafka 0.11.x by default. The consumer for Kafka indexing service is incompatible with older Kafka brokers. If you are using an older version, refer to the Kafka upgrade guide.

Additionally, you can set isolation.level to read_uncommitted in consumerProperties if either:

  • You don’t need Druid to consume transactional topics.
  • You need Druid to consume older versions of Kafka. Make sure offsets are sequential, since there is no offset gap check in Druid anymore.

If your Kafka cluster enables consumer-group based ACLs, you can set group.id in consumerProperties to override the default auto generated group id.

Load the Kafka indexing service

To use the Kafka indexing service, load the druid-kafka-indexing-service extension on both the Overlord and the MiddleManagers. See Loading extensions for instructions on how to configure extensions.

Define a supervisor spec

Similar to the ingestion spec for batch ingestion, the supervisor spec configures the data ingestion for Kafka streaming ingestion. A supervisor spec has the following sections:

  • dataSchema to specify the Druid datasource name, primary timestamp, dimensions, metrics, transforms, and any necessary filters.
  • ioConfig to configure Kafka connection settings and configure how Druid parses the data. Kafka-specific connection details go in the consumerProperties. The ioConfig is also where you define the input format (inputFormat) of your Kafka data. For supported formats for Kafka and information on how to configure the input format, see Data formats.
  • tuningConfig to control various tuning parameters specific to each ingestion method. For a full description of all the fields and parameters in a Kafka supervisor spec, see the Kafka supervisor reference.

The following sections contain examples to help you get started with supervisor specs.

JSON input format supervisor spec example

The following example demonstrates a supervisor spec for Kafka that uses the JSON input format. In this case Druid parses the event contents in JSON format:

  1. {
  2. "type": "kafka",
  3. "spec": {
  4. "dataSchema": {
  5. "dataSource": "metrics-kafka",
  6. "timestampSpec": {
  7. "column": "timestamp",
  8. "format": "auto"
  9. },
  10. "dimensionsSpec": {
  11. "dimensions": [],
  12. "dimensionExclusions": [
  13. "timestamp",
  14. "value"
  15. ]
  16. },
  17. "metricsSpec": [
  18. {
  19. "name": "count",
  20. "type": "count"
  21. },
  22. {
  23. "name": "value_sum",
  24. "fieldName": "value",
  25. "type": "doubleSum"
  26. },
  27. {
  28. "name": "value_min",
  29. "fieldName": "value",
  30. "type": "doubleMin"
  31. },
  32. {
  33. "name": "value_max",
  34. "fieldName": "value",
  35. "type": "doubleMax"
  36. }
  37. ],
  38. "granularitySpec": {
  39. "type": "uniform",
  40. "segmentGranularity": "HOUR",
  41. "queryGranularity": "NONE"
  42. }
  43. },
  44. "ioConfig": {
  45. "topic": "metrics",
  46. "inputFormat": {
  47. "type": "json"
  48. },
  49. "consumerProperties": {
  50. "bootstrap.servers": "localhost:9092"
  51. },
  52. "taskCount": 1,
  53. "replicas": 1,
  54. "taskDuration": "PT1H"
  55. },
  56. "tuningConfig": {
  57. "type": "kafka",
  58. "maxRowsPerSegment": 5000000
  59. }
  60. }
  61. }

Kafka input format supervisor spec example

If you want to parse the Kafka metadata fields in addition to the Kafka payload value contents, you can use the kafka input format.

The kafka input format wraps around the payload parsing input format and augments the data it outputs with the Kafka event timestamp, the Kafka event headers, and the key field that itself can be parsed using any available InputFormat.

For example, consider the following structure for a Kafka message that represents a fictitious wiki edit in a development environment:

  • Kafka timestamp: 1680795276351
  • Kafka headers:
    • env=development
    • zone=z1
  • Kafka key: wiki-edit
  • Kafka payload value: {"channel":"#sv.wikipedia","timestamp":"2016-06-27T00:00:11.080Z","page":"Salo Toraut","delta":31,"namespace":"Main"}

Using { "type": "json" } as the input format would only parse the payload value. To parse the Kafka metadata in addition to the payload, use the kafka input format.

You would configure it as follows:

  • valueFormat: Define how to parse the payload value. Set this to the payload parsing input format ({ "type": "json" }).
  • timestampColumnName: Supply a custom name for the Kafka timestamp in the Druid schema to avoid conflicts with columns from the payload. The default is kafka.timestamp.
  • headerFormat: The default value string decodes strings in UTF-8 encoding from the Kafka header. Other supported encoding formats include the following:
    • ISO-8859-1: ISO Latin Alphabet No. 1, that is, ISO-LATIN-1.
    • US-ASCII: Seven-bit ASCII. Also known as ISO646-US. The Basic Latin block of the Unicode character set.
    • UTF-16: Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.
    • UTF-16BE: Sixteen-bit UCS Transformation Format, big-endian byte order.
    • UTF-16LE: Sixteen-bit UCS Transformation Format, little-endian byte order.
  • headerColumnPrefix: Supply a prefix to the Kafka headers to avoid any conflicts with columns from the payload. The default is kafka.header.. Considering the header from the example, Druid maps the headers to the following columns: kafka.header.env, kafka.header.zone.
  • keyFormat: Supply an input format to parse the key. Only the first value will be used. If, as in the example, your key values are simple strings, then you can use the tsv format to parse them.

    1. {
    2. "type": "tsv",
    3. "findColumnsFromHeader": false,
    4. "columns": ["x"]
    5. }

    Note that for tsv,csv, and regex formats, you need to provide a columns array to make a valid input format. Only the first one is used, and its name will be ignored in favor of keyColumnName.

  • keyColumnName: Supply the name for the Kafka key column to avoid conflicts with columns from the payload. The default is kafka.key.

Putting it together, the following input format (that uses the default values for timestampColumnName, headerColumnPrefix, and keyColumnName)

  1. {
  2. "type": "kafka",
  3. "valueFormat": {
  4. "type": "json"
  5. },
  6. "headerFormat": {
  7. "type": "string"
  8. },
  9. "keyFormat": {
  10. "type": "tsv",
  11. "findColumnsFromHeader": false,
  12. "columns": ["x"]
  13. }
  14. }

would parse the example message as follows:

  1. {
  2. "channel": "#sv.wikipedia",
  3. "timestamp": "2016-06-27T00:00:11.080Z",
  4. "page": "Salo Toraut",
  5. "delta": 31,
  6. "namespace": "Main",
  7. "kafka.timestamp": 1680795276351,
  8. "kafka.header.env": "development",
  9. "kafka.header.zone": "z1",
  10. "kafka.key": "wiki-edit"
  11. }

For more information on data formats, see Data formats.

Finally, add these Kafka metadata columns to the dimensionsSpec or set your dimensionsSpec to auto-detect columns.

The following supervisor spec demonstrates how to ingest the Kafka header, key, and timestamp into Druid dimensions:

  1. {
  2. "type": "kafka",
  3. "spec": {
  4. "ioConfig": {
  5. "type": "kafka",
  6. "consumerProperties": {
  7. "bootstrap.servers": "localhost:9092"
  8. },
  9. "topic": "wiki-edits",
  10. "inputFormat": {
  11. "type": "kafka",
  12. "valueFormat": {
  13. "type": "json"
  14. },
  15. "headerFormat": {
  16. "type": "string"
  17. },
  18. "keyFormat": {
  19. "type": "tsv",
  20. "findColumnsFromHeader": false,
  21. "columns": ["x"]
  22. }
  23. },
  24. "useEarliestOffset": true
  25. },
  26. "dataSchema": {
  27. "dataSource": "wikiticker",
  28. "timestampSpec": {
  29. "column": "timestamp",
  30. "format": "posix"
  31. },
  32. "dimensionsSpec": "dimensionsSpec": {
  33. "useSchemaDiscovery": true,
  34. "includeAllDimensions": true
  35. },
  36. "granularitySpec": {
  37. "queryGranularity": "none",
  38. "rollup": false,
  39. "segmentGranularity": "day"
  40. }
  41. },
  42. "tuningConfig": {
  43. "type": "kafka"
  44. }
  45. }
  46. }

After Druid ingests the data, you can query the Kafka metadata columns as follows:

  1. SELECT
  2. "kafka.header.env",
  3. "kafka.key",
  4. "kafka.timestamp"
  5. FROM "wikiticker"

This query returns:

kafka.header.envkafka.keykafka.timestamp
developmentwiki-edit1680795276351

For more information, see kafka data format.

Submit a supervisor spec

Druid starts a supervisor for a dataSource when you submit a supervisor spec. You can use the data loader in the web console or you can submit a supervisor spec to the following endpoint:

http://<OVERLORD_IP>:<OVERLORD_PORT>/druid/indexer/v1/supervisor

For example:

  1. curl -X POST -H 'Content-Type: application/json' -d @supervisor-spec.json http://localhost:8090/druid/indexer/v1/supervisor

Where the file supervisor-spec.json contains your Kafka supervisor spec file.