您的位置:58编程 > apache pivot Apache Pig PigStorage()函数

apache pivot Apache Pig PigStorage()函数

2023-05-20 01:33 ApachePig教程

apache pivot Apache Pig PigStorage()函数

apache pivot Apache Pig PigStorage()函数

apache pivot

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


阅读全文
以上是58编程为你收集整理的apache pivot Apache Pig PigStorage()函数全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58编程 58biancheng.com 版权所有 联系我们
桂ICP备12005667号-32 Powered by CMS