×

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


Apache Pig Eval函数Apache Pig Eval函数


Pig Latin的  BagToString() 函数用于将包的元素连接成字符串。在连接时,我们可以在这些值之间放置分隔符(可选)。通常,包是无序的,可以使用 ORDER BY 运算符排序。

语法

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

grunt> BagToString(vals:bag [, delimiter:chararray])

假设在HDFS目录 /pig_data/ 中有一个名为 date of birth.txt 的文件,如下所示。 此文件包含出生日期。

dateofbirth.txt

22,3,1990
23,11,1989
1,3,1998
2,6,1980
26,9,1989

通过关系 dob 将这个文件加载到Pig中,如下所示。

grunt> dob = LOAD 'hdfs://localhost:9000/pig_data/dateofbirth.txt' USING PigStorage(',')
   as (day:int, month:int, year:int);

将包转换为字符串

使用 bagtostring() 函数,我们可以将bag中的数据转换为字符串。让我们将 dob 关系分组,组操作将生成含有关系的所有元组的包。

使用Group All运算符将关系 dob 分组,并将结果存储在名为 group_dob 的关系中,如下所示。

grunt> group_dob = Group dob All;

它将产生如下所示的关系。

grunt> Dump group_dob; 
 
(all,{(26,9,1989),(2,6,1980),(1,3,1998),(23,11,1989),(22,3,1990)})

在这里,我们可以观察到一个有所有出生日期的包作为它的元组。现在,让我们使用函数 BagToString() 将包转换为字符串。

grunt> dob_string = foreach group_dob Generate BagToString(dob);

验证

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

grunt> Dump dob_string;

输出

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

(26_9_1989_2_6_1980_1_3_1998_23_11_1989_22_3_1990)

Apache Pig Eval函数Apache Pig Eval函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)