【发布时间】:2021-02-19 01:04:54
【问题描述】:
我有一个包含三列的表格,source_word 、target_word 和 json_col
source_word target_word json_col
source_1 target_1 {"source_1":{"method1":[{"w":"target_1"},{"w":"target_3"}]}}
source_2 target_2 {"source_2":{"method2":[{"w":"target_2"},{"w":"target_4"}]}}
如您所见,json_col 包含一个嵌套的 dict/json,其中第一个键是 source_word 列中的单词source_1 ,然后是方法名称,例如method1, method2, ..., methodn,最后它有一个列表带有<w, target_word> 模式的字典(w 只是表示这是一个单词的文字)。我有兴趣检查json_col 是否包含源词source_1 作为键,然后是method1,关键字target1 在method1 中。如何在 Spark SQL 中执行此操作?
这是我的工作 presto sql:
select
source_word, target_word
from
table
where
contains(cast(json_extract(json_col, concat('$["', source_word, '"]["method1"]')) as array(json)),
json_parse(concat('{"w":"', target_word, '"}')))
这就是我在 Spark SQL 中提出的:
select
source_word, target_word
from
table
where
instr(json_col, concat('{"', source_word, '":{"method1"')) > 0
and instr(json_col, concat('{"w":"', target_word, '"}')) > 0
但后来我意识到这个 sql 中的缺陷,instr 的条件可能都为真,但我正在寻找的 target_word 不是来自 method1。
理想情况下,它应该从方法 1 中获取值,比如 [{"w":"target_1"},{"w":"target_3"}] 并查看 {"w":"target_1"} 是否在其中。
有人能指出我正确的方向吗?谢谢!
【问题讨论】:
-
嗯,我想知道为什么要投反对票?这可能对其他人没有用。
标签: sql pyspark apache-spark-sql presto