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

hadoop自定义输出文件名

2015-10-20 15:41 519 查看
使用MultipleOutputs可以定义输出文件名的前缀,但文件名总是会出现讨厌的-00000,我们可以通过查看源码,发现输出文件名
FileOutputFormat
中的某个函数实现,如下:

public Path getDefaultWorkFile(TaskAttemptContext context,String extension) throws IOException{
FileOutputCommitter committer = (FileOutputCommitter) getOutputCommitter(context);
return new Path(committer.getWorkPath(), getUniqueFile(context, getOutputName(context), extension));
}

public synchronized static String getUniqueFile(TaskAttemptContext context, String name, String extension) {
TaskID taskId = context.getTaskAttemptID().getTaskID();
int partition = taskId.getId();
StringBuilder result = new StringBuilder();
result.append(name);
result.append('-');
result.append(
TaskID.getRepresentingCharacter(taskId.getTaskType()));
result.append('-');
result.append(NUMBER_FORMAT.format(partition));
result.append(extension);
return result.toString();
}


所以假如我们想要让输入文件名是什么输出文件名就是什么,可以通过继承
TextOutputFormat
,重写里面的
getDefaultWorkFile
函数,如下

public Path getDefaultWorkFile(TaskAttemptContext context,String extension) throws IOException{
FileOutputCommitter committer = (FileOutputCommitter) getOutputCommitter(context);
return new Path(committer.getWorkPath(), getOutputName(context));
}


最后通过
job.setOutputFormatClass(XXX.class)
来设置我们自定义的
OutputFormat
就行了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: