×

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


Apache Pig Eval函数Apache Pig Eval函数


Pig Latin的 TOKENIZE() 函数用于在单个元组中分割字符串(其中包含一组字),并返回包含拆分操作输出的包。

语法

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

grunt> TOKENIZE(expression [, 'field_delimiter']) 

作为 TOKENIZE() 函数的分隔符,我们可以传递空格[],双引号[“"],逗号 [,],括号[()],星号[*]。

假设在HDFS目录 /pig_data/ 中有一个名为 student_details.txt 的文件,如下所示。此文件包含学生的详细信息,如id,姓名,年龄和城市。如果我们仔细观察,学生的姓名包括用空格[]分隔的名字和姓氏。

student_details.txt

001,Rajiv Reddy,21,Hyderabad
002,siddarth Battacharya,22,Kolkata 
003,Rajesh Khanna,22,Delhi 
004,Preethi Agarwal,21,Pune 
005,Trupthi Mohanthy,23,Bhuwaneshwar 
006,Archana Mishra,23 ,Chennai 
007,Komal Nayak,24,trivendram 
008,Bharathi Nambiayar,24,Chennai 

通过关系 student_details 将此文件加载到的Pig中,如下所示。

grunt> student_details = LOAD 'hdfs://localhost:9000/pig_data/student_details.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int,  city:chararray);

拆分字符串

我们可以使用 TOKENIZE() 函数拆分字符串。作为示例,让我们使用此函数分割名称,如下所示。

grunt> student_name_tokenize = foreach student_details  Generate TOKENIZE(name);

验证

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

grunt> Dump student_name_tokenize;

输出

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

({(Rajaiv),(Reddy)})
({(siddarth),(Battacharya)})
({(Rajesh),(Khanna)})
({(Preethi),(Agarwal)})
({(Trupthi),(Mohanthy)})
({(Archana),(Mishra)})
({(Komal),(Nayak)})
({(Bharathi),(Nambiayar)})

其他分隔符

以同样的方式,TOKENIZE()函数接受空格[],双引号[“"],逗号[,],括号[()],星号[*]作为分隔符。

假设有一个名为 details.txt 的文件,其中包含学生的详细信息,如id,name,age和city。在名称列下,此文件包含由各种分隔符分隔的学生的名字和姓氏,如下所示。

details.txt

001,"siddarth""Battacharya",22,Kolkata 
002,Rajesh*Khanna,22,Delhi 
003,(Preethi)(Agarwal),21,Pune 

使用关系 details 将此文件加载到Pig中,如下所示。

grunt> details = LOAD 'hdfs://localhost:9000/pig_data/details.txt' USING PigStorage(',')
   as (id:int, name:chararray, age:int,  city:chararray);

现在,尝试使用TOKENIZE()分隔学生的名字和姓氏如下。

grunt> tokenize_data = foreach details Generate TOKENIZE(name);

在使用dump运算符验证 tokenize_data 关系时,将获得以下结果。

grunt> Dump tokenize_data;

({(siddarth),(Battacharya)})
({(Rajesh),(Khanna)})
({(Preethi),(Agarwal)})

Apache Pig Eval函数Apache Pig Eval函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)