'Create Table without data aggregation

I just started to use the ksqlDB Confluent feature, and it stood out that it is not possible to proceed with the following command: CREATE TABLE AS SELECT A, B, C FROM [STREAM_A] [EMIT CHANGES];

I wonder why this is not possible or if there's a way of doing it? Data aggregation here is feeling a heavy process to a simple solution.

Edit 1: Source is a STREAM and not a TABLE. The field types are:

  • String
  • Integers
  • Record

Let me share an example of the executed command that returns an error as a result.

CREATE TABLE test_table
WITH (KEY_FORMAT='JSON',VALUE_FORMAT='AVRO')
AS
SELECT id
    , timestamp
    , servicename
    , content->assignedcontent
FROM created_stream
WHERE content->assignedcontent IS NOT NULL
[EMIT CHANGES]; 


Solution 1:[1]

create a table with a smaller dataset and fewer fields than the original topic

I think the confusion here is that you talk about a TABLE, but you're actually creating a STREAM. The two are different types of object.

  • A STREAM is an unbounded series of events - just like a Kafka topic. The only difference is that a STREAM has a declared schema.
  • A TABLE is state, for a given key. It's the same as KTable in Kafka Streams if you're familiar with that.

Both are backed by Kafka topics.

So you can do this - note that it's creating a STREAM not a TABLE

CREATE STREAM test_stream
WITH (KEY_FORMAT='JSON',VALUE_FORMAT='AVRO')
AS
SELECT id
    , timestamp
    , servicename
    , content->assignedcontent
FROM created_stream
WHERE content->assignedcontent IS NOT NULL;

If you really want to create a TABLE then use the LATEST_BY_OFFSET aggregation, assuming you'd using id as your key:

CREATE TABLE test_table
WITH (KEY_FORMAT='JSON',VALUE_FORMAT='AVRO')
AS
SELECT id
    , LATEST_BY_OFFSET(timestamp)
    , LATEST_BY_OFFSET(servicename)
    , LATEST_BY_OFFSET(content->assignedcontent)
FROM created_stream
WHERE content->assignedcontent IS NOT NULL
GROUP BY id;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1