当前位置:K88软件开发文章中心大数据Apache Pig → 文章内容

Apache Pig 存储数据

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-26 10:03:33

由 jarodhu 创建,youj 最后一次修改 2016-12-28 在上一章中,我们学习了如何将数据加载到Apache Pig中。你可以使用 store 运算符将加载的数据存储在文件系统中,本章介绍如何使用 Store 运算符在Apache Pig中存储数据。语法下面给出了Store语句的语法。STORE Relation_name INTO ' required_directory_path ' [USING function];例假设我们在HDFS中有一个包含以下内容的文件 student_data.txt 。001,Rajiv,Reddy,9848022337,Hyderabad002,siddarth,Battacharya,9848022338,Kolkata003,Rajesh,Khanna,9848022339,Delhi004,Preethi,Agarwal,9848022330,Pune005,Trupthi,Mohanthy,9848022336,Bhuwaneshwar006,Archana,Mishra,9848022335,Chennai.使用LOAD运算符将它读入关系 student ,如下所示。grunt> student = LOAD 'hdfs://localhost: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://localhost: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% complete2015-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/aMaxReduceTime MinReduceTime AvgReduceTime MedianReducetime Alias Feature 0 0 0 0 student MAP_ONLY OutPut folderhdfs://localhost:9000/pig_Output/ Input(s): Successfully read 0 records from: "hdfs://localhost:9000/pig_data/student_data.txt" Output(s): Successfully stored 0 records in: "hdfs://localhost:9000/pig_Output" Counters:Total records written : 0Total bytes written : 0Spillable Memory Manager spill count : 0 Total bags proactively spilled: 0Total 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://localhost:9000/pig_Output/'Found 2 itemsrw-r--r- 1 Hadoop supergroup 0 2015-10-05 13:03 hdfs://localhost:9000/pig_Output/_SUCCESSrw-r--r- 1 Hadoop supergroup 224 2015-10-05 13:03 hdfs://localhost:9000/pig_Output/part-m-00000 可以观察到在执行 store 语句后创建了两个文件。步骤2使用 cat 命令,列出名为 part-m-00000 的文件的内容,如下所示。$ hdfs dfs -cat 'hdfs://localhost:9000/pig_Output/part-m-00000' 1,Rajiv,Reddy,9848022337,Hyderabad2,siddarth,Battacharya,9848022338,Kolkata3,Rajesh,Khanna,9848022339,Delhi4,Preethi,Agarwal,9848022330,Pune5,Trupthi,Mohanthy,9848022336,Bhuwaneshwar6,Archana,Mishra,9848022335,Chennai

Apache Pig 存储数据