【问题标题】:Table Schema inside PIG UDFPIG UDF 中的表模式
【发布时间】:2015-05-27 21:05:50
【问题描述】:

在将数据加载到 Hive 表之前,我必须在平面文件中格式化数据。

CF32|4711|00010101Z| +34.883|  98562AS1D |N8594ãä| 00   | 2

该文件是管道分隔的,我需要在平面文件的不同列上应用不同的清理和格式化功能。 我对 Clean_Text、Format_Date、Format_TimeStamp、Format_Integer 等有多种功能。

我的想法是将架构作为构造函数传递给我的 UDF,并在 pig 中的平面文件上调用不同的函数。

A = LOAD 'call_detail_records'  USING org.apache.hcatalog.pig.HCatLoader();
DESCRIBE A;

REGISTER ZPigUdfs.jar;
DEFINE DFormat com.zna.pig.udf.DataColumnFormatter(A);

B = FOREACH A GENERATE DFormat($0);
DUMP B;

但我怎样才能传递架构? DUMP A 实际上转储了整个表,但我只需要元数据。 我当前的 UDF 伪代码看起来像

公共类 DataColumnFormatter 扩展 EvalFunc {

private Tuple schema;

public DataColumnFormatter(Tuple schema) {
    this.schema = schema;
}

@Override
public String exec(Tuple inputTuple) throws IOException {

    if (inputTuple != null && inputTuple.size() > 0) {
        String inpString = inputTuple.get(0).toString();
        System.out.println(inpString);
        System.out.println(schema);

        /**
         * Logic for splitting the string as pipe and apply functions based
         * on positions of schema if(schema[1] -> date ){
         * 
         * formatDate(input) }else if(schema[1] -> INT ){
         * 
         * formatInt(input); }
         * 
         */

    }

    return null;
}

}

如何在 PIG UDF 中获取架构,或者有其他方法可以实现这一点。

提前致谢。

【问题讨论】:

  • (1) 这个模式应该从哪里来? (2) 是什么阻碍了您将模式定义为常量? (3) 同一张表中的不同行是否可能有不同的架构?
  • 架构应该来自 HCatalog。我有多个文件,我不想每次运行脚本时都定义架构。表中的所有记录都没有相同的模式。

标签: hadoop hive apache-pig hcatalog udf


【解决方案1】:

在您的 EvalFunc 中,您可以调用 this.getInputSchema()(至少从 Pig v0.12 开始,可能更早)。你不需要做任何特别的事情来传递模式,因为你是从 HCatalog 加载的,A 已经被装饰了。

或者,您可以考虑为每种数据类型拆分单独的 UDF 函数。类似B = FOREACH A GENERATE dateFormat($0), cleanText($1), dateFormat($2);

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多