Get Started with SQL Over Kafka
In this tutorial, you use an interactive SQL shell on a Hazelcast member to query Kafka topics in real-time.
Before You Begin
To complete this tutorial, you need the following:
Prerequisites | Useful resources |
---|---|
A Hazelcast cluster in client/server mode and an instance of Management Center running on your local network |
|
A connection to the SQL shell |
Step 1. Set Up a Kafka Broker
-
On the same device as your Hazelcast member, start a Kafka broker.
-
Download Kafka.
-
Start Zookeeper.
-
In another terminal, start Kafka.
-
Step 2. Create a Mapping to Kafka
In the SQL shell, create a Kafka mapping to allow Hazelcast to access messages in the trades
topic.
Here, you configure the connector to read JSON values with the following fields:
Step 3. Run a Streaming Query on the Kafka Topic
-
Write a streaming query that filters trade messages from Kafka.
You should see an empty table:
Streaming queries like this one continue to run until you close the shell or kill the process with Ctrl+C. -
In another terminal, open another connection to the SQL shell and publish some messages to the
trades
topic. -
Go back to the terminal where you created the streaming query.
You should see that Hazelcast has executed the query and filtered the results:
Step 4. Enrich the Data in the Kafka Messages
Kafka messages are often small and contain minimal data to reduce network latency. For example, the trades
topic does not contain any information about the company that’s associated with a given ticker. To get deeper insights from data in Kafka topics, you can join query results with data in other mappings.
-
Create a mapping to a new map in which to store the company information that you’ll use to enrich results from the
trades
topic. -
Add some entries to the
companies
map. -
Use the
JOIN
clause to merge results from thecompanies
map andtrades
topic so you can see which companies are being traded. -
In another SQL shell, publish some messages to the
trades
topic. -
Go back to the terminal where you created the streaming query that merges results from the
companies
map andtrades
topic.You should see that Hazelcast has executed the query.
Step 5. Ingest Query Results into a Hazelcast Map
To save your query results as a view that you can later access faster, you can cache them in Hazelcast by ingesting them into a map.
-
Create a mapping to a new map in which to ingest your streaming query results.
-
Submit a streaming job to your cluster that will monitor your
trade
topic for changes and store them in a map.A streaming job will run indefinitely until it is explicitly canceled or the cluster is shut down. Even if you kill the shell connection, the job will continue running on the cluster. -
List your job to make sure that it was successfully submitted.
You should see a job called
ingest_trades
. -
Publish some messages to the Kafka topic.
-
Query your
trade_map
map to see that the Kafka messages have been added to it.You should see that the data coming from the Kafka broker is being stored in your map.
Step 6. Cancel the Job
A streaming job will run indefinitely until it is explicitly canceled or the cluster is shut down. Even if you kill the shell connection, the job will continue running on the cluster.
In the terminal where you started the Hazelcast member, you should see that the job is canceled as well as the time it was started and how long it ran for.
Next Steps
To learn more about SQL, see SQL
To learn how to work with jobs, see the following: