Apache kafka KSQL表或流中包含部分原始JSON消息的字符串字段
是否可以将字符串字段添加到KSQL表/流中,该表/流将包含原始消息的JSON的一部分 比如说, 原始信息:Apache kafka KSQL表或流中包含部分原始JSON消息的字符串字段,apache-kafka,ksqldb,Apache Kafka,Ksqldb,是否可以将字符串字段添加到KSQL表/流中,该表/流将包含原始消息的JSON的一部分 比如说, 原始信息: {userId:12345, service:"service-1", "debug":{ "msg":"Debug message", "timer": 11.12} } 因此,我们需要将userId映射到userId BIGINT,service映射到service STRING,debug映射到debug STRING,该字符串将包
{userId:12345,
service:"service-1",
"debug":{
"msg":"Debug message",
"timer": 11.12}
}
因此,我们需要将
userId
映射到userId BIGINT
,service
映射到service STRING
,debug
映射到debug STRING
,该字符串将包含{“msg”:“debug message”,“timer”:11.12}
作为字符串。是的,您可以简单地将其声明为VARCHAR
。从这里,您可以将其视为恰好是JSON的字符串,或者可以使用EXTRACTJSONFIELD
函数进一步操作它
将示例消息发送到主题:
echo '{"userId":12345, "service":"service-1", "debug":{ "msg":"Debug message", "timer": 11.12} }' | kafkacat -b localhost:9092 -t test_topic -P
声明流:
ksql> CREATE STREAM demo (userid BIGINT, service VARCHAR, debug VARCHAR) WITH (KAFKA_TOPIC='test_topic', VALUE_FORMAT='JSON');
Message
----------------
Stream created
----------------
查询列:
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 USERID, SERVICE, DEBUG FROM demo;
12345 | service-1 | {"msg":"Debug message","timer":11.12}
访问嵌套的JSON字段:
ksql> SELECT USERID, SERVICE, EXTRACTJSONFIELD(DEBUG,'$.msg') FROM demo;
12345 | service-1 | Debug message
ksql> SELECT USERID, SERVICE, EXTRACTJSONFIELD(DEBUG,'$.timer') FROM demo;
12345 | service-1 | 11.12