pig Demo

1 篇文章 0 订阅
1 篇文章 0 订阅
demo1 对童谣“Mary 和她的羊羔”进行词频统计。

--从HDFS 中加载文件
records = load '/tmp/dyf/content' as (line);

--TOKENIZE 将line 按单词分割成列
--flatten 接受 TOKENIZE 操作后产生的记录集合,然后分开成独立的列。
words = foreach records generate flatten(TOKENIZE(line)) as word;

--按照 word 进行分组
grpd = group words by word;
--计数
cntd = foreach grpd generate group,COUNT(words);
--打印结果
DUMP cntd;

童谣内容
Mary had a little lamb
its fleece was white as snow
and everywhere that Mary went
the lamb was sure to go

打印结果:
 (a,1)
  (as,1)
  (go,1)
  (to,1)
  (and,1)
  (had,1)
  (its,1)
  (the,1)
  (was,2)
  (Mary,2)
  (lamb,2)
  (snow,1)
  (sure,1)
  (that,1)
  (went,1)
  (white,1)
  (fleece,1)
  (little,1)
  (everywhere,1)

demo2: 统计用户购买总金额,join 邮编。
transactions = load '/tmp/dyf/transactions' as (customer,purchase);
grouped = group transactions by customer;
total = foreach grouped generate group, SUM(transactions.purchase) as total_purchase;

profile = load '/tmp/dyf/customer_profile' as (customer,zipcode);
answer = join total by group,profile by customer;
dump answer;

transactions 数据:
zwj 36
dyf 120
dzh 1000
dyf 66
zwj 88
dzh 99
customer_profile 数据:
dyf 456373
dzh 458000
zwj 458000

打印结果:
(dyf,186.0,dyf,456373)
(dzh,1099.0,dzh,458000)
(zwj,124.0,zwj,458000)

demo3:查找年龄为18~25岁的用户访问最多的5个网站。
users = load '/tmp/dyf/users' as (name,age);
— 过滤18~25 用户
fltrd = filter users by age >= 18 and age <= 25;

pages = load '/tmp/dyf/pages' as(user,url);
— 过滤18~25 用户访问网站
jnd = join fltrd by name,pages by user;
grpd = group jnd by url;
smmd = foreach grpd generate group,COUNT(jnd) as clicks;
srtd = order smmd by clicks desc;
top5 = limit srtd 5;
dump top5;

users 数据:
dyf 24
zwj 22
dzh 2
pages 数据:
dyf baidu
dyf google
dyf xueqiu
zwj baobao
zwj baidu
zwj google
dzh baobao
dzh taobao
打印结果:
(baidu,2)
(google,2)
(baobao,1)
(xueqiu,1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值