- 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 - 資料儲存
在上一章中,我們學習瞭如何將資料載入到 Apache Pig 中。您可以使用 **store** 運算子將載入的資料儲存到檔案系統中。本章解釋瞭如何使用 **Store** 運算子在 Apache Pig 中儲存資料。
語法
以下是 Store 語句的語法。
STORE Relation_name INTO ' required_directory_path ' [USING function];
示例
假設我們在 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.
並且我們已經使用 LOAD 運算子將其讀取到名為 **student** 的關係中,如下所示。
grunt> student = LOAD 'hdfs://:9000/pig_data/student_data.txt'
USING PigStorage(',')
as ( id:int, firstname:chararray, lastname:chararray, phone:chararray,
city:chararray );
現在,讓我們將關係儲存到 HDFS 目錄 **“/pig_Output/”** 中,如下所示。
grunt> STORE student INTO ' hdfs://:9000/pig_Output/ ' USING PigStorage (',');
輸出
執行 **store** 語句後,您將獲得以下輸出。將建立具有指定名稱的目錄,並將資料儲存在其中。
2015-10-05 13:05:05,429 [main] INFO org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.
MapReduceLau ncher - 100% complete
2015-10-05 13:05:05,429 [main] INFO org.apache.pig.tools.pigstats.mapreduce.SimplePigStats -
Script Statistics:
HadoopVersion PigVersion UserId StartedAt FinishedAt Features
2.6.0 0.15.0 Hadoop 2015-10-0 13:03:03 2015-10-05 13:05:05 UNKNOWN
Success!
Job Stats (time in seconds):
JobId Maps Reduces MaxMapTime MinMapTime AvgMapTime MedianMapTime
job_14459_06 1 0 n/a n/a n/a n/a
MaxReduceTime MinReduceTime AvgReduceTime MedianReducetime Alias Feature
0 0 0 0 student MAP_ONLY
OutPut folder
hdfs://:9000/pig_Output/
Input(s): Successfully read 0 records from: "hdfs://:9000/pig_data/student_data.txt"
Output(s): Successfully stored 0 records in: "hdfs://:9000/pig_Output"
Counters:
Total records written : 0
Total bytes written : 0
Spillable Memory Manager spill count : 0
Total bags proactively spilled: 0
Total records proactively spilled: 0
Job DAG: job_1443519499159_0006
2015-10-05 13:06:06,192 [main] INFO org.apache.pig.backend.hadoop.executionengine
.mapReduceLayer.MapReduceLau ncher - Success!
驗證
您可以驗證儲存的資料,如下所示。
步驟 1
首先,使用 **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** 語句後建立了兩個檔案。
步驟 2
使用 **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
廣告