当前位置:K88软件开发文章中心大数据Apache Pig → 文章内容

Apache Pig Join运算符

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-26 10:03:42

由 jarodhu 创建,youj 最后一次修改 2016-12-28 JOIN 运算符用于组合来自两个或多个关系的记录。在执行连接操作时,我们从每个关系中声明一个(或一组)元组作为key。 当这些key匹配时,两个特定的元组匹配,否则记录将被丢弃。连接可以是以下类型: Self-joinInner-joinOuter-join ? left join, right join, and full join本章介绍了如何在Pig Latin中使用join运算符的示例。假设在HDFS的 /pig_data/ 目录中有两个文件,即 customers.txt 和 orders.txt ,如下所示。 customers.txt 1,Ramesh,32,Ahmedabad,2000.002,Khilan,25,Delhi,1500.003,kaushik,23,Kota,2000.004,Chaitali,25,Mumbai,6500.00 5,Hardik,27,Bhopal,8500.006,Komal,22,MP,4500.007,Muffy,24,Indore,10000.00 orders.txt 102,2009-10-08 00:00:00,3,3000100,2009-10-08 00:00:00,3,1500101,2009-11-20 00:00:00,2,1560103,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);现在让我们对这两个关系执行各种连接操作。Self-join(自连接)Self-join 用于将表与其自身连接,就像表是两个关系一样,临时重命名至少一个关系。通常,在Apache Pig中,为了执行self-join,我们将在不同的别名(名称)下多次加载相同的数据。那么,将文件 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); 语法下面给出使用 JOIN 运算符执行self-join操作的语法。grunt> Relation3_name = JOIN Relation1_name BY key, Relation2_name BY key ;例通过如图所示加入两个关系 customers1 和 customers2 ,对关系 customers 执行self-join 操作。grunt> customers3 = JOIN customers1 BY id, customers2 BY id;验证使用 DUMP 运算符验证关系 customers3 ,如下所示。grunt> Dump customers3;输出将产生以下输出,显示关系 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使用较为频繁;它也被称为等值连接。当两个表中都存在匹配时,内部连接将返回行。基于连接谓词(join-predicate),通过组合两个关系(例如A和B)的列值来创建新关系。查询将A的每一行与B的每一行进行比较,以查找满足连接谓词的所有行对。当连接谓词被满足时,A和B的每个匹配的行对的列值被组合成结果行。语法以下是使用 JOIN 运算符执行inner join操作的语法。grunt> result = JOIN relation1 BY columnname, relation2 BY columnname;例让我们对customers和orders执行inner join操作,如下所示。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 joinRight outer joinFull outer joinLeft Outer Join(左外连接)left outer join操作返回左表中的所有行,即使右边的关系中没有匹配项。语法下面给出使用 JOIN 运算符执行left outer join操作的语法。grunt> Relation3_name = JOIN Relation1_name BY id LEFT OUTER, Relation2_name BY customer_id;例让我们对customers和orders的两个关系执行left outer join操作,如下所示。grunt> outer_left = JOIN customers BY id LEFT OUTER, orders BY customer_id;验证使用 DUMP 运算符验证关系 outer_left ,如下所示。grunt> Dump outer_left;输出它将产生以下输出,显示关系 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操作将返回右表中的所有行,即使左表中没有匹配项。语法下面给出使用 JOIN 运算符执行right outer join操作的语法。grunt> outer_right = JOIN customers BY id RIGHT, orders BY customer_id;例让我们对customers和orders执行right outer join操作,如下所示。grunt> outer_right = JOIN customers BY id RIGHT, orders BY customer_id;验证使用 DUMP 运算符验证关系 outer_right ,如下所示。grunt> Dump outer_right输出它将产生以下输出,显示关系 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

[1] [2]  下一页


Apache Pig Join运算符