Current date in KSQL

Get local date in ksql

Confluent Platform is a full-scale data streaming managed cloud service that enables us to access, store, and manage data as a continuous, real-time stream. Confluent Cloud provides ksqlDB, a streaming SQL engine for Kafka, and it is an easy-to-use yet powerful interactive SQL interface for stream processing on Kafka. In this article, you will learn how to get current date in KSQL.

Syntax

select UNIX_TIMESTAMP() as current_date from your_stream emit changes;
get current date in ksql Confluent.

 The function UNIX_TIMESTAMP returns the BIGINT value representing the number of milliseconds since 1970-01-01T00:00:00 UTC.

Convert TIMESTAMP from BIGINT to DateTime in KSQL.

If you want to convert the millisecond value to a TIMESTAMP, use the FROM_UNIXTIME.

select FROM_UNIXTIME(UNIX_TIMESTAMP()) as current_date from your_stream emit changes;
get current date in ksql using FROM_UNIXTIME(UNIX_TIMESTAMP())

FORMAT_TIMESTAMP() can also be used further to format timestamps in the required format. Read more about DateTimeFormater here.

select FROM FORMAT_TIMESTAMP(FROM_UNIXTIME(unix_timestamp)) as current_date from your_stream emit changes;

Pro tips:
1. unix_timestamp() function to get current date in ksql, when used in a persistent query, is evaluated only once when creating a persistent query.
2. If you’d like to automate the deployment of ksqlDB streams and tables, you can read more about it here.

See more

Kunal Rathi

With over a decade of experience in data engineering and analytics, I've assisted countless clients in gaining valuable insights from their data. As a dedicated supporter of Data, Cloud and DevOps, I'm excited to connect with individuals who share my passion for this field. If my work resonates with you, we can talk and collaborate.
I am always interested in new challenges so if you need consulting help, reach me at kunalrathi55@gmail.com.

Shopping Cart
Scroll to Top