使用loadfunction pig UDF将protobuf格式文件加载到pig脚本中



我对猪知之甚少。我有protobuf格式的数据文件。我需要将这个文件加载到一个pig脚本中。我需要写一个loadfunction UDF来加载它。假设函数是Protobufloader()

我的PIG脚本应该是

A = LOAD 'abc_protobuf.dat' USING Protobufloader() as (name, phonenumber, email);

所有我想知道的是我如何得到文件输入流。一旦我获得了文件输入流,我就可以将数据从protobuf格式解析为PIG元组格式。

PS: thanks in advance

Twitter的开源库象鸟有很多这样的加载器:https://github.com/kevinweil/elephant-bird

你可以使用LzoProtobufB64LinePigLoader和LzoProtobufBlockPigLoader。https://github.com/kevinweil/elephant-bird/tree/master/src/java/com/twitter/elephantbird/pig/load

使用它,你只需要这样做:

define ProtoLoader com.twitter.elephantbird.pig.load.LzoProtobufB64LineLoader('your.proto.class.name');
a = load '/your/file' using ProtoLoader;
b = foreach a generate
  field1, field2;

加载后,它将自动转换为具有合适模式的pig元组。

但是,它们假设您将数据写入序列化的protobuffer并使用lzo压缩。

它们也有相应的作者,在package com.twitter.elephantbird.pig.store中。如果您的数据格式有点不同,您可以将它们的代码调整为您的自定义加载器。

最新更新