Apache Pig 简明教程

Apache Pig - Join Operator

JOIN 运算符用于组合来自两个或更多关系的记录。在执行连接操作时,我们从每个关系中声明一个(或一组)元组作为键。当这些键匹配时,这两个特定的元组将被匹配,否则记录将被丢弃。连接可以是以下类型 −

  1. Self-join

  2. Inner-join

  3. 外连接 - 左连接、右连接和全连接

本章使用示例说明如何在 Pig Latin 中使用连接运算符。假设我们在 HDFS 的 /pig_data/ 目录中有两个文件,名为 customers.txtorders.txt ,如下所示。

customers.txt

1,Ramesh,32,Ahmedabad,2000.00
2,Khilan,25,Delhi,1500.00
3,kaushik,23,Kota,2000.00
4,Chaitali,25,Mumbai,6500.00
5,Hardik,27,Bhopal,8500.00
6,Komal,22,MP,4500.00
7,Muffy,24,Indore,10000.00

orders.txt

102,2009-10-08 00:00:00,3,3000
100,2009-10-08 00:00:00,3,1500
101,2009-11-20 00:00:00,2,1560
103,2008-05-20 00:00:00,4,2060

我们用关系 customersorders 将这两个文件加载到 Pig 中,如下所示。

grunt> customers = LOAD 'hdfs://localhost:9000/pig_data/customers.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int, address:chararray, salary:int);

grunt> orders = LOAD 'hdfs://localhost:9000/pig_data/orders.txt' USING PigStorage(',')
   as (oid:int, date:chararray, customer_id:int, amount:int);

现在让我们对这两个关系执行各种 Join 操作。

Self - join

Self-join 用于像表一样将表与自身连接起来,暂时重新命名至少一个关系。

通常,在 Apache Pig 中,为了执行自连接,我们会使用不同的别名(名称)多次加载相同的数据。因此,让我们像下面所示将文件 customers.txt 的内容加载为两个表。

grunt> customers1 = LOAD 'hdfs://localhost:9000/pig_data/customers.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int, address:chararray, salary:int);

grunt> customers2 = LOAD 'hdfs://localhost:9000/pig_data/customers.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int, address:chararray, salary:int);

Syntax

下面是使用 JOIN 操作符执行 self-join 操作的语法。

grunt> Relation3_name = JOIN Relation1_name BY key, Relation2_name BY key ;

Example

让我们对关系 customers 执行 self-join 操作,方法是像下面所示连接两个关系 customers1customers2

grunt> customers3 = JOIN customers1 BY id, customers2 BY id;

Verification

使用 DUMP 操作符验证关系 customers3 ,如下所示。

grunt> Dump customers3;

Output

它将生成以下输出,显示关系 customers 的内容。

(1,Ramesh,32,Ahmedabad,2000,1,Ramesh,32,Ahmedabad,2000)
(2,Khilan,25,Delhi,1500,2,Khilan,25,Delhi,1500)
(3,kaushik,23,Kota,2000,3,kaushik,23,Kota,2000)
(4,Chaitali,25,Mumbai,6500,4,Chaitali,25,Mumbai,6500)
(5,Hardik,27,Bhopal,8500,5,Hardik,27,Bhopal,8500)
(6,Komal,22,MP,4500,6,Komal,22,MP,4500)
(7,Muffy,24,Indore,10000,7,Muffy,24,Indore,10000)

Inner Join

Inner Join 使用非常频繁;它也称为 equijoin 。当在两个表中匹配时,内连接返回行。

它根据连接谓词通过组合两个关系(比如 A 和 B)的列值创建新关系。该查询将 A 的每一行与 B 的每一行进行比较,以查找满足连接谓词的所有行对。当满足连接谓词时,将 A 和 B 的每一对匹配行的列值组合成结果行。

Syntax

以下是使用 JOIN 操作符执行 inner join 操作的语法。

grunt> result = JOIN relation1 BY columnname, relation2 BY columnname;

Example

让我们对两个关系 customersorders 执行 inner join 操作,如下所示。

grunt> coustomer_orders = JOIN customers BY id, orders BY customer_id;

Verification

使用 DUMP 操作符验证关系 coustomer_orders ,如下所示。

grunt> Dump coustomer_orders;

Output

你将得到以下输出,即名为 coustomer_orders 的关系的内容。

(2,Khilan,25,Delhi,1500,101,2009-11-20 00:00:00,2,1560)
(3,kaushik,23,Kota,2000,100,2009-10-08 00:00:00,3,1500)
(3,kaushik,23,Kota,2000,102,2009-10-08 00:00:00,3,3000)
(4,Chaitali,25,Mumbai,6500,103,2008-05-20 00:00:00,4,2060)

Note

外连接:不同于内连接, outer join 从至少一个关系中返回所有行。外连接操作以三种方式进行 −

  1. Left outer join

  2. Right outer join

  3. Full outer join

Left Outer Join

如果右关系中没有匹配, left outer Join 操作也会返回左表中的所有行。

Syntax

以下是使用 JOIN 操作符执行 left outer join 操作的语法。

grunt> Relation3_name = JOIN Relation1_name BY id LEFT OUTER, Relation2_name BY customer_id;

Example

让我们对关系 customers 和 orders 执行左外连接操作,如下所示。

grunt> outer_left = JOIN customers BY id LEFT OUTER, orders BY customer_id;

Verification

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

grunt> Dump outer_left;

Output

它将产生以下输出,显示关系 outer_left 的内容。

