从Pig中删除括号和逗号 [英] Remove brackets and commas in output from Pig

查看:110
本文介绍了从Pig中删除括号和逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ((130,1))
((131,1) ))
((132,1))
((133,1))
((137,1))
((138,2))
((139,1))
((140,1))
((142,2))
((143,1))


我想拥有它:

  130 1 
131 1
132 1

我的代码如下:

  A = LOAD'user-links-small.txt'AS(user_a:int,user_b:int); 
B = ORDER BY BY user_a;
分组= COGROUP B BY user_a;
C = FOREACH分组生成计数(B);
D = COGROUP C BY $ 0;
E = FOREACH D GENERATE($ 0,COUNT($ 1));
DUMP E;

我正在浏览这些论坛,有人建议,定义的功能。我可以尝试一下,但我对猪很新,希望能够更详细地了解它的功能。我在flatten()上找到了一些东西,但不能真正得到那个输出。有没有办法去除所示的括号和逗号?如果您在默认情况下使用 DUMP 命令,那么输出将会是 DUMP 命令。

以元组形式存储(也就是所有字段在函数括号内被分隔符分隔','



您可以使用 FLATTEN 运算符,第二个括号和','使用 STORE 命令。 试试这个

  E = FOREACH D GENERATE FLATTEN(($ 0,COUNT($ 1))); 
使用PigStorage('');

转到'输出'文件夹并检查文件名以部分*。你会看到像这样的输出

130 1

131 1

132 1


Currently my output is as below:

((130,1))
((131,1))
((132,1))
((133,1))
((137,1))
((138,2))
((139,1))
((140,1))
((142,2))
((143,1))

I want to have it like:

130 1
131 1
132 1

My code is given below:

A = LOAD 'user-links-small.txt' AS (user_a: int, user_b: int);
B = ORDER A BY user_a;
grouped = COGROUP B BY user_a;
C = FOREACH grouped GENERATE COUNT(B);
D = COGROUP C BY $0;
E = FOREACH D GENERATE($0, COUNT($1));
DUMP E;

I was looking through these forums, and some suggested that the way to this was by coding a user-defined function. I can try that, but I am new to Pig and want to learn its functions a bit more in details. I found something on flatten() but can't really get that output. Is there a way to remove the brackets and commas as shown? Thanks in advance for any help!

解决方案

If you use DUMP command by default the output will be stored as tuples (ie all the fields dumped inside function bracket separated by delimiter ',')

You can remove the first bracket using FLATTEN operator and second bracket and ',' using STORE command.

Try this

E = FOREACH D GENERATE FLATTEN(($0, COUNT($1)));
STORE E INTO 'output' USING PigStorage(' ');

Go to the folder 'output' and check the file name starts with part*. you will see the output like this
130 1
131 1
132 1

这篇关于从Pig中删除括号和逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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