简体   繁体   中英

How to use string array field in Kafka ksql

After creating users stream with its interests field as a string array ARRAY<STRING> data type:

CREATE STREAM users
  (userid VARCHAR,
   interests ARRAY<STRING>)
  WITH (KAFKA_TOPIC = 'users',
        VALUE_FORMAT='JSON');

I go ahead and insert the first user data with:

INSERT INTO users (userid, interests) VALUES ('user0001', ['music','sport'])

Unfortunately this insert statement fails with an error

line 1:60: extraneous input '[' expecting {'(', 'STRING'...

I tried to replace the square bracket [] with () as:

INSERT INTO users (userid, interests) VALUES ('user0001', ('music','sport'))

but it fails too.

How to fix this error? Is there a way to store a sting array ['music','sport'] using string array data type?

You have to use the ARRAY function:

https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-reference/scalar-functions/#array

INSERT INTO users (userid, interests) VALUES ('user0001', ARRAY['music','sport']);

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