×

Apache Pig 介绍

Apache Pig 概述Apache Pig 架构

Apache Pig 环境

Apache Pig 安装Apache Pig 执行Apache Pig Grunt Shell

Pig Latin 介绍

Pig Latin 基础

Apache Pig 加载和存储

Apache Pig 加载数据Apache Pig 存储数据

Apache Pig 诊断运算符

Apache Pig Diagnostic运算符Apache Pig Describe运算符Apache Pig Explain运算符Apache Pig illustrate运算符

Apache Pig 分组和连接

Apache Pig Group运算符Apache Pig Cogroup运算符Apache Pig Join运算符Apache Pig Cross运算符

Apache Pig 合并和拆分

Apache Pig Union运算符Apache Pig Split运算符

Apache Pig 过滤

Apache Pig Filter运算符Apache Pig Distinct运算符Apache Pig Foreach运算符

Apache Pig 排序

Apache Pig Order By运算符Apache Pig Limit运算符

Pig Latin 内置函数

Apache Pig Eval函数Apache Pig 加载和存储函数Apache Pig 包和元组函数Apache Pig 字符串函数Apache Pig 日期时间函数Apache Pig 数学函数

Apache Pig 其他执行模式

Apache Pig 用户定义函数Apache Pig 运行脚本

Apache Pig 有用的资源

Apache Pig 有用资源Apache Pig 讨论

Apache Pig Cogroup运算符


COGROUP 运算符的运作方式与 GROUP 运算符相同。两个运算符之间的唯一区别是 group 运算符通常用于一个关系,而 cogroup 运算符用于涉及两个或多个关系的语句。

使用Cogroup分组两个关系

假设在HDFS目录 /pig_data/ 中有两个文件,即 student_details.txt employee_details.txt ,如下所示。

student_details.txt

001,Rajiv,Reddy,21,9848022337,Hyderabad
002,siddarth,Battacharya,22,9848022338,Kolkata
003,Rajesh,Khanna,22,9848022339,Delhi
004,Preethi,Agarwal,21,9848022330,Pune
005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar
006,Archana,Mishra,23,9848022335,Chennai
007,Komal,Nayak,24,9848022334,trivendram
008,Bharathi,Nambiayar,24,9848022333,Chennai

employee_details.txt

001,Robin,22,newyork 
002,BOB,23,Kolkata 
003,Maya,23,Tokyo 
004,Sara,25,London 
005,David,23,Bhuwaneshwar 
006,Maggy,22,Chennai

将这些文件分别加载到Pig中,关系名称分别为 student_details employee_details ,如下所示。

grunt> student_details = LOAD 'hdfs://localhost:9000/pig_data/student_details.txt' USING PigStorage(',')
   as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray); 
  
grunt> employee_details = LOAD 'hdfs://localhost:9000/pig_data/employee_details.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int, city:chararray);

现在,将 student_details employee_details 关系的记录/元组按关键字age进行分组,如下所示。

grunt> cogroup_data = COGROUP student_details by age, employee_details by age;

验证

使用 DUMP 运算符验证关系 cogroup_data ,如下所示。

grunt> Dump cogroup_data;

输出

它将产生以下输出,显示名为 cogroup_data 的关系的内容,如下所示。

(21,{(4,Preethi,Agarwal,21,9848022330,Pune), (1,Rajiv,Reddy,21,9848022337,Hyderabad)}, 
   {    })  
(22,{ (3,Rajesh,Khanna,22,9848022339,Delhi), (2,siddarth,Battacharya,22,9848022338,Kolkata) },  
   { (6,Maggy,22,Chennai),(1,Robin,22,newyork) })  
(23,{(6,Archana,Mishra,23,9848022335,Chennai),(5,Trupthi,Mohanthy,23,9848022336 ,Bhuwaneshwar)}, 
   {(5,David,23,Bhuwaneshwar),(3,Maya,23,Tokyo),(2,BOB,23,Kolkata)}) 
(24,{(8,Bharathi,Nambiayar,24,9848022333,Chennai),(7,Komal,Nayak,24,9848022334, trivendram)}, 
   { })  
(25,{   }, 
   {(4,Sara,25,London)})

cogroup 运算符根据年龄对来自每个关系的元组进行分组,其中每个组描述特定的年龄值。

例如,如果我们考虑结果的第一个元组,它按照年龄21分组,那它包含两个包

  • 第一个包保存了具有21岁的第一关系(在这种情况下是 student_details )的所有元组;

  • 第二个包具有第二关系(在这种情况下为 employee_details )的所有元组,其年龄为21岁。

如果关系不具有年龄值为21的元组,则返回一个空包。


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)