×

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 IsEmpty()函数


Apache Pig Eval函数Apache Pig Eval函数


Pig Latin的 IsEmpty() 函数用于检查包或映射是否为空。

语法

下面给出了 IsEmpty() 函数的语法。

grunt> IsEmpty(expression)

假设在HDFS目录 /pig_data/ 中有两个文件,即 emp_sales.txt emp_bonus.txt ,如下所示。emp_sales.txt 包含销售部门员工的详细信息, emp_bonus.txt 包含获得奖金的员工详细信息。

emp_sales.txt

1,Robin,22,25000,sales 
2,BOB,23,30000,sales 
3,Maya,23,25000,sales 
4,Sara,25,40000,sales 
5,David,23,45000,sales 
6,Maggy,22,35000,sales

emp_bonus.txt

1,Robin,22,25000,sales 
2,Jaya,23,20000,admin 
3,Maya,23,25000,sales 
4,Alia,25,50000,admin 
5,David,23,45000,sales 
6,Omar,30,30000,admin

分别使用关系 emp_sales  emp_bonus 将这些文件加载到Pig中,如下所示。

grunt> emp_sales = LOAD 'hdfs://localhost:9000/pig_data/emp_sales.txt' USING PigStorage(',')
   as (sno:int, name:chararray, age:int, salary:int, dept:chararray);
	
grunt> emp_bonus = LOAD 'hdfs://localhost:9000/pig_data/emp_bonus.txt' USING PigStorage(',')
   as (sno:int, name:chararray, age:int, salary:int, dept:chararray);

现在让我们使用 cogroup 来分组 emp_sales emp_bonus 关系的记录/ 运算符,如下所示。

grunt> cogroup_data = COGROUP emp_sales by age, emp_bonus by age;

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

grunt> Dump cogroup_data;
  
(22,{(6,Maggy,22,35000,sales),(1,Robin,22,25000,sales)}, {(1,Robin,22,25000,sales)}) 
(23,{(5,David,23,45000,sales),(3,Maya,23,25000,sales),(2,BOB,23,30000,sales)}, 
   {(5,David,23,45000,sales),(3,Maya,23,25000,sales),(2,Jaya,23,20000,admin)})  
(25,{(4,Sara,25,40000,sales)},{(4,Alia,25,50000,admin)}) 
(30,{},{(6,Omar,30,30000,admin)})

COGROUP操作符根据年龄从每个关系中分组元组,每个组描述特定的年龄值。例如,如果我们考虑结果的第一个元组,则按照年龄22分组。包含两个包,第一个包包含所有来自第一个关系的元组(本例中为student_details),其年龄为22岁,第二个 bag包含来自具有年龄22的第二关系(在这种情况下是employee_details)的所有元组。如果关系不具有年龄值为22的元组,则它返回一个空包。

获得有空包的组

让我们使用 IsEmpty() 函数从组中的 emp_sales 关系中列出这些空包。

grunt> isempty_data = filter cogroup_data by IsEmpty(emp_sales);

验证

使用DUMP运算符验证关系 isempty_data ,如下所示。 emp_sales 关系保存了 emp_bonus 关系中不存在的元组。

grunt> Dump isempty_data; 
  
(30,{},{(6,Omar,30,30000,admin)})

Apache Pig Eval函数Apache Pig Eval函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)