使用PIG加入后过滤数据

时间:2017-04-25 13:29:31

标签: hadoop apache-pig bigdata

我想在两个文件加入后过滤记录。

文件BX-Books.csv包含图书数据。文件BX-Book-Ratings.csv包含书籍评级数据,其中 ISBN 是两个文件的公共列。使用此列完成文件之间的内连接。
我想获得2002年出版的书籍。

我使用了以下脚本,但我得到了0条记录。

grunt> BookXRecords = LOAD '/user/pradeep/BX-Books.csv'  USING PigStorage(';') AS (ISBN:chararray,BookTitle:chararray,BookAuthor:chararray,YearOfPublication:chararray, Publisher:chararray,ImageURLS:chararray,ImageURLM:chararray,ImageURLL:chararray);
grunt> BookXRating = LOAD '/user/pradeep/BX-Book-Ratings.csv'  USING PigStorage(';') AS (user:chararray,ISBN:chararray,rating:chararray);
grunt> BxJoin = JOIN BookXRecords BY ISBN, BookXRating BY ISBN;
grunt> BxJoin_Mod = FOREACH BxJoin GENERATE $0 AS ISBN, $1, $2, $3, $4;
grunt> FLTRBx2002 = FILTER BxJoin_Mod BY $3 == '2002';

2 个答案:

答案 0 :(得分:0)

我创建了一个test.csv和test-rating.csv以及一个可以使用它们的Pig脚本。它工作得非常好。

<强> test.csv

1;abc;author1;2002
2;xyz;author2;2003

<强>测试rating.csv

user1;1;3
user2;2;5

猪脚本:

A = LOAD 'test.csv' USING PigStorage(';') AS (ISBN:chararray,BookTitle:chararray,BookAuthor:chararray,YearOfPublication:chararray);
describe A;
dump A;

B = LOAD 'test-rating.csv' USING PigStorage(';') AS (user:chararray,ISBN:chararray,rating:chararray);
describe B;
dump B;

C = JOIN A BY ISBN, B BY ISBN;
describe C;
dump C;

D = FOREACH C GENERATE $0 as ISBN,$1,$2,$3;
describe D;
dump D;

E = FILTER D BY $3 == '2002';
describe E;
dump E;

<强>输出:

A: {ISBN: chararray,BookTitle: chararray,BookAuthor: chararray,YearOfPublication: chararray}
(1,abc,author1,2002)
(2,xyz,author2,2003)
B: {user: chararray,ISBN: chararray,rating: chararray}
(user1,1,3)
(user2,2,5)
C: {A::ISBN: chararray,A::BookTitle: chararray,A::BookAuthor: chararray,A::YearOfPublication: chararray,B::user: chararray,B::ISBN: chararray,B::rating: chararray}
(1,abc,author1,2002,user1,1,3)
(2,xyz,author2,2003,user2,2,5)
D: {ISBN: chararray,A::BookTitle: chararray,A::BookAuthor: chararray,A::YearOfPublication: chararray}
(1,abc,author1,2002)
(2,xyz,author2,2003)
E: {ISBN: chararray,A::BookTitle: chararray,A::BookAuthor: chararray,A::YearOfPublication: chararray}
(1,abc,author1,2002)

答案 1 :(得分:0)

要求:获取2002年出版的书籍。

不需要有2个数据集。 只有使用“BookXRecords”,才能实现这一目标。

grunt>BookXRecords = LOAD '/user/pradeep/BX-Books.csv'  USING PigStorage(';') AS (ISBN:chararray,BookTitle:chararray,BookAuthor:chararray,YearOfPublication:chararray, Publisher:chararray,ImageURLS:chararray,ImageURLM:chararray,ImageURLL:chararray);
grunt>A=FILTER BookXRecords BY year ='2002';
grunt>dump A;