简体   繁体   中英

How to filter using ksql with array attribute type

I have a Stream on a topic with schema:

--root
  --name: string
  --age: integer
  --accounts: Array
    --email

I would like to select all root elements having accounts with email ='xx'

Thanks you for help.

select * from t1 where accounts->email='xx'

假设表或流名称是 t1。

In general case a new stream with exploded array elements must be created

create stream ExplodedStream
with(kafka_topic='ExplodedTopic')
as
select
  root->name,
  root->age,
  explode(root->accounts) as account
from OriginStream

This stream will explode one message with multiple accounts into multiple messages with one account inside. Messages will be stored in a new ExplodedTopic topic. In your case streams and topics names will be other. This topic messages can be easily filtered as

select * 
from ExplodedStream 
where account->email='xx'

If number of accounts are known then filtering can be done without creating a new stream. Just filter by all known account elements

select * 
from OriginStream 
where 
  root->accounts[1]->email='xx'
  or root->accounts[2]->email='xx'
  ...
  or root->accounts[n]->email='xx'

Number of array element is one based (1,2,3,...)

I guess you could do something like this

where array_length( filter(accounts, a => (a->email = 'xxx@xxx.xx') )) >=1

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