(1,Ramesh,32,Ahmedabad,2000,,,,)
(2,Khilan,25,Delhi,1500,101,2009-11-20 00:00:00,2,1560)
(3,kaushik,23,Kota,2000,100,2009-10-08 00:00:00,3,1500)
(3,kaushik,23,Kota,2000,102,2009-10-08 00:00:00,3,3000)
(4,Chaitali,25,Mumbai,6500,103,2008-05-20 00:00:00,4,2060)
(5,Hardik,27,Bhopal,8500,,,,)
(6,Komal,22,MP,4500,,,,)
(7,Muffy,24,Indore,10000,,,,)

Right Outer Join

right outer join 操作会返回右侧表的全部行,即使在左侧表中没有匹配项也是如此。

Syntax

以下是使用 JOIN 运算符执行 right outer join 操作的语法。

grunt> outer_right = JOIN customers BY id RIGHT, orders BY customer_id;

Example

让我们对两个关系 customersorders 执行 right outer join 操作,如下所示。

grunt> outer_right = JOIN customers BY id RIGHT, orders BY customer_id;

Verification

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

grunt> Dump outer_right

Output

它将产生以下输出,显示关系 outer_right 的内容。

(2,Khilan,25,Delhi,1500,101,2009-11-20 00:00:00,2,1560)
(3,kaushik,23,Kota,2000,100,2009-10-08 00:00:00,3,1500)
(3,kaushik,23,Kota,2000,102,2009-10-08 00:00:00,3,3000)
(4,Chaitali,25,Mumbai,6500,103,2008-05-20 00:00:00,4,2060)

Full Outer Join

full outer join 操作会在某个关系中出现匹配项时返回行。

Syntax

以下是使用 JOIN 运算符执行 full outer join 的语法。

grunt> outer_full = JOIN customers BY id FULL OUTER, orders BY customer_id;

Example

让我们对两个关系 customersorders 执行 full outer join 操作,如下所示。

grunt> outer_full = JOIN customers BY id FULL OUTER, orders BY customer_id;

Verification

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

grun> Dump outer_full;

Output

它将产生以下输出,显示关系 outer_full 的内容。

(1,Ramesh,32,Ahmedabad,2000,,,,)
(2,Khilan,25,Delhi,1500,101,2009-11-20 00:00:00,2,1560)
(3,kaushik,23,Kota,2000,100,2009-10-08 00:00:00,3,1500)
(3,kaushik,23,Kota,2000,102,2009-10-08 00:00:00,3,3000)
(4,Chaitali,25,Mumbai,6500,103,2008-05-20 00:00:00,4,2060)
(5,Hardik,27,Bhopal,8500,,,,)
(6,Komal,22,MP,4500,,,,)
(7,Muffy,24,Indore,10000,,,,)

Using Multiple Keys

我们可以使用多个键执行 JOIN 操作。

Syntax

以下是使用多个键对两张表执行 JOIN 操作的方法。

grunt> Relation3_name = JOIN Relation2_name BY (key1, key2), Relation3_name BY (key1, key2);

假设我们在 HDFS 的 /pig_data/ 目录中拥有两个文件,即 employee.txtemployee_contact.txt ,如下所示。

employee.txt

001,Rajiv,Reddy,21,programmer,003
002,siddarth,Battacharya,22,programmer,003
003,Rajesh,Khanna,22,programmer,003
004,Preethi,Agarwal,21,programmer,003
005,Trupthi,Mohanthy,23,programmer,003
006,Archana,Mishra,23,programmer,003
007,Komal,Nayak,24,teamlead,002
008,Bharathi,Nambiayar,24,manager,001

employee_contact.txt

001,9848022337,Rajiv@gmail.com,Hyderabad,003
002,9848022338,siddarth@gmail.com,Kolkata,003
003,9848022339,Rajesh@gmail.com,Delhi,003
004,9848022330,Preethi@gmail.com,Pune,003
005,9848022336,Trupthi@gmail.com,Bhuwaneshwar,003
006,9848022335,Archana@gmail.com,Chennai,003
007,9848022334,Komal@gmail.com,trivendram,002
008,9848022333,Bharathi@gmail.com,Chennai,001

并且我们已使用关系 employeeemployee_contact 将这两个文件加载到 Pig 中,如下所示。

grunt> employee = LOAD 'hdfs://localhost:9000/pig_data/employee.txt' USING PigStorage(',')
   as (id:int, firstname:chararray, lastname:chararray, age:int, designation:chararray, jobid:int);

grunt> employee_contact = LOAD 'hdfs://localhost:9000/pig_data/employee_contact.txt' USING PigStorage(',')
   as (id:int, phone:chararray, email:chararray, city:chararray, jobid:int);

现在,让我们使用 JOIN 运算符连接这两个关系的内容,如下所示。

grunt> emp = JOIN employee BY (id,jobid), employee_contact BY (id,jobid);

Verification

Verify the relation emp using the DUMP operator as shown below.

grunt> Dump emp;

Output

It will produce the following output, displaying the contents of the relation named emp as shown below.

(1,Rajiv,Reddy,21,programmer,113,1,9848022337,Rajiv@gmail.com,Hyderabad,113)
(2,siddarth,Battacharya,22,programmer,113,2,9848022338,siddarth@gmail.com,Kolka ta,113)
(3,Rajesh,Khanna,22,programmer,113,3,9848022339,Rajesh@gmail.com,Delhi,113)
(4,Preethi,Agarwal,21,programmer,113,4,9848022330,Preethi@gmail.com,Pune,113)
(5,Trupthi,Mohanthy,23,programmer,113,5,9848022336,Trupthi@gmail.com,Bhuwaneshw ar,113)
(6,Archana,Mishra,23,programmer,113,6,9848022335,Archana@gmail.com,Chennai,113)
(7,Komal,Nayak,24,teamlead,112,7,9848022334,Komal@gmail.com,trivendram,112)
(8,Bharathi,Nambiayar,24,manager,111,8,9848022333,Bharathi@gmail.com,Chennai,111)