您的位置:首页 > 运维架构

控制Hadoop的reducer函数输出文件命名

2013-11-06 23:52 169 查看
Hadoop 控制输出文件命名

在一般情况下,Hadoop 每一个 Reducer 产生一个输出文件,文件以

part-r-00000、part-r-00001 的方式进行命名。如果需要人为的控制输出文件的命

名或者每一个 Reducer 需要写出多个输出文件时,可以采用 MultipleOutputs 类来

完成。MultipleOutputs 采用输出记录的键值对(output Key 和 output Value)或者

任意字符串来生成输出文件的名字,文件一般以 name-r-nnnnn 的格式进行命名,

其中 name 是程序设置的任意名字;nnnnn 表示分区号。

MultipleOutputs 的使用方式 的使用方式: :: :

想要使用 MultipeOutputs,需要完成以下四个步骤:

1. 在 Reducer 中声明 MultipleOutputs 的变量

private MultipleOutputs<NullWritable, Text> multipleOutputs;

2. 在 Reducer 的 setup 函数中进行 MultipleOutputs 的初始化

protected void setup(Context context)throws IOException, InterruptedException {

multipleOutputs = new MultipleOutputs<NullWritable, Text>(context);

}


3. 在 reduce 函数中进行输出控制

protected void reduce(Text key, Iterable<Text> values, Context context)throws IOException,

InterruptedException {

for (Text value : values) {

multipleOutputs.write(NullWritable.get(), value, key.toString());

}

}


4. 在 cleanup 函数中关闭输出 MultipleOutputs

protected void cleanup(Context context)throws IOException, InterruptedException {

multipleOutputs.close();

}


注意:multipleOutputs.write(key, value, baseOutputPath)方法的第三个函数表明了该输出所在的目录(相对于用户指定的输出目录)。如果baseOutputPath不包含文件分隔符“/”,那么输出的文件格式为baseOutputPath-r-nnnnn(name-r-nnnnn);如果包含文件分隔符“/”,例如baseOutputPath=“029070-99999/1901/part”,那么输出文件则为






来源
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: