【问题标题】:Multiple Output for multiple files loaded in PIG在 PIG 中加载的多个文件的多个输出
【发布时间】:2015-08-08 20:50:25
【问题描述】:

我的数据目录中有 50 个文本文件(路径:/home/admin/Desktop/data)。我的任务是将文本文件中的数据展平(标记)并将输出存储在 50 个输出文件中。

以下是我为完成这项工作而建立的关系:

--This will load all the 50 text files.
A = Load '/home/admin/Desktop/data' Using PigStorage(','); 

--This relation will create every word as a token and will flatten the data.
B = FOREACH A GENERATE FLATTEN(TOKENIZE($0));

STORE B into '/home/ameya/Desktop/PigOutput';

现在,当我执行这个 pig 脚本时,我只得到一个输出文件,用于 50 个输入文件。

如何得到50个不同的输出文件,每个文件都包含与其输入文件中的数据对应的输出数据?

【问题讨论】:

  • 感谢伙伴以正确的方式添加 cmets :)

标签: hadoop apache-pig


【解决方案1】:

拆分运算符可用于根据某个表达式将关系的内容划分为两个或多个关系。根据表达式中提供的条件,将完成以下两个之一:

  • 一个元组可以分配给多个关系
  • 元组不能分配给任何关系

一个目录下的多个文件,用于在pig中加载、展平和存储:

[user1@localhost ~]# ls /pigsamples/mfilesdata/
file1  file2  file3

加载上面的目录:

grunt> input_data = LOAD '/pigsamples/mfilesdata' USING PigStorage (',') AS (f1:INT, f2:INT, f3:INT);
grunt> DUMP input_data;
(1,2,3)
(2,3,1)
(3,1,2)
(4,5,6)
(5,6,4)
(6,4,5)
(7,8,9)
(8,9,7)
(9,7,8)

根据您的要求格式化数据。我使用了与问题中相同的操作。

grunt> formatted_data = FOREACH input_data GENERATE FLATTEN(TOKENIZE($0));    //replace with your requirements

使用SPLIT运算符根据条件将关系拆分为多个关系。

grunt> 
SPLIT formatted_data 
INTO split1 IF f1 <= 3, 
split2 IF (f1 > 3 AND f1 <= 6), 
split3 IF f1 > 6;       //split based on the column which is unique within all the files

输出:

grunt> DUMP split1;
(1,2,3)
(2,3,1)
(3,1,2)

grunt> DUMP split2;
(4,5,6)
(5,6,4)
(6,4,5)

grunt> DUMP split3;
(7,8,9)
(8,9,7)
(9,7,8)

【讨论】:

    【解决方案2】:

    你试过PIG MultiStorage UDF吗?

    如果您想为单独的 50 个 i/p 文件创建 50 个 o/p 文件,那么最好运行您的 PIG 脚本 50 次(循环)并使用 i/p 文件和 o/p 文件作为参数你的 PIG 脚本。

    【讨论】:

    • 据我所知,Multistorage UDF 对于根据输出元组中用户指定的关键字段动态地将输出数据拆分到不同的目录很有用。但我不知道输入文件中有什么。所以无法定义拆分数据的键
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多