pig函数以及关键字 的一些实例应用的总结(来自pig笔记)

时间:2023-01-30 23:33:01
http://wenku.baidu.com/link?url=yb7KnpSj9nHxWk_MsEVUezvB24evRf9wR87FX0dTT77pGXNXi6k3o_kTmAkBrpIHTqo66cr8P5cGOdHJKzqFsWFVt6xgvzWs5cxWKfcs6ee
这里面总结的挺全面,尤其最后的一些小例子,很使用。我都写过,不过还是借鉴一下比较好

我总结了一写有用的重点:

pig的计算函数,过滤函数,加载函数和存储函数:
加载/存储函数:PigStorage, BinStorage, BinaryStorage, TextLoader, PigDump
计算函数: AVG, COUNT, CONCAT, COUNTSTAR, DIFF, MAX, MIN, SIZE, SUM, TOKENIZE
过滤函数IsEmpty:sjnd = filter grpd by not IsEmpty(divs);
用户自定义函数(UDF)
public abstract class EvalFunc<T {
public abstract T exec(Tuple input) throws IOException;
public List<FuncSpec getAvgToFuncMapping() throws FrontendException;
public FuncSpec outputSchema() throws FrontendException;
}
输入元组的字段包含传递给函数的表达式,输出是泛型;对于过滤函数输出就是Boolean类型。建议尽量在
getAvgToFuncMapping()/outputSchema()申明输入和输出数据的类型,以便Pig进行类型转换或过滤不匹配类型的错误值。
GruntREGISTER pig-examples.jar;
DEFINE isGood org.hadoopbook.pig.IsGoodQuality();
加载UDF
public LoadFunc {
public void setLocation(String location, Job job);
public InputFormat getInputFormat();
public void prepareToRead(RecordReader reader, PigSplit split);
public Tuple next() throws IOException;
}



foreach中使用$+数字代表某个位置的列。下面语句中gain与gain2的值一样:
prices = load 'NYSE_daily' as (exchange, symbol, date, open, high, low, close,volume, adj_close);
gain = foreach prices generate close - open;
gain2 = foreach prices generate $6 - $3;
在schema定义不确定或不清楚时,位置风格的引用是很有用的。使用“*”代表全部列,还可以使用“..”表示范围内的列,这对简化命令文本很有用:
prices = load 'NYSE_daily' as (exchange, symbol, date, open,high, low, close, volume, adj_close);
beginning = foreach prices generate ..open; -- exchange, symbol, date, open
middle = foreach prices generate open..close; -- open, high, low, close
end = foreach prices generate volume..; -- volume, adj_close




复杂的数据类型,比如元组(touple),包(bag)和映射(map)。这些复杂类型的数据,一般从文件加载或由一些关系操作而得来。
tuple: (1, ‘hello’) //任何类型的字段序列
Map查找 m#k 在映射m中键k对应的值 items’Coat’
map: [‘a’ ‘hello’] //一组键值对,键必须是字符数组
类型转换 (t)f 将字段t转换成f类型 (int)year


包是元组的无序集合,比如{(1,’value’),(‘v1’,v2)}。而关系则是有名字的包,这个名称成为关系的别名。一般情况下,一个Pig语句会产生一个关系。
通过文字直接创建一个关系是非法的,比如:
A = {(1,2)} –非法


将一个关系的字段投影为一个新的关系也是非法的,比如:
B = a.$0 –非法


作为一个变通方案,你可以用以下语句实现该功能:
B = foreach A genereate $0


将来的Pig版本也许会统一包和关系的处理模式,以消除这种不一致。


join


连接一组key:


jnd = join daily by symbol, divs by symbol;


连接两组key:


jnd = join daily by (symbol, date), divs by (symbol, date);


外连接:


jnd = join daily by (symbol, date) left outer, divs by (symbol, date);

jnd = join daily by (symbol, date) right outer, divs by (symbol, date);

jnd = join daily by (symbol, date) full outer, divs by (symbol, date);



多表连接:


A = load 'input1' as (x, y);


B = load 'input2' as (u, v);


C = load 'input3' as (e, f);


alpha = join A by x, B by u, C by e;


自连接(必须准备两份数据):


divs1 = load 'NYSE_dividends' as (exchange:chararray, symbol:int,date:chararray, dividends);


divs2 = load 'NYSE_dividends' as (exchange:chararray, symbol:int,date:chararray, dividends);


jnd = join divs1 by symbol, divs2 by symbol;


increased = filter jnd by divs1::date < divs2::date and


divs1::dividends < divs2::dividends;