2016-08-25 28 views
0

由於配置單元不支持樞軸,我需要一個更好的方法來配置下面的配置單元。如何在Hive中轉置

enter image description here

,我需要轉換爲下面的結果。

enter image description here

+0

請分享樣本輸出您的意思是什麼_col1,_col2 –

回答

0

你可以試試這個

Select ID, Yes from (
SELECT ID, 'Col1' as 'Yes' where Col1 = 'yes' 
UNION ALL 
SELECT ID, 'Col2' as 'Yes' where Col2 = 'yes' 
UNION ALL 
SELECT ID, 'Col3' as 'Yes' where Col3 = 'yes' 
UNION ALL 
SELECT ID, 'Col4' as 'Yes' where Col4 = 'yes' 
UNION ALL 
SELECT ID, NULL as 'Yes' where Col1 != 'yes' and Col2 != 'yes' and Col3 != 'yes' and Col4 != 'yes' 
) TempTable ORDER BY ID 
+0

如果您關心效率,此解決方案絕對是一場災難。 – gobrewers14

2

可以使用conditional_emit UDF從brickhouse(http://github.com/klout/brickhouse)來做到這一點。

SELECT ID, ce.feature as yes 
FROM 
table 
LATERAL VIEW conditional_emit( 
     ARRAY(col1 = 'yes', 
       col2 = 'yes', 
       col3 = 'yes', 
       col4 = 'yes', 
       col1 != 'yes' and col2 != 'yes' and col3 != 'yes' and col4 != 'yes'), 
     ARRAY('col1', 'col2', col3', 'col4', ' ')) c as ce; 

conditional_emit是將發出對作爲真正的數組元素與從傳入的第二陣列的相應字符串(在通過了第一陣列中)的記錄的UDTF。請注意,這使得只有一個通過數據,而UNION會進行多次傳遞。