MultipleOutputFormat allowing to write the output data to different output files.
MultipleOutputFormat可以将相似的记录输出到相同的数据集。在写每条记录之前,MultipleOutputFormat将调用generateFileNameForKeyValue方法来确定需要写入的文件名。通常,我们都是继承MultipleTextOutputFormat类,来重新实现generateFileNameForKeyValue方法以返回每个输出键/值对的文件名。
在generateFileNameForKeyValue函数中参数String name为默认的输出part-00000:
/** * 多目录输出 */
MultipleOutputFormat可以将相似的记录输出到相同的数据集。在写每条记录之前,MultipleOutputFormat将调用generateFileNameForKeyValue方法来确定需要写入的文件名。通常,我们都是继承MultipleTextOutputFormat类,来重新实现generateFileNameForKeyValue方法以返回每个输出键/值对的文件名。generateFileNameForKeyValue方法的默认实现如下:
1
2
3
|
protected
String generateFileNameForKeyValue(K key, V value, String name) {
return
name;
}
|
返回默认的name,我们可以在自己的类中重写这个方法,来定义自己的输出路径,比如:
01
02
03
04
05
06
07
08
09
10
11
12
13
|
public static class
RDDMultipleTextOutputFormat
extends
MultipleTextOutputFormat<NullWritable, Text> {
@Override
protected
String generateFileNameForKeyValue(
NullWritable key,
Text value,
String name) {
String[] split = value.toString().split(
","
, -
1
);
String country = split[
4
].substring(
1
,
3
);
return
country +
"/"
+ name;
}
//输出时不输出key override def generateActualKey(key: Any, value: Any): Any = NullWritable.get() }
|
//调用
modeltags
.partitionBy(new HashPartitioner(20)) //按新key重新分区,否则会出现数据混乱丢失
.saveAsHadoopFile(outputPath, classOf[String], classOf[String], classOf[RDDMultipleTextOutputFormat])