- Apache Pig 教程
- Apache Pig - 首頁
- 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 - Describe 運算子
- Apache Pig - Explain 運算子
- Apache Pig - Illustrate 運算子
- 分組 & 連線
- Apache Pig - Group 運算子
- Apache Pig - Cogroup 運算子
- Apache Pig - Join 運算子
- Apache Pig - Cross 運算子
- Pig Latin 內建函式
- Apache Pig - Eval 函式
- 載入 & 儲存函式
- Apache Pig - Bag & Tuple 函式
- Apache Pig - 字串函式
- Apache Pig - 日期時間函式
- Apache Pig - 數學函式
- Apache Pig 有用資源
- Apache Pig - 快速指南
- Apache Pig - 有用資源
- Apache Pig - 討論
Apache Pig - PigStorage() 函式
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://: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://:9000/pig_Output/ ' USING PigStorage (',');
這會將資料儲存到指定的目錄中。您可以按照以下步驟驗證資料。
驗證
您可以按照以下步驟驗證儲存的資料。首先,使用ls命令列出名為pig_output目錄中的檔案,如下所示。
$ hdfs dfs -ls 'hdfs://:9000/pig_Output/' Found 2 items rw-r--r- 1 Hadoop supergroup 0 2015-10-05 13:03 hdfs://:9000/pig_Output/_SUCCESS rw-r--r- 1 Hadoop supergroup 224 2015-10-05 13:03 hdfs://:9000/pig_Output/part-m-00000
您可以看到在執行Store語句後建立了兩個檔案。
然後,使用cat命令列出名為part-m-00000檔案的內容,如下所示。
$ hdfs dfs -cat 'hdfs://: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_load_store_functions.htm
廣告