如何使用Pig Latin进行基本的数学运算?
我进入了Grunt shell并尝试了例如dump 1+2;
,但得到了
2014-11-28 23:23:48,011 [main] ERROR org.apache.pig.tools.grunt.Grunt - 错误2997:遇到IOException。找不到先前定义的别名。请定义一个别名并使用'dump'操作符。
更新
做A = 1 + 2 DUMP A
我得到:
grunt> A = 1 + 2 DUMP A
>> [ENTER]
>> [ENTER]
...
答案 0 :(得分:2)
你不能像使用python shell那样使用pig grunt shell。 Pig是数据流语言,它将关系作为输入,并产生另一种关系作为输出。 猪粪通常以这种形式组织
1.LOAD stmt to load data from local file system or HDFS file system
2.A series of stmts to process the data using pig built-in function or Custom UDF
3.DUMP stmt to display the data in console, it will take only relation as input(No constant values as you mentioned)
4.STORE stmt to store the output into local file system or HDFS file system
示例:强>
的 input.txt中强>
1,5,10.0
2,6,20.0
3,7,30.0
使用分隔符从本地文件系统加载文件','
A = LOAD 'input.txt' USING PigStorage(',') AS (f1:int,f2:int,f3:float);
处理数据,添加,差异和div
B = FOREACH A GENERATE f1+10 AS sum, f2-2 AS diff, f3/10 AS div;
在控制台中显示结果
DUMP B;
您可以从grunt shell或通过猪脚本
执行一系列猪stmtsGrunt shell:
grunt> A = LOAD 'input.txt' USING PigStorage(',') AS (f1:int,f2:int,f3:float);
grunt> B = FOREACH A GENERATE f1+10 AS sum, f2-2 AS diff, f3/10 AS div;
grunt> DUMP B;
(11,2,1.0)
(12,3,2.0)
(13,4,3.0)
猪脚本:
1.Add the above three lines of pig stmt into a file Ex: test.pig
2.Run from terminal
pig -x local test.pig(本地模式)
pig test.pig(或)pig -x mapreduce test.pig(mapreduce mode)
请参考基本的猪文档,它将为您提供更多帮助 https://pig.apache.org/docs/r0.13.0/basic.html#artichmetic-ops
答案 1 :(得分:0)
https://pig.apache.org/docs/r0.7.0/piglatin_ref2.html#Arithmetic+Operators
这给出了具有PIG的算术运算符的例子。
DUMP用于显示结果数据数组。