The guide below demonstrates how to get a minimal environment up and running. Choose the distribution that's right for you.
When you're ready to learn more, take the next steps with use-case-driven tutorials.
Confluent Cloud offers a fully managed, cloud-native platform for data in motion powered by Apache Kafka®. Spin up ksqlDB clusters on demand with pay-as-you-go pricing.
Begin by signing up for a Confluent Cloud account. Follow the in-product instructions to launch Kafka and ksqlDB clusters within the Confluent Cloud user interface.
Select the Global access option when creating your ksqlDB application. This grants ksqlDB access to your Kafka cluster at the same level as your Confluent Cloud user account, making Global access a good choice for development environments.
We'll leverage the Confluent Cloud CLI to help expedite the next steps.
Once it's installed, authenticate using your Confluent Cloud credentials.
confluent login
Use the Confluent CLI to list metadata about your ksqlDB cluster. Paste the output of the command into the following field.
Your ksqlDB cluster's status | |
---|---|
Provisioned on Confluent Cloud | |
Available for queries |
confluent ksql app list -o json
Tip: Pasting cluster information above will automatically replace variables like $KSQL_CLUSTER_ID
, in the steps below.
Generate an API key to authenticate with your ksqlDB cluster. Then, save the key and secret locally.
confluent api-key create --resource $KSQL_CLUSTER_ID
Run this command to connect to the ksqlDB server and enter an interactive CLI session.
docker run --rm -it confluentinc/ksqldb-cli:0.29.0 ksql \
-u $KSQL_API_KEY \
-p $KSQL_API_SECRET \
"$KSQL_ENDPOINT"
The first thing we're going to do is create a stream. A stream essentially associates a schema with an underlying Kafka topic.
Copy and paste this statement into your interactive CLI session, and press enter to execute the statement.
Here's what each parameter in the CREATE STREAM statement does:
CREATE STREAM riderLocations (profileId VARCHAR, latitude DOUBLE, longitude DOUBLE)
WITH (kafka_topic='quickstart-locations', value_format='json', partitions=1);
We might also want to keep track of the latest location of the riders using a materialized view. For this we create a table currentLocation by issuing a SELECT statement over the previously created stream. Note that the table will be incrementally updated as new rider location data arrives. We use the LATEST_BY_OFFSET aggregate function to denote the fact that we are only interested in the latest location of a rider.
To make it more fun, let us also materialize a derived table (Table ridersNearMountainView) that captures how far the riders are from a given location or city.
Copy and paste those table statements into your interactive CLI session, and press enter to execute.
Check the documentation for more information about tables and materialized views.
CREATE TABLE currentLocation AS
SELECT profileId,
LATEST_BY_OFFSET(latitude) AS la,
LATEST_BY_OFFSET(longitude) AS lo
FROM riderlocations
GROUP BY profileId
EMIT CHANGES;
CREATE TABLE ridersNearMountainView AS
SELECT ROUND(GEO_DISTANCE(la, lo, 37.4133, -122.1162), -1) AS distanceInMiles,
COLLECT_LIST(profileId) AS riders,
COUNT(*) AS count
FROM currentLocation
GROUP BY ROUND(GEO_DISTANCE(la, lo, 37.4133, -122.1162), -1);
Now, let us run a push query over the stream. Run the given query using your interactive CLI session.
This query will output all rows from the riderLocations stream whose coordinates are within 5 miles of Mountain View.
This is the first thing that may feel a bit unfamiliar to you, because the query will never return until it's terminated. It will perpetually push output rows to the client as events are written to the riderLocations stream.
Leave this query running in the CLI session for now. Next, we're going to write some data into the riderLocations stream so that the query begins producing output.
-- Mountain View lat, long: 37.4133, -122.1162
SELECT * FROM riderLocations
WHERE GEO_DISTANCE(latitude, longitude, 37.4133, -122.1162) <= 5
EMIT CHANGES;
Since the CLI session from step 8 is busy waiting for output from the push query, let's start another session that we can use to write some data into ksqlDB.
docker run --rm -it confluentinc/ksqldb-cli:0.29.0 ksql \
-u $KSQL_API_KEY \
-p $KSQL_API_SECRET \
"$KSQL_ENDPOINT"
Run each of the given INSERT statements within the new CLI session, and keep an eye on the CLI session from step 8 as you do.
The push query will output matching rows in real time as soon as they're written to the riderLocations stream.
INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('c2309eec', 37.7877, -122.4205);
INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('18f4ea86', 37.3903, -122.0643);
INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('4ab5cbad', 37.3952, -122.0813);
INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('8b6eae59', 37.3944, -122.0813);
INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('4a7c7b41', 37.4049, -122.0822);
INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('4ddad000', 37.7857, -122.4011);
Finally, we run a pull query against the materialized view to retrieve all the riders that are currently within 10 miles from Mountain View.
In contrast to the previous push query which runs continuously, the pull query follows a traditional request-response model retrieving the latest result from the materialized view.
SELECT * from ridersNearMountainView WHERE distanceInMiles <= 10;