gpt4 book ai didi

hadoop - PIG UDF 中的表架构

转载 作者:可可西里 更新时间:2023-11-01 15:32:28 25 4
gpt4 key购买 nike

在将数据加载到 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 伪代码看起来像

公共(public)类 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 中获取架构,或者是否有任何其他方法可以实现此目的。

提前致谢。

最佳答案

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

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

关于hadoop - PIG UDF 中的表架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30493143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com