- Apache Pig 教程
- Apache Pig - 首頁
- 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 - Describe 運算子
- Apache Pig - Explain 運算子
- Apache Pig - Illustrate 運算子
- 分組 & 連線
- Apache Pig - Group 運算子
- Apache Pig - Cogroup 運算子
- Apache Pig - Join 運算子
- Apache Pig - Cross 運算子
- Pig Latin 內建函式
- Apache Pig - Eval 函式
- 載入 & 儲存函式
- Apache Pig - Bag & Tuple 函式
- 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://: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://:9000/pig_data/employee_details.txt' USING PigStorage(',')
as (id:int, name:chararray, age:int, city:chararray);
現在,讓我們使用鍵 age 對關係 student_details 和 employee_details 的記錄/元組進行分組,如下所示。
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 分組。它包含兩個包 -
第一個包包含來自第一個關係(在本例中為 student_details)中所有年齡為 21 的元組,以及
第二個包包含來自第二個關係(在本例中為 employee_details)中所有年齡為 21 的元組。
如果某個關係沒有年齡值為 21 的元組,則它將返回一個空包。
廣告