简体   繁体   中英

Are nested objects defined in JSON supported in KSQL using the STRUCT type?

How to create a KSQL stream listening on the TOPIC T where the JSON structure of the message is:

{"k":"1","a":{"b":1,"c":{"d":10}}}

I tried the following and it does not work. Getting a syntax error.

create stream s (k VARCHAR,a STRUCT <b INT ,c <STRUCT d INT >> ) 
                 with (KAFKA_TOPIC='T',VALUE_FORMAT='JSON',KEY='k')

Here's an example of how to do it. I've got the test data in a topic:

ksql> PRINT test FROM BEGINNING;
Format:JSON
{"ROWTIME":1578571011016,"ROWKEY":"null","k":"1","a":{"b":1,"c":{"d":10}}}
^CTopic printing ceased

Declare the stream:

ksql> CREATE STREAM TEST (K VARCHAR, 
                          A STRUCT<B INT,
                                   C STRUCT<D INT>>
                          ) WITH (KAFKA_TOPIC='test', 
                                  VALUE_FORMAT='JSON');

 Message
----------------
 Stream created
----------------

Query the stream:

ksql> SET 'auto.offset.reset' = 'earliest';
Successfully changed local property 'auto.offset.reset' to 'earliest'. Use the UNSET command to revert your change.

ksql> SELECT * FROM TEST EMIT CHANGES LIMIT 1;
+---------------------+----------+-----+---------------------+
|ROWTIME              |ROWKEY    |K    |A                    |
+---------------------+----------+-----+---------------------+
|1578571011016        |null      |1    |{B=1, C={D=10}}      |
Limit Reached
Query terminated

ksql> SELECT K, A, A->B, A->C, A->C->D FROM TEST EMIT CHANGES LIMIT 1;
+----+-----------------+-------+---------+----------+
|K   |A                |A__B   |A__C     |A__C__D   |
+----+-----------------+-------+---------+----------+
|1   |{B=1, C={D=10}}  |1      |{D=10}   |10        |
Limit Reached
Query terminated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM