# Utilize topic keys in KSQL Stream

**URL:** <https://forum.confluent.io/t/utilize-topic-keys-in-ksql-stream/2473>\
**Category:** ksqlDB\
**Created:** [10 August 2021 19:08 UTC](https://forum.confluent.io/t/utilize-topic-keys-in-ksql-stream/2473 "2021-08-10T19:08:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheACG](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@TheACG](https://forum.confluent.io/u/TheACG)\
**Post date:** [10 August 2021 19:08 UTC](https://forum.confluent.io/t/utilize-topic-keys-in-ksql-stream/2473/1 "2021-08-10T19:08:27Z")

</div>

I am looking to incorporate the key value from a topic’s messages into a KSQLDB Stream to be a unique identifier. The keys are unique to what sensor the data is coming from. I am in a situation where the format of the messages coming from our topic cannot be changed and the three columns of data we are receiving do not have anything that would be able to identify what sensor that row came from. Is there a way to include the key from each topic message into the Stream?

I have seen some older posts online that refer to ROWKEY as something I can use in a select statement, but that cannot be resolved when I try it. I am only able to use ROWTIME.

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [10 August 2021 21:08 UTC](https://forum.confluent.io/t/utilize-topic-keys-in-ksql-stream/2473/2 "2021-08-10T21:08:16Z")

</div>

How did you declare the schema of the STREAM? If you want a column to take the data from the message key, you can declare the column as `KEY`:

```sql
CREATE STREAM foo (myKey VARCHAR KEY, someOtherColumn INT,...)

```

Column `myKey` will get the data from the message key, as it’s declared as `KEY` column. The message key must be a `String` for this case (as we used type `VARCHAR`) – of course, you can also use any other data type. If you have a complex key (eg, JSON), you can also declare multiple `KEY` columns (similar to “value” columns).

Cf [Data definition - ksqlDB Documentation](https://docs.ksqldb.io/en/latest/reference/sql/data-definition/#keys)

> I have seen some older posts online that refer to ROWKEY

In older versions, it was not possible to just declare some “key columns” in the schema but ksqlDB provided the `ROWKEY` column. Using `ROWKEY` column has quite some disadvantages: (1) the name is hard coded and cannot be customized; (2) only primitive data types could be support.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [9 September 2021 19:09 UTC](https://forum.confluent.io/t/utilize-topic-keys-in-ksql-stream/2473/3 "2021-09-09T19:09:14Z")

</div>

This topic was automatically closed after 30 days. New replies are no longer allowed.
