使用Pig脚本将文本文件转换为avro

时间:2015-01-31 05:21:13

标签: apache-pig avro

我正在使用pig脚本

处理文本文件到Avro转换

`我有一个管道分隔格式的文本文件位于/user/hduser/pig_input/abc.dat

1|8|123|985|659856|10000000002546 1|8|123|985|659856|10000000002546 1|8|123|985|659856|10000000002546 1|8|123|985|659856|10000000002546 1|8|123|985|659856|10000000002546

Schema文件位于hdfs /user/hduser/pig_schema_files/abc.avsc

{
  "type" : "record",
  "name" : "import_dummy",
  "doc" : "import_123dummy",
  "fields" : [ {
  "name" : "ID",
  "type" : [ "string", "null" ],
  "columnName" : "ID",
  "sqlType" : "3"
  }, {
  "name" : "TRANS_O",
  "type" : [ "string", "null" ],
  "columnName" : "TRANS_O",
  "sqlType" : "3"
 }, {
 "name" : "CARD_O",
 "type" : [ "string", "null" ],
 "columnName" : "CARD_O",
 "sqlType" : "3"
 }, {
 "name" : "SEQ_O",
 "type" : [ "string", "null" ],
 "columnName" : "SEQ_O",
 "sqlType" : "1"
 }, {
 "name" : "DATE_O",
 "type" : [ "string", "null" ],
 "columnName" : "DATE_O",
 "sqlType" : "3"
 }],"tableName" : "123dummy"}

以下是我写的脚本

REGISTER /app/cloudera/parcels/CDH/lib/pig/piggybank.jar
REGISTER /app/cloudera/parcels/CDH/lib/pig/lib/avro-1.3.7.jar
REGISTER /app/cloudera/parcels/CDH/lib/pig/lib/jackson-core-asl.jar
REGISTER /app/cloudera/parcels/CDH/lib/pig/lib/jackson-mapper-asl.jar
REGISTER /app/cloudera/parcels/CDH/lib/pig/lib/json-simple.jar
REGISTER /app/cloudera/parcels/CDH/lib/pig/lib/snappy-java.jar

textfile = load 'user/hduser/pig_input/abc.dat' using pigStorage('|');
STORE textfile INTO '/user/hduser/pig_output/' 
    USING org.apache.pig.piggybank.storage.avro.AvroStorage('schema_file','/user/hduser/pig_schema_files/abc.avsc');

运行脚本后出现以下错误:

2015-02-03 09:46:56,369 [main] ERROR org.apache.pig.tools.grunt.Grunt  -                                         ERROR 6000:<file script.pig, line 9, column 0> 
    Output Location Validation Failed for: '/user/hduser/pig_output/
    More info to follow:
    Output schema is null!

1 个答案:

答案 0 :(得分:0)

除CSV文件外,我们还必须阅读字段名称,以便在编写avro时,它会自动映射字段名称。

textfile = load 'user/hduser/pig_input/abc.dat' using pigStorage('|') as (ID, TRANS_O,CARD_O,SEQ_O, DATE_O );
STORE textfile INTO '/user/hduser/pig_output/' USING org.apache.pig.piggybank.storage.avro.AvroStorage();