我有一个带有以下模式的hive表:
COOKIE | PRODUCT_ID | CAT_ID | QTY
1234123 [1,2,3] [r,t,null] [2,1,null]如何使数组规范化,从而得到以下结果
COOKIE | PRODUCT_ID | CAT_ID | QTY
1234123 [1] [r] [2]
1234123 [2] [t] [1]
1234123 [3] null null 我尝试了以下几点:
select concat_ws('|',visid_high,visid_low) as cookie
,pid
,catid
,qty
from table
lateral view explode(productid) ptable as pid
lateral view explode(catalogId) ptable2 as catid
lateral view explode(qty) ptable3 as qty然而,结果却是笛卡尔的产物。
发布于 2013-12-18 21:35:17
您可以使用来自Brickhouse ( numeric_range )的array_index和http://github.com/klout/brickhouse来解决这个问题。在范围/范围上有一篇信息丰富的博客文章详细描述了
使用这些UDF,查询将类似于
select cookie,
array_index( product_id_arr, n ) as product_id,
array_index( catalog_id_arr, n ) as catalog_id,
array_index( qty_id_arr, n ) as qty
from table
lateral view numeric_range( size( product_id_arr )) n1 as n;发布于 2018-05-22 11:28:58
您可以使用position来实现这一点,它将提供一个介于0和n之间的整数,以指示数组中每个元素在数组中的位置。然后使用这个整数调用pos (表示位置),使用块表示法获取其他数组中的匹配值,如下所示:
select
cookie,
n.pos as position,
n.prd_id as product_id,
cat_id[pos] as catalog_id,
qty[pos] as qty
from table
lateral view posexplode(product_id_arr) n as pos, prd_id;这既避免了使用导入的UDF,也避免了将各种数组连接在一起(这具有更好的性能)。
发布于 2020-06-25 13:00:30
如果您在火星之火中使用Spark2.4,请使用arrays_zip和posexplode
df = (df
.withColumn('zipped', arrays_zip('col1', 'col2'))
.select('id', posexplode('zipped')))https://stackoverflow.com/questions/20667473
复制相似问题