【问题标题】:Un-nesting nested tuples to single terms将嵌套元组取消嵌套到单个术语
【发布时间】:2015-08-22 00:12:14
【问题描述】:

我编写了一个 udf (extends EvalFunc<Tuple>),其中包含带有内部元组(嵌套)的输出元组。

例如转储看起来像:

(((photo,photos,photo)))
(((wedg,wedge),(audusd,audusd)))
(((quantum,quantum),(mind,mind)))
(((cassi,cassie),(cancion,canciones)))
(((calda,caldas),(nova,novas),(rodada,rodada)))
(((fingerprint,fingerprint),(craft,craft),(easter,easter)))

现在我想处理每个术语,区分它并给它一个 id (RANK)。为此,我需要去掉括号。一个简单的FLATTEN在这种情况下没有帮助。

最终的输出应该是这样的:

1 photo
2 photos
3 wedg
4 wedge
5 audusd
6 quantum
7 mind
....

我的代码(不是 udf 部分,也不是原始解析):

tags = FOREACH raw GENERATE FLATTEN(tags) AS tag;
tags_distinct = DISTINCT tags;
tags_sorted = RANK tags_distinct BY tag;
DUMP tags_sorted;

【问题讨论】:

    标签: nested tuples apache-pig flatten udf


    【解决方案1】:

    我认为您的 UDF 是返回不是您的工作流程的最佳选择。与其返回具有可变数量字段(即元组)的元组,不如返回一袋元组更方便。

    代替

    (((wedg,wedge),(audusd,audusd)))
    

    你会有

    ({(wedg,wedge),(audusd,audusd)})
    

    您将能够将该包压平以: 1. 与众不同 2. 给标签排序

    为此,请像这样更新您的 UDF:

    class MyUDF extends EvalFunc <DataBag> {
    
        @Override
        public DataBag exec(Tuple input) throws IOException {
            // create DataBag
        }
    }
    

    【讨论】:

    • 感谢您的建议。如何指定数据类型? “xyz AS 标签:包{t:tuple()};” - 像这样?
    • 是的,谢谢,但我已经在我的 UDF 中进行了更改。我的意思是猪的数据类型。告诉 pig 它是一个带有元组的包,以便我可以迭代它。我的问题是我无法弄清楚如何迭代它,直到
    • 如果您无法将数据转储为 ({(f1, f2), (f1, f2)}) 格式,您将需要一个 UDF 来解析您的输入。跨度>
    • 问题是我需要指定数据模式。我以为是“b:bag{t:Tuple()}”,但这不起作用
    猜你喜欢
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多