Pig JOIN 运算符
-
JOIN 运算符
在JOIN操作符是用来记录从两个或两个以上的关系结合起来。在执行联接操作时,我们将每个关系中的一个(或一组)元组声明为键。当这些键匹配时,两个特定的元组将匹配,否则记录将被删除。联接可以是以下类型-- Self-join
- Inner-join
- Outer-join − left join, right join, 和 full join
本章举例说明如何在Pig Latin中使用join运算符。假设我们在HDFS的/pig_data/目录中有两个文件,即customers.txt和orders.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
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
我们已将这两个文件与customers和orders的关系加载到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中,要执行 Self-join,我们将使用不同的别名(名称)多次加载相同的数据。因此,让我们将文件customer.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);
句法下面给出的是使用JOIN运算符执行自联接操作的语法。grunt> Relation3_name = JOIN Relation1_name BY key, Relation2_name BY key ;
例子让我们通过关联两个客户customers1和customers2来对关系客户执行自联接操作,如下所示。grunt> customers3 = JOIN customers1 BY id, customers2 BY id;
验证如下所示,使用DUMP运算符验证关系customers3。grunt> Dump customers3;
输出它将产生以下输出,显示关系客户的内容。(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。当两个表中都匹配时,Inner-join将返回行。它通过基于Join谓词组合两个关系(例如A和B)的列值来创建新关系。该查询将A的每一行与B的每一行进行比较,以找到满足Join谓词的所有行对。当满足连接谓词时,A和B的每个匹配行对的列值将合并为结果行。句法这是使用JOIN运算符执行内部联接操作的语法。grunt> result = JOIN relation1 BY columnname, relation2 BY columnname;
例子让我们对两个关系客户和订单执行内部联接操作,如下所示。grunt> coustomer_orders = JOIN customers BY id, orders BY customer_id;
验证如下所示,使用DUMP运算符验证关系coustomer_orders。grunt> Dump coustomer_orders;
输出您将获得以下输出,该输出将包含名为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)
注意-Outer Join:与Inner Join 不同,Outer Join 返回至少一个关系中的所有行。Outer Join 操作以三种方式执行-
- Left outer join
- Right outer join
- Full outer join
-
Left outer join
在LEFT OUTER JOIN操作返回左表中所有行,即使是在右表中的关系不匹配。句法下面给出的是使用JOIN运算符执行LEFT OUTER JOIN操作的语法。grunt> Relation3_name = JOIN Relation1_name BY id LEFT OUTER, Relation2_name BY customer_id;
例让我们对两个关系客户和订单执行左外部联接操作,如下所示。grunt> outer_left = JOIN customers BY id LEFT OUTER, orders BY customer_id;
验证如下所示,使用DUMP运算符验证关系external_left。grunt> Dump outer_left;
输出量它将产生以下输出,显示关系external_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返回的所有行右表中,即使有左表中的不匹配。句法下面给出的是使用JOIN运算符执行右外部联接操作的语法。grunt> outer_right = JOIN customers BY id RIGHT, orders BY customer_id;
例让我们对两个客户和订单执行正确的外部联接操作,如下所示。grunt> outer_right = JOIN customers BY id RIGHT, orders BY customer_id;
验证如下所示,使用DUMP运算符验证关系external_right。grunt> Dump outer_right
输出它将产生以下输出,显示关系external_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操作将返回行。句法下面给出的是使用JOIN运算符执行完全外部联接的语法。grunt> outer_full = JOIN customers BY id FULL OUTER, orders BY customer_id;
例让我们对两个关系客户和订单执行完全外部联接操作,如下所示。grunt> outer_full = JOIN customers BY id FULL OUTER, orders BY customer_id;
验证如下所示,使用DUMP运算符验证关系external_full。grun> Dump outer_full;
输出它将产生以下输出,显示关系external_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,,,,)
-
使用多个键
我们可以使用多个键执行JOIN操作。句法这是您可以使用多个键在两个表上执行JOIN操作的方法。grunt> Relation3_name = JOIN Relation2_name BY (key1, key2), Relation3_name BY (key1, key2);
假设我们在HDFS的/pig_data/目录中有两个文件,即employee.txt和employee_contact.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
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
并且我们已将这两个文件通过关系employee和employee_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);
验证如下所示,使用DUMP运算符验证关系emp。grunt> Dump emp;
输出它将产生以下输出,显示名为emp的关系的内容,如下所示。(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)