pysparksql从嵌套表达式中的json字段中提取值



我有一些数据看起来像:

{ "col1": "val1",
"col2": "val2",
"col3": "{ "a": "A", "b": "B", .... }"
}

与模式:

root:
|-- col1: string (nullable = true)
|-- col2: string (nullable = true)
|-- col3: string (nullable = true)

基于某个条件,我需要选择col3.b

中的值所以我有pyspark sql代码如下:

spark.sql(''' 
select 
col1, 
col2, 
case when col1 like .... then "yo" 
when col1 like .... then json_tuple(col3,'b') else null end as col_3_val 
from data ''').show()

这给了我错误

spark.sql.utils.AnalysisException: Generators are not supported when it's nested in expressions

但是当我运行

spark.sql(''' select json_tuple(col3,'b') as col_3_val from data ''').show()

我得到了预期的输出:

col_3_val
B

是我错过了什么,还是有另一种方法来做到这一点?

不使用json_tuple。使用REGEXP_EXTRACT(col2, my_regex)

从列中提取Json值

最新更新