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

查看:32
本文介绍了从 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 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;

我浏览了这些论坛,有些人建议通过编写用户定义的函数来解决这个问题.我可以试试,但我是 Pig 的新手,想更详细地了解它的功能.我在 flatten() 上找到了一些东西,但无法真正获得该输出.有没有办法删除括号和逗号,如图所示?在此先感谢您的帮助!

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!

推荐答案

如果默认使用 DUMP 命令,输出将存储为元组(即所有转储在函数括号内的字段由分隔符分隔',')

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 ',')

您可以使用 FLATTEN 运算符删除第一个括号,使用 STORE 命令删除第二个括号和 ','.

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

试试这个

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

转到文件夹 'output' 并检查文件名以 part* 开头.你会看到这样的输出
130 1
131 1
132 1

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天全站免登陆