SELECT mac, get_json_object(appListInfo, '$[].pkgname') AS pkgname, get_json_object(appListInfo, '$[].vercode') AS vercode FROM( SELECT DISTINCT upper(distinct_id) AS mac, lower(properties['appListInfo']) AS appListInfo FROM default.ods_slog WHERE DAY = '20230720' AND event = 'appListInfo' AND distinct_id NOT LIKE '00%' AND distinct_id NOT IN ('',' ','null','NULL') AND upper(distinct_id) NOT IN ('FFFFFFFFFFFF','HHHHHHHHHHHH') ) AS t; 其中vercode示例:[402005,70600000,20100003,1012,10400011,1100088,40300004,501092115,10208,1030026,1302,2070004,10011,15,109037,3074,805000036,805000036,805000036,10000900,7900095],pkgname也类似,如何将这两者拆分多行?
时间: 2024-01-12 13:03:10 浏览: 160
要将pkgname和vercode字段拆分成多行,您可以使用LATERAL VIEW EXPLODE函数。但是,由于您的查询中使用了通配符`$[]`,表示获取所有的pkgname和vercode,而不是按照索引位置获取。因此,我们需要先将JSON数组解析为Hive的内置数组类型(array)。
以下是修改后的查询语句:
```sql
SELECT mac, pkgname, vercode
FROM (
SELECT DISTINCT upper(distinct_id) AS mac,
split(get_json_object(appListInfo, '$[].pkgname'), ',') AS pkgnames,
split(get_json_object(appListInfo, '$[].vercode'), ',') AS vercodes
FROM (
SELECT DISTINCT upper(distinct_id) AS mac,
lower(properties['appListInfo']) AS appListInfo
FROM default.ods_slog
WHERE DAY = '20230720'
AND event = 'appListInfo'
AND distinct_id NOT LIKE '00%'
AND distinct_id NOT IN ('', ' ', 'null', 'NULL')
AND upper(distinct_id) NOT IN ('FFFFFFFFFFFF', 'HHHHHHHHHHHH')
) AS t
) AS t2
LATERAL VIEW EXPLODE(pkgnames) t3 AS pkgname
LATERAL VIEW EXPLODE(vercodes) t4 AS vercode;
```
这样,pkgname和vercode将会按照逗号分隔符拆分成多行,并与mac字段一起显示。请注意,我们首先使用split函数将pkgname和vercode解析为Hive的内置数组类型(array),然后再使用LATERAL VIEW EXPLODE函数拆分成多行。
阅读全文