Manipulating a data structure in Pig/Hive

时间:2015-07-31 20:51:15

标签: sql data-structures hive apache-pig

I'm not really sure how to phrase this question, so please redirect me if there is a better place for this question.

Right now I have a data structure, more or less organized like this:

enter image description here

I want my data to look like this:

enter image description here

Sorry for the images, apparently I can't use markdown to make these!

I realize my question is similar to this one, but ideally I would like to be able to do this in Pig, but knowing how to do it in Hive, R, Python, or Excel/LibreCalc would be useful/interesting too.

I'm not even sure what this kind of data manipulation is called, so directing me to some sort of general wiki page would be helpful.

2 个答案:

答案 0 :(得分:1)

我不确定这是否适用于Hive。我知道它与SQL非常相似。试一试。

select item, year,
'Jan' as Month,
Jan as value
from yourtable
UNION
select item, year,
'Feb' as Month,
Feb as value
from yourtable
UNION
select item, year,
'Mar' as Month,
Mar as value
from yourtable    

答案 1 :(得分:0)

@vkp让我开始朝着正确的方向前进,但我不得不添加一些调整来让它在Hive上运行:

CREATE TABLE myDatabase.newTable STORED AS TEXTFILE AS 
SELECT item, year, 'jan' AS Month, jan AS Value FROM myDatabase.myTable UNION ALL
SELECT item, year, 'feb' AS Month, feb AS Value FROM myDatabase.myTable UNION ALL
SELECT item, year, 'mar' AS Month, mar AS Value FROM myDatabase.myTable;

仍对猪的解决方案感兴趣。