【问题标题】:Pig UDF - Return dynamic schema as a set of fields (not tuple)Pig UDF - 将动态模式作为一组字段(不是元组)返回
【发布时间】:2015-08-18 06:19:01
【问题描述】:

在 GROUP BY + FLATTEN 之后,我有一个带有命名空间的数据:

DESCRIBE users;
users: {user_id: int, group_id: int, registration_timestamp: int}

users_with_namespace = FOREACH (GROUP users BY group_id) {
    first_to_latest = ORDER users BY registration_timestamp ASC;
    first_user = LIMIT first_to_latest 1;
    GENERATE FLATTEN(first_user);
};

DESCRIBE users_with_namespace;
users_with_namespace: {first_user::user_id: int, first_user::group_id: int, first_user::registration_timestamp: int}

我希望能够做类似的事情:

users = myudf.strip_namespace(users_with_namespace);

或者(因为这似乎不可能):

users = FOREACH (GROUP users_with_namespaceALL) 
GENERATE myudf.strip_namespace(users_with_namespace);

结果是:

> DESCRIBE users;
users: {user_id: int, registration_timestamp: int}

我编写了一个 Jython Pig UDF,它应该删除任何命名空间的字段名称,但我似乎无法从我的 UDF 返回一组字段。只有 Bag/Tuple/Single 字段是可能的,这给我留下了这样的结果:

DESCRIBE users;
users: {t: (user_id: int, registration_timestamp: int)}

有什么方法可以省略 't' 并返回一个列表/字段集?我的 UDF 如下所示:

@outputSchemaFunction("tupleSchema")
def strip_namespace(input):
    return input


@schemaFunction("tupleSchema")
def tupleSchema(input):
    fields = []
    dt = []
    for i in input.getField(0).schema.getFields():
        for field in i.schema.getFields():
            fields.append(field.alias.split("::")[-1])
            dt.append(field.type)
    return SchemaUtil.newTupleSchema(fields, dt)

到目前为止我用过

FOREACH .. GENERATE namespace::field as field, ...

去除命名空间,但这种方法对于具有许多字段的数据集来说确实很乏味。

【问题讨论】:

    标签: apache-pig jython user-defined-functions


    【解决方案1】:

    很遗憾,您不能,至少现在不能。问题正是你所说的:现在你只能返回一个元组、一个包或一个字段。 2 个月前,我创建了一个 JIRA issue 以允许为这种情况返回多个字段,但还没有回复......

    我真的希望他们将来实现这一点,因为当您必须执行许多连接时,您最终会得到比实际代码更多的 FOREACH 语句来重命名字段。

    【讨论】:

      猜你喜欢
      • 2016-10-05
      • 2012-11-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多