×

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


Apache Pig Eval函数Apache Pig Eval函数


在执行join之类的操作以区分两个模式的列之后,我们使用函数 PluckTuple() 要使用此函数,首先,我们必须定义一个字符串Prefix,并且我们必须对以该prefix开头的关系中的列进行过滤。

语法

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

DEFINE pluck PluckTuple(expression1) 
DEFINE pluck PluckTuple(expression1,expression3) 
pluck(expression2)

假设在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);

使用 join 运算符连接这两个关系,如下所示。

grunt> join_data = join emp_sales by sno, emp_bonus by sno;

使用 Dump 运算符验证关系 join_data

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

使用PluckTuple() 函数

现在,使用 PluckTupe() 函数定义要用来区分列的必需表达式。

grunt> DEFINE pluck PluckTuple('a::');

过滤 join_data 关系中的列,如下所示。

grunt> data = foreach join_data generate FLATTEN(pluck(*));

描述名为 data 的关系,如下所示。

grunt> Describe data;
 
data: {emp_sales::sno: int, emp_sales::name: chararray, emp_sales::age: int,
   emp_sales::salary: int, emp_sales::dept: chararray, emp_bonus::sno: int,
   emp_bonus::name: chararray, emp_bonus::age: int, emp_bonus::salary: int,
   emp_bonus::dept: chararray}

由于我们已将表达式定义为“a ::",因此 emp_sales 模式的列将被作为 emp_sales :: column name emp_bonus 模式的列将被作为 emp_bonus :: column name


Apache Pig Eval函数Apache Pig Eval函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)