【发布时间】: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