Ingest streaming data from Apache Kafka
This guide shows you how to ingest a stream of records from an Apache Kafka topic into a Pinot table.
Learn how to ingest data from Kafka, a stream processing platform. You should have a local cluster up and running, following the instructions in Set up a cluster.
Install and Launch Kafka
Let's start by downloading Kafka to our local machine.
<Tabs items={['Docker', 'Launcher Scripts']}> <Tabs.Tab> To pull down the latest Docker image, run the following command:
</Tabs.Tab> <Tabs.Tab> Download Kafka from kafka.apache.org/quickstart#quickstart_download and then extract it:
</Tabs.Tab>
Next we'll spin up a Kafka broker:
<Tabs items={['Docker', 'Launcher Scripts']}>
<Tabs.Tab> bash docker run \ --network pinot-demo \ --name=kafka \ -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181/kafka \ -e KAFKA_BROKER_ID=0 \ -e KAFKA_ADVERTISED_HOST_NAME=kafka \ wurstmeister/kafka:latest
</Tabs.Tab> <Tabs.Tab> On one terminal window run this command:
</Tabs.Tab>
Data Source
We're going to generate some JSON messages from the terminal using the following script:
datagen.py
If you run this script (python datagen.py
), you'll see the following output:
Ingesting Data into Kafka
Let's now pipe that stream of messages into Kafka, by running the following command:
<Tabs items={['Docker', 'Launcher Scripts']}>
<Tabs.Tab> bash python datagen.py | docker exec -i kafka /opt/kafka/bin/kafka-console-producer.sh \ --bootstrap-server localhost:9092 \ --topic events;
</Tabs.Tab> <Tabs.Tab> bash python datagen.py | bin/kafka-console-producer.sh \ --bootstrap-server localhost:9092 \ --topic events;
</Tabs.Tab>
We can check how many messages have been ingested by running the following command:
<Tabs items={['Docker', 'Launcher Scripts']}>
<Tabs.Tab> bash docker exec -i kafka kafka-run-class.sh kafka.tools.GetOffsetShell \ --broker-list localhost:9092 \ --topic events
</Tabs.Tab> <Tabs.Tab> bash kafka-run-class.sh kafka.tools.GetOffsetShell \ --broker-list localhost:9092 \ --topic events
</Tabs.Tab>
Output
And we can print out the messages themselves by running the following command
<Tabs items={['Docker', 'Launcher Scripts']}>
<Tabs.Tab> bash docker exec -i kafka /opt/kafka/bin/kafka-console-consumer.sh \ --bootstrap-server localhost:9092 \ --topic events
</Tabs.Tab> <Tabs.Tab> bash bin/kafka-console-consumer.sh \ --bootstrap-server localhost:9092 \ --topic events
</Tabs.Tab>
Output
Schema
A schema defines what fields are present in the table along with their data types in JSON format.
Create a file called /tmp/pinot/schema-stream.json
and add the following content to it.
Table Config
A table is a logical abstraction that represents a collection of related data. It is composed of columns and rows (known as documents in Pinot). The table config defines the table's properties in JSON format.
Create a file called /tmp/pinot/table-config-stream.json
and add the following content to it.
Create schema and table
Create the table and schema by running the appropriate command below:
<Tabs items={['Docker', 'Launcher Scripts']}>
<Tabs.Tab> bash docker run --rm -ti \ --network=pinot-demo \ -v /tmp/pinot:/tmp/pinot \ apachepinot/pinot:1.0.0 AddSchema \ -schemaFile /tmp/pinot/schema-stream.json \ -tableConfigFile /tmp/pinot/table-config-stream.json \ -controllerHost pinot-controller \ -controllerPort 9000 -exec
</Tabs.Tab> <Tabs.Tab> bash bin/pinot-admin.sh AddTable \ -schemaFile /tmp/pinot/schema-stream.json \ -tableConfigFIle /tmp/pinot/table-config-stream.json
</Tabs.Tab>
Querying
Navigate to localhost:9000/#/query and click on the events
table to run a query that shows the first 10 rows in this table.
## Kafka ingestion guidelines
Kafka versions in Pinot
Pinot supports 2 major generations of Kafka library - kafka-0.9 and kafka-2.x for both high and low level consumers.
Post release 0.10.0, we have started shading kafka packages inside Pinot. If you are using our latest
tagged docker images or master
build, you should replace org.apache.kafka
with shaded.org.apache.kafka
in your table config.
Upgrade from Kafka 0.9 connector to Kafka 2.x connector
Update table config for both high level and low level consumer: Update config:
stream.kafka.consumer.factory.class.name
fromorg.apache.pinot.core.realtime.impl.kafka.KafkaConsumerFactory
toorg.apache.pinot.core.realtime.impl.kafka2.KafkaConsumerFactory
.If using Stream(High) level consumer, also add config
stream.kafka.hlc.bootstrap.server
intotableIndexConfig.streamConfigs
. This config should be the URI of Kafka broker lists, e.g.localhost:9092
.
How to consume from a Kafka version > 2.0.0
This connector is also suitable for Kafka lib version higher than 2.0.0
. In Kafka 2.0 connector pom.xml, change the kafka.lib.version
from 2.0.0
to 2.1.1
will make this Connector working with Kafka 2.1.1
.
Kafka configurations in Pinot
Use Kafka partition (low) level consumer with SSL
Here is an example config which uses SSL based authentication to talk with kafka and schema-registry. Notice there are two sets of SSL options, ones starting with ssl.
are for kafka consumer and ones with stream.kafka.decoder.prop.schema.registry.
are for SchemaRegistryClient
used by KafkaConfluentSchemaRegistryAvroMessageDecoder
.
Consume transactionally-committed messages
The connector with Kafka library 2.0+ supports Kafka transactions. The transaction support is controlled by config kafka.isolation.level
in Kafka stream config, which can be read_committed
or read_uncommitted
(default). Setting it to read_committed
will ingest transactionally committed messages in Kafka stream only.
For example,
Note that the default value of this config read_uncommitted
to read all messages. Also, this config supports low-level consumer only.
Use Kafka partition (low) level consumer with SASL_SSL
Here is an example config which uses SASL_SSL based authentication to talk with kafka and schema-registry. Notice there are two sets of SSL options, some for kafka consumer and ones with stream.kafka.decoder.prop.schema.registry.
are for SchemaRegistryClient
used by KafkaConfluentSchemaRegistryAvroMessageDecoder
.
Extract record headers as Pinot table columns
Pinot's Kafka connector supports automatically extracting record headers and metadata into the Pinot table columns. The following table shows the mapping for record header/metadata to Pinot table column names:
Kafka Record | Pinot Table Column | Description |
---|---|---|
Record key: any type <K> |
| For simplicity of design, we assume that the record key is always a UTF-8 encoded String |
Record Headers: Map<String, String> | Each header key is listed as a separate column:
| For simplicity of design, we directly map the string headers from kafka record to pinot table column |
Record metadata - offset : long |
| |
Record metadata - recordTimestamp : long |
|
In order to enable the metadata extraction in a Kafka table, you can set the stream config metadata.populate
to true
.
In addition to this, if you want to use any of these columns in your table, you have to list them explicitly in your table's schema.
For example, if you want to add only the offset and key as dimension columns in your Pinot table, it can listed in the schema as follows:
Once the schema is updated, these columns are similar to any other pinot column. You can apply ingestion transforms and / or define indexes on them.
Remember to follow the schema evolution guidelines when updating schema of an existing table!
Tell Pinot where to find an Avro schema
There is a standalone utility to generate the schema from an Avro file. See [infer the pinot schema from the avro schema and JSON data](https://docs.pinot.apache.org/basics/data-import/complex-type#infer-the-pinot-schema-from-the-avro-schema-and-json-data) for details.
To avoid errors like The Avro schema must be provided
, designate the location of the schema in your streamConfigs
section. For example, if your current section contains the following:
Then add this key: "stream.kafka.decoder.prop.schema"
followed by a value that denotes the location of your schema.
Last updated