gpt4 book ai didi

apache-kafka - KSQL 流 - 从结构数组中获取数据

转载 作者:行者123 更新时间:2023-12-04 02:41:02 26 4
gpt4 key购买 nike

我的 JSON 看起来像:

{
"Obj1": {
"a": "abc",
"b": "def",
"c": "ghi"
},
"ArrayObj": [
{
"key1": "1",
"Key2": "2",
"Key3": "3",

},
{
"key1": "4",
"Key2": "5",
"Key3": "6",

},
{
"key1": "7",
"Key2": "8",
"Key3": "9",

}
]

}


我已经编写了 KSQL 流将其转换为 AVRO 并保存到一个主题,以便我可以将其推送到 JDBC Sink 连接器
CREATE STREAM Example1(ArrayObj ARRAY<STRUCT<key1 VARCHAR, Key2 VARCHAR>>,Obj1 STRUCT<a VARCHAR>)WITH(kafka_topic='sample_topic', value_format='JSON');
CREATE STREAM Example_Avro WITH(VALUE_FORMAT='avro') AS SELECT e.ArrayObj[0] FROM Example1 e;

在 Example_Avro 中,我只能获取数组中的第一个对象。

当我在 KSQL 中点击 select * from Example_Avro 时,如何获得如下所示的数据?
  a    b   key1   key2  key3

abc def 1 2 3
abc def 4 5 6
abc def 7 8 9

最佳答案

测试数据(我在 key3 值之后删除了无效的尾随逗号):

ksql> PRINT test4;
Format:JSON
1/9/20 7:45:18 PM UTC , NULL , { "Obj1": { "a": "abc", "b": "def", "c": "ghi" }, "ArrayObj": [ { "key1": "1", "Key2": "2", "Key3": "3" }, { "key1": "4", "Key2": "5", "Key3": "6" }, { "key1": "7", "Key2": "8", "Key3": "9" } ] }

询问:

SELECT OBJ1->A AS A, 
OBJ1->B AS B,
EXPLODE(ARRAYOBJ)->KEY1 AS KEY1,
EXPLODE(ARRAYOBJ)->KEY2 AS KEY2,
EXPLODE(ARRAYOBJ)->KEY3 AS KEY3
FROM TEST4
EMIT CHANGES;

结果:
+-------+-------+------+-------+-------+
|A |B |KEY1 |KEY2 |KEY3 |
+-------+-------+------+-------+-------+
|abc |def |1 |2 |3 |
|abc |def |4 |5 |6 |
|abc |def |7 |8 |9 |

在 ksqlDB 0.6 上测试,其中 EXPLODE添加了功能。

关于apache-kafka - KSQL 流 - 从结构数组中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59429813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com