如何在猪/蜂巢中相应的几列中进行转置 [英] how to do Transpose in corresponding few columns in pig/hive

查看:31
本文介绍了如何在猪/蜂巢中相应的几列中进行转置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 pig/hive 中对相应的几列进行换位.
在处理数据时,我得到了低于要求

I was wondering is it possible to do transposition corresponding few columns in pig/hive.
as dealing with data i got below requirement

id      jan     feb     march
1       j1      f1      m1
2       j2      f2      m2
3       j3      f3      m3

我需要将它与第一列转置的地方,所以它看起来像 -

where i need to transpose it against first column, so it would look like -

id      value   month
1       j1      jan
1       f1      feb
1       m1      march
2       j2      jan
2       f2      feb
2       m2      march
3       j3      jan
3       f3      feb
3       m3      march

我已经用 java 尝试过这个,但是要让它进入分布式模式,有没有办法在 pig/hive 中做到这一点.
提前感谢您的帮助!!

I have tried this with java, but to get it into distributed mode is there any way to do it in pig/hive.
appreciating your help in advance!!

推荐答案

Pig 没有任何内置函数来解决您的需求,但是您可以尝试以下方法,我想它对您有用.

Pig doesn't have any built-in function to solve your requirement, but you can try the below approach, i guess it will work for you.

input.txt

1       j1      f1      m1
2       j2      f2      m2
3       j3      f3      m3

PigScript:

A = LOAD 'input.txt' USING PigStorage() AS (id,month1,month2,month3);
B = FOREACH A GENERATE FLATTEN(TOBAG(TOTUPLE(id,month1,'jan'),TOTUPLE(id,month2,'feb'),TOTUPLE(id,month3,'mar')));
DUMP B;

输出:

(1,j1,jan)
(1,f1,feb)
(1,m1,mar)
(2,j2,jan)
(2,f2,feb)
(2,m2,mar)
(3,j3,jan)
(3,f3,feb)
(3,m3,mar)

这篇关于如何在猪/蜂巢中相应的几列中进行转置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