我有一个HIVE表,其中将Tripid的数据加载为tripid,gps_location_1,gps_location_2位置,一次旅行可能是10 gps位置,而其他旅行可能是500 gps位置,当我查询数据时我的结果是如下
选择Tripid,大小(gps)作为旅行计数;
三元组gps_location_1 | gps_location_2 |计数|
1451f2b3d | 44.1145 | 44.1148 | 9 |
选择Tripid,gps.gps_location_1,gps.gps_location_1作为行程计数;
+ --------- + ------------------------------------ ----------------------- + -------------------------- -------------------------------- +
三重| gps_location_1 | gps_location_2 |计数|
+ --------- + ------------------------------------ ------------------------- + ------------------------ ---------------------------------- + 1451f2b3d | [44.1145,44.1146,44.1147,44.1148,44.1148,44.1129,44.1127,44.1121] | [44.1148,44.1146,44.1146,44.1141,44.1138,44.1129,44.1127] | 9 | + --------- + --------------------------------------- --------------------------- + ---------------------- ------------------------------------ +
我可以从行程数组表中看到第一个值。
从行程中选择三叉戟,gps [0] .gps_location_1,gps [0] .gps_location_1;
三重gps_location_1 gps_location_2
1451f2b3d 44.1145 44.1148
行程数组表的第二行
从行程中选择三叉号,gps [1] .gps_location_1,gps [1] .gps_location_1;
三重gps_location_1 gps_location_2
1451f2b3d 44.1146 44.1146
行程数组表中的最后一行
从行程中选择三元组,gps [size(gps)]。gps_location_1,gps [size(gps)]。gps_location_1;
1451f2b3d 44.1121 44.1127
我需要像这样将每一行存储在新的target_trip表中,遍历行表中一个Tripid的所有行,并将其插入如下所示的target_table中。
我该如何实现?
三重gps_location_1 gps_location_2
1451f2b3d 44.1145 44.1148
1451f2b3d 44.1146 44.1146
1451f2b3d 44.1147 44.1146
1451f2b3d 44.1148 44.1141
1451f2b3d 44.1129 44.1138
1451f2b3d 44.1127 44.1129
1451f2b3d 44.1121 44.1127
答案 0 :(得分:0)
使用lateral view explode
:
elect tripid, coordinates.gps_location_1, coordinates.gps_location_1
from trip
lateral view outer explode(gps) s as coordinates
explode()
UDTF为每个数组元素生成行。侧视图将UDTF应用于基表的每一行,然后将结果行与输入行连接以形成具有指定表别名的虚拟表。
有关横向视图的更多信息,另请参见this答案。