×

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


Apache Pig 加载和存储函数Apache Pig 加载和存储函数


PigStorage() 函数将数据加载并存储为结构化文本文件。它采用分隔符,使用它来将元组的每个实体分隔为一个参数。默认情况下,以't'作为参数。

语法

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

grunt> PigStorage(field_delimiter)

假设在名为 /data/ 的HDFS目录中有一个名为 student_data.txt 的文件,其中包含以下内容。

001,Rajiv,Reddy,9848022337,Hyderabad
002,siddarth,Battacharya,9848022338,Kolkata 
003,Rajesh,Khanna,9848022339,Delhi  
004,Preethi,Agarwal,9848022330,Pune 
005,Trupthi,Mohanthy,9848022336,Bhuwaneshwar
006,Archana,Mishra,9848022335,Chennai.

我们可以使用PigStorage函数加载数据,如下所示。

grunt> student = LOAD 'hdfs://localhost:9000/pig_data/student_data.txt' USING PigStorage(',')
   as ( id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray );

在上面的例子中,我们已经看到过使用逗号(',')分隔符。因此,我们使用(,分隔了记录的值。

同样,我们可以使用 PigStorage() 函数将数据存储到HDFS目录中,如下所示。

grunt> STORE student INTO ' hdfs://localhost:9000/pig_Output/ ' USING PigStorage (',');

这将把数据存储到给定的目录中。可以按如下所示验证数据。

验证

你可以如下所示验证存储的数据。首先,使用 ls 命令列出名为 pig_output 的目录中的文件,如下所示。

$ hdfs dfs -ls 'hdfs://localhost:9000/pig_Output/'
 
Found 2 items 
rw-r--r- 1 Hadoop supergroup 0 2015-10-05 13:03 hdfs://localhost:9000/pig_Output/_SUCCESS
 
rw-r--r- 1 Hadoop supergroup 224 2015-10-05 13:03 hdfs://localhost:9000/pig_Output/part-m-00000

可以观察到在执行 Store 语句后创建了两个文件。

然后,使用 cat 命令,列出名为 part-m-00000 的文件的内容,如下所示。

$ hdfs dfs -cat 'hdfs://localhost:9000/pig_Output/part-m-00000'
  
1,Rajiv,Reddy,9848022337,Hyderabad  
2,siddarth,Battacharya,9848022338,Kolkata  
3,Rajesh,Khanna,9848022339,Delhi  
4,Preethi,Agarwal,9848022330,Pune  
5,Trupthi,Mohanthy,9848022336,Bhuwaneshwar 
6,Archana,Mishra,9848022335,Chennai

Apache Pig 加载和存储函数Apache Pig 加载和存储函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)