Pig:FLATTEN 关键词
Pig:FLATTEN keyword
我对 PIG 中 FLATTEN
关键字的使用有点困惑。
考虑以下数据集:
tuple_record: {details: (firstname: chararray,lastname: chararray,age: int,sex: chararray)}
不使用 FLATTEN
我可以像这样访问一个字段(假设名字):
display_firstname = FOREACH tuple_record GENERATE details.firstname;
现在,使用 FLATTEN
关键字:
flatten_record = FOREACH tuple_record GENERATE FLATTEN(details);
DESCRIBE
给我这个:
flatten_record: {details::firstname: chararray,details::lastname: chararray,details::age: int,details::sex: chararray}
因此我可以直接访问存在的字段而无需 dereferencing
像这样:
display_record = FOREACH flatten_record GENERATE firstname;
我与此 FLATTEN
关键字相关的问题是:
1) 两者中哪一种方式(即使用或不使用 FLATTEN
)是实现相同输出的优化方式?
2) 有什么特殊场景不使用FLATTEN
关键字,无法达到预期的输出?
一头雾水;请说明它的用途以及我将在哪些情况下使用它。
- 有时您的包或元组中有数据,您想要删除该层级的嵌套。
- 当您想动态切换数据并按特定字段分组时,您需要一种方法将这些条目从包中提取出来。
根据 Pig 文档:
The FLATTEN operator looks like a UDF syntactically, but it is
actually an operator that changes the structure of tuples and bags in
a way that a UDF cannot. Flatten un-nests tuples as well as bags. The
idea is the same, but the operation and result is different for each
type of structure.
有关更多详细信息,请查看this link他们已经通过示例清楚地解释了 FLATTEN 的用法
我对 PIG 中 FLATTEN
关键字的使用有点困惑。
考虑以下数据集:
tuple_record: {details: (firstname: chararray,lastname: chararray,age: int,sex: chararray)}
不使用 FLATTEN
我可以像这样访问一个字段(假设名字):
display_firstname = FOREACH tuple_record GENERATE details.firstname;
现在,使用 FLATTEN
关键字:
flatten_record = FOREACH tuple_record GENERATE FLATTEN(details);
DESCRIBE
给我这个:
flatten_record: {details::firstname: chararray,details::lastname: chararray,details::age: int,details::sex: chararray}
因此我可以直接访问存在的字段而无需 dereferencing
像这样:
display_record = FOREACH flatten_record GENERATE firstname;
我与此 FLATTEN
关键字相关的问题是:
1) 两者中哪一种方式(即使用或不使用 FLATTEN
)是实现相同输出的优化方式?
2) 有什么特殊场景不使用FLATTEN
关键字,无法达到预期的输出?
一头雾水;请说明它的用途以及我将在哪些情况下使用它。
- 有时您的包或元组中有数据,您想要删除该层级的嵌套。
- 当您想动态切换数据并按特定字段分组时,您需要一种方法将这些条目从包中提取出来。
根据 Pig 文档:
The FLATTEN operator looks like a UDF syntactically, but it is actually an operator that changes the structure of tuples and bags in a way that a UDF cannot. Flatten un-nests tuples as well as bags. The idea is the same, but the operation and result is different for each type of structure.
有关更多详细信息,请查看this link他们已经通过示例清楚地解释了 FLATTEN 的用法