如何使用配置单元将 table 中的列值转换为行值

How do i convert column values in a table to row values using hive

例如: 当前 table

employee_id employee_name   location_1  location_2  location_3
111 Reeta   Delhi
112 Pinky   Chennai Kerala  Null
113 Tinku   Noida   Ranchi  Jaipur
114 Teena   Null
115 Bing    Assam   Assam

想要table:

employee_id employee_name   Location
111 Delhi
112 Pinky   Chennai
112 Pinky   Kerala
113 Tinku   Noida
113 Tinku   Ranchi
113 Tinku   Jaipur
115 Bing    Assam

1.The 在将目标中的列转换为行时应忽略位置列上的空值 table 2. 不应将 employee_id 和具有空值作为位置的名称带到目标 table。 3.When emplyee_id 和 employee_name 作为重复值,只有一个应该被带到目标 table

可能最简单的方法是union all。但这需要多次扫描 table。所以,改为:

select tn.*
from (select t.employee_id, t.employee_name,
             (case when n.n = 1 then location_1
                   when n.n = 2 then location_2
                   when n.n = 3 then location_3
              end) as location 
      from t cross join
           (select 1 as n union all select 2 union all select 3) n
     ) tn
where location is not null;

一个简单的选项使用 union all:

select employee_id, employee_name, location_1 location from mytable where location_1 is not null
union all 
select employee_id, employee_name, location_2 from mytable where location_2 is not null
union all 
select employee_id, employee_name, location_3 from mytable where location_3 is not null

使用映射和 lateral view explode 的特定于配置单元的方法可能更有效:

select employee_id, employee_name, location
from (
    select 
        employee_id, 
        employee_name, 
        map("location_1", location_1, "location_2", location_2, "location_3", location_3) as mp
    from mytable
) t
lateral view explode(mp) m as locname, location  
where location is not null