Suppose you want to create reports from a table and all the timestamps must be in a particular time zone, which happens to be different from the timezone of the Kafka data source. This tutorial shows how you can convert timestamp data into another timezone.
You'll start with a stream of temperature readings sourced from a Kafka topic named device-events. The timestamps are in Unix time format of a long which is a BIGINT in ksqlDB.
CREATE STREAM temperature_readings_raw (event_time BIGINT, temperature INT)
WITH (KAFKA_TOPIC='device-events',
VALUE_FORMAT='JSON');
In order to convert this column to timestamp format and in a particular time zone (America/Denver), first convert event_time from a BIGINT to a timestamp with the FROM_UNIXTIME function. Then the CONVERT_TZ function uses the result to produce a timestamp in the desired time zone.
SELECT temperature,CONVERT_TZ(FROM_UNIXTIME(event_time), 'UTC', 'America/Denver') AS event_time_mt
FROM temperature_readings_raw
EMIT CHANGES;
You can run the example backing this tutorial in one of two ways: locally with the ksql CLI against Kafka and ksqlDB running in Docker, or with Confluent Cloud.
Clone the confluentinc/tutorials GitHub repository (if you haven't already) and navigate to the tutorials directory:
git clone git@github.com:confluentinc/tutorials.git
cd tutorials
Start ksqlDB and Kafka:
docker compose -f ./docker/docker-compose-ksqldb.yml up -d
Next, open the ksqlDB CLI:
docker exec -it ksqldb-cli ksql http://ksqldb-server:8088
Run the following SQL statements to create the temperature_readings_raw stream backed by Kafka running in Docker and populate it with test data.
CREATE STREAM temperature_readings_raw (event_time BIGINT, temperature INT)
WITH (KAFKA_TOPIC='device-events',
PARTITIONS=1,
VALUE_FORMAT='JSON');
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615566394751, 100);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615566401534, 132);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567732840, 144);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567735866, 103);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567736875, 102);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567738890, 101);
Finally, run the timestamp conversion query. Note that we first tell ksqlDB to consume from the beginning of the stream.
SET 'auto.offset.reset'='earliest';
SELECT temperature,
CONVERT_TZ(FROM_UNIXTIME(event_time), 'UTC', 'America/Denver') AS event_time_mt
FROM temperature_readings_raw
EMIT CHANGES;
The query output should look like this:
+-------------------------------------+-------------------------------------+
|TEMPERATURE |EVENT_TIME_MT |
+-------------------------------------+-------------------------------------+
|100 |2021-03-12T09:26:34.751 |
|132 |2021-03-12T09:26:41.534 |
|144 |2021-03-12T09:48:52.840 |
|103 |2021-03-12T09:48:55.866 |
|102 |2021-03-12T09:48:56.875 |
|101 |2021-03-12T09:48:58.890 |
+-------------------------------------+-------------------------------------+
When you are finished, exit the ksqlDB CLI by entering CTRL-D and clean up the containers used for this tutorial by running:
docker compose -f ./docker/docker-compose-ksqldb.yml down
Login to your Confluent Cloud account:
confluent login --prompt --save
Install a CLI plugin that will streamline the creation of resources in Confluent Cloud:
confluent plugin install confluent-cloud_kickstart
Run the following command to create a Confluent Cloud environment and Kafka cluster. This will create resources in AWS region us-west-2 by default, but you may override these choices by passing the --cloud argument with a value of aws, gcp, or azure, and the --region argument that is one of the cloud provider's supported regions, which you can list by running confluent kafka region list --cloud <CLOUD PROVIDER>
confluent cloud-kickstart --name ksqldb-tutorial \
--environment-name ksqldb-tutorial \
--output-format stdout
Now, create a ksqlDB cluster by first getting your user ID of the form u-123456 when you run this command:
confluent iam user list
And then create a ksqlDB cluster called ksqldb-tutorial with access linked to your user account:
confluent ksql cluster create ksqldb-tutorial \
--credential-identity <USER ID>
Login to the Confluent Cloud Console. Select Environments in the lefthand navigation, and then click the ksqldb-tutorial environment tile. Click the ksqldb-tutorial Kafka cluster tile, and then select ksqlDB in the lefthand navigation.
The cluster may take a few minutes to be provisioned. Once its status is Up, click the cluster name and scroll down to the editor.
In the query properties section at the bottom, change the value for auto.offset.reset to Earliest so that ksqlDB will consume from the beginning of the stream we create.
Enter the following statements in the editor and click Run query. This creates the temperature_readings_raw stream and populates it with test data.
CREATE STREAM temperature_readings_raw (event_time BIGINT, temperature INT)
WITH (KAFKA_TOPIC='device-events',
PARTITIONS=1,
VALUE_FORMAT='JSON');
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615566394751, 100);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615566401534, 132);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567732840, 144);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567735866, 103);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567736875, 102);
INSERT INTO temperature_readings_raw (event_time, temperature) VALUES (1615567738890, 101);
Now paste the timestamp conversion query in the editor and click Run query:
SELECT temperature,
CONVERT_TZ(FROM_UNIXTIME(event_time), 'UTC', 'America/Denver') AS event_time_mt
FROM temperature_readings_raw
EMIT CHANGES;
The query output should look like this:
+-------------------------------------+-------------------------------------+
|TEMPERATURE |EVENT_TIME_MT |
+-------------------------------------+-------------------------------------+
|100 |2021-03-12T09:26:34.751 |
|132 |2021-03-12T09:26:41.534 |
|144 |2021-03-12T09:48:52.840 |
|103 |2021-03-12T09:48:55.866 |
|102 |2021-03-12T09:48:56.875 |
|101 |2021-03-12T09:48:58.890 |
+-------------------------------------+-------------------------------------+
When you are finished, delete the ksqldb-tutorial environment by first getting the environment ID of the form env-123456 corresponding to it:
confluent environment list
Delete the environment, including all resources created for this tutorial:
confluent environment delete <ENVIRONMENT ID>