阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Pig简单的代码实例:报表统计行业中的点击和曝光量

429次阅读
没有评论

共计 6924 个字符,预计需要花费 18 分钟才能阅读完成。

注意:pig 中用 run 或者 exec 运行脚本。除了 cd 和 ls,其他命令不用。在本代码中用 rm 和 mv 命令做例子,容易出错。

另外,pig 只有在 store 或 dump 时候才会真正加载数据,否则,只是加载代码,不具体操作数据。所以在 rm 操作时必须注意该文件是否已经生成。如果 rm 的文件为生成,可以第三文件,进行 mv 改名操作

SET job.name ‘test_age_reporth_istorical’;– 定义任务名字,在 http://172.XX.XX.XX:50030/jobtracker.jsp 中查看任务状态,失败成功。

SET job.priority HIGH;– 优先级

– 注册 jar 包,用于读取 sequence file 和输出分析结果文件
REGISTER piggybank.jar;
DEFINE SequenceFileLoader org.apache.pig.piggybank.storage.SequenceFileLoader(); – 读取二进制文件,函数名定义

%default Cleaned_Log /user/C/data/XXX/cleaned/$date/*/part* –$date 是外部传入参数

%default AD_Data /user/XXX/data/xxx/metadata/ad/part*
%default Campaign_Data /user/xxx/data/xxx/metadata/campaign/part*
%default Social_Data /user/xxx/data/report/socialdata/part*

– 所有的输出文件路径:
%default Industry_Path $file_path/report/historical/age/$year/industry
%default Industry_SUM $file_path/report/historical/age/$year/industry_sum
%default Industry_TMP $file_path/report/historical/age/$year/industry_tmp

%default Industry_Brand_Path $file_path/report/historical/age/$year/industry_brand
%default Industry_Brand_SUM $file_path/report/historical/age/$year/industry_brand_sum
%default Industry_Brand_TMP $file_path/report/historical/age/$year/industry_brand_tmp

%default ALL_Path $file_path/report/historical/age/$year/all
%default ALL_SUM $file_path/report/historical/age/$year/all_sum
%default ALL_TMP $file_path/report/historical/age/$year/all_tmp

%default output_path /user/xxx/tmp/result

 

origin_cleaned_data = LOAD ‘$Cleaned_Log’ USING PigStorage(‘,’) – 读取日志文件
AS (ad_network_id:chararray,
    xxx_ad_id:chararray,
    guid:chararray,
    id:chararray,
    create_time:chararray,
    action_time:chararray,
    log_type:chararray,
    ad_id:chararray,
    positioning_method:chararray,
    location_accuracy:chararray,
    lat:chararray,
    lon:chararray,
    cell_id:chararray,
    lac:chararray,
    mcc:chararray,
    mnc:chararray,
    ip:chararray,
    connection_type:chararray,
    Android_id:chararray,
    android_advertising_id:chararray,
    openudid:chararray,
    mac_address:chararray,
    uid:chararray,
    density:chararray,
    screen_height:chararray,
    screen_width:chararray,
    user_agent:chararray,
    app_id:chararray,
    app_category_id:chararray,
    device_model_id:chararray,
    carrier_id:chararray,
    os_id:chararray,
    device_type:chararray,
    os_version:chararray,
    country_region_id:chararray,
    province_region_id:chararray,
    city_region_id:chararray,
    ip_lat:chararray,
    ip_lon:chararray,
    quadkey:chararray);

–loading metadata/ad(adId,campaignId)
metadata_ad = LOAD ‘$AD_Data’ USING PigStorage(‘,’) AS (adId:chararray, campaignId:chararray);

–loading metadata/campaign数据(campaignId, industryId, brandId)
metadata_campaign = LOAD ‘$Campaign_Data’ USING PigStorage(‘,’) AS (campaignId:chararray, industryId:chararray, brandId:chararray);

–ad and campaign for inner join
joinAdCampaignByCampaignId = JOIN metadata_ad BY campaignId,metadata_campaign BY campaignId;–(adId,campaignId,campaignId,industryId,brandId)
–filtering out redundant column of joinAdCampaignByCampaignId
joined_ad_campaign_data = FOREACH joinAdCampaignByCampaignId GENERATE $0 AS adId,$3 AS industryId,$4 AS brandId; –(adId,industryId,brandId)

–extract column for analyzing
origin_historical_age = FOREACH origin_cleaned_data GENERATE xxx_ad_id,guid,log_type;–(xxx_ad_id,guid,log_type)
–distinct
distinct_origin_historical_age = DISTINCT origin_historical_age;–(xxx_ad_id,guid,log_type)

–loading metadata_region(guid_social, sex, age, income, edu, hobby)
metadata_social = LOAD ‘$Social_Data’ USING PigStorage(‘,’) AS (guid_social:chararray, sex:chararray, age:chararray, income:chararray, edu:chararray, hobby:chararray);
–extract needed column in metadata_social
social_age = FOREACH metadata_social GENERATE guid_social,age;

–join socialData(metadata_social) and logData(distinct_origin_historical_age):
joinedByGUID = JOIN social_age BY guid_social, distinct_origin_historical_age BY guid;
–(guid_social, age; xxx_ad_id,guid,log_type)

 

–generating analyzing age data
joined_orgin_age_data = FOREACH joinedByGUID GENERATE xxx_ad_id,guid,log_type,age;
joinedByAdId = JOIN joined_ad_campaign_data BY adId, joined_orgin_age_data BY xxx_ad_id; –(adId,industryId,brandId,xxx_ad_id,guid,log_type,age)
–filtering
all_current_data = FOREACH joinedByAdId GENERATE guid,log_type,industryId,brandId,age; –(guid,log_type,industryId,brandId,age)

–for industry analyzing
industry_current_data = FOREACH all_current_data GENERATE industryId,guid,age,log_type;  –(industryId,guid,age,log_type)

–load all in the path “industry”
industry_existed_Data = LOAD ‘$Industry_Path’ USING PigStorage(‘,’) AS (industryId:chararray,guid:chararray,age:chararray,log_type:chararray);

–merge with history data
union_Industry = UNION industry_existed_Data, industry_current_data;
distict_union_industry = DISTINCT union_Industry;
group_industry = GROUP distict_union_industry BY ($2,$0,$3);
count_guid_for_industry = FOREACH group_industry GENERATE FLATTEN(group),COUNT($1.$1);

rm $Industry_SUM;
STORE count_guid_for_industry INTO ‘$Industry_SUM’ USING PigStorage(‘,’);

–storing union industry data(current and history)
STORE distict_union_industry INTO ‘$Industry_TMP’ USING PigStorage(‘,’);
rm $Industry_Path
mv $Industry_TMP $Industry_Path

–counting guid for industry and brand
industry_brand_current = FOREACH all_current_data GENERATE age,industryId,brandId,log_type,guid;
–(age,industryId,brandId,log_type,guid)

–load history data of industry_brand
industry_brand_history = LOAD ‘$Industry_Brand_Path’ USING PigStorage(‘,’) AS(age:chararray, industryId:chararray, brandId:chararray, log_type:chararray, guid:chararray);

–union all data of industry_brand
union_industry_brand = UNION industry_brand_current,industry_brand_history;
unique_industry_brand = DISTINCT union_industry_brand;
–(age,industryId,brandId,log_type,guid)

–counting users’ number for industry and brand
group_industry_brand = GROUP unique_industry_brand BY ($0,$1,$2,$3);
count_guid_for_industry_brand = FOREACH group_industry_brand GENERATE FLATTEN(group),COUNT($1.$4);

rm $Industry_Brand_SUM;
STORE count_guid_for_industry_brand INTO ‘$Industry_Brand_SUM’ USING PigStorage(‘,’);

STORE unique_industry_brand INTO ‘$Industry_Brand_TMP’ USING PigStorage(‘,’);
rm $Industry_Brand_Path;
mv $Industry_Brand_TMP $Industry_Brand_Path

–counting user number for age and logtype
current_data = FOREACH all_current_data GENERATE age,log_type,guid;–(age,log_type,guid)

–load history data of age and logtype
history_data = LOAD ‘$ALL_Path’ USING PigStorage(‘,’) AS(age:chararray,log_type:chararray,guid:chararray);

–union current and history data
union_all_data = UNION history_data, current_data;
unique_all_data = DISTINCT union_all_data;

–count users’ number
group_all_data = GROUP unique_all_data BY ($0,$1);
count_guid_for_age_logtype = FOREACH group_all_data GENERATE FLATTEN(group),COUNT($1.$2);

rm $ALL_SUM;
STORE count_guid_for_age_logtype INTO ‘$ALL_SUM’ USING PigStorage(‘,’);

STORE unique_all_data INTO ‘$ALL_TMP’ USING PigStorage(‘,’);
rm $ALL_Path
mv $ALL_TMP $ALL_Path

Pig 的安装与测试 http://www.linuxidc.com/Linux/2014-07/104039.htm

Pig 安装与配置教程 http://www.linuxidc.com/Linux/2013-04/82785.htm

Pig 安装部署及 MapReduce 模式下测试 http://www.linuxidc.com/Linux/2013-04/82786.htm

Pig 安装及本地模式测试, 体验 http://www.linuxidc.com/Linux/2013-04/82783.htm

Pig 的安装配置与基本使用 http://www.linuxidc.com/Linux/2013-02/79928.htm

Hadoop Pig 进阶语法 http://www.linuxidc.com/Linux/2013-02/79462.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计6924字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7984520
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年 0.99 刀,拿下你的第一个顶级域名,详细注册使用 前言 作为长期折腾云服务、域名建站的老玩家,星哥一直...
浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍 前言 在 AI 自动化快速发展的当下,浏览器早已不再只是...
星哥带你玩飞牛NAS-8:有了NAS你可以干什么?软件汇总篇

星哥带你玩飞牛NAS-8:有了NAS你可以干什么?软件汇总篇

星哥带你玩飞牛 NAS-8:有了 NAS 你可以干什么?软件汇总篇 前言 哈喽各位玩友!我是是星哥,不少朋友私...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...
告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

告别Notion焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁”

  告别 Notion 焦虑!这款全平台开源加密笔记神器,让你的隐私真正“上锁” 引言 在数字笔记工...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Android 的最优解?

  三大开源投屏神器横评:QtScrcpy、scrcpy、escrcpy 谁才是跨平台控制 Andr...
恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击 PHP-FPM(FastCGl Process M...
支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare也瘫了连监控都挂,根因藏在哪?

支付宝、淘宝、闲鱼又双叕崩了,Cloudflare 也瘫了连监控都挂,根因藏在哪? 最近两天的互联网堪称“故障...
星哥带你玩飞牛NAS-13:自动追番、订阅下载 + 刮削,动漫党彻底解放双手!

星哥带你玩飞牛NAS-13:自动追番、订阅下载 + 刮削,动漫党彻底解放双手!

星哥带你玩飞牛 NAS-13:自动追番、订阅下载 + 刮削,动漫党彻底解放双手! 作为动漫爱好者,你是否还在为...
Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集 在云原生体系中,Prometheus 已成为最主流的监控与报警...