如果元素包含hive/SparkSQL中指定的字符串,则过滤掉数组元素

问题描述 投票:0回答:1

我有如下所示的示例数据,我想提取仅具有名称及其完整键而不是 dep_names 及其完整键的元素。

我正在尝试如下所示,

WITH dataset AS 
(
   SELECT '{"name": "Bob Smith",
             "org": "engineering",
             "projects": [{"dep_namename":"project1", "completed":none},{"name":"project2", "completed":false},{"name":"project3", "completed":true}]}'
     AS myblob
)
SELECT get_json_object(myblob, '$.projects[*]?(exists(@.name))'  AS project_name
FROM dataset

我期待如下的输出

[false,true]
sql arrays json apache-spark-sql hive
1个回答
0
投票

请在下面找到

Spark SQL

SELECT 
    FILTER(
        FROM_JSON(
            input,
            'projects array<struct<dep_namename:string, completed:string, name:string>>'
        ).projects, 
        mp -> mp.name IS NOT NULL
    ).completed as output 
FROM VALUES 
 ('{"name": "Bob Smith","org": "engineering","projects": [{"dep_namename":"project1", "completed":"none"},{"name":"project2", "completed":false},{"name":"project3", "completed":true}]}') 
AS (input)

+-------------+
|output       |
+-------------+
|[false, true]|
+-------------+
© www.soinside.com 2019 - 2024. All rights reserved.