hadoop自定义文件输出格式
2013-04-22 17:03
393 查看
自定义文件的输出格式相对于输入格式来说就简单一些,在自己的输出格式里面实现RecordWriter接口即可,关键是同步写方法的实现可以把key,value的输出控制的很灵活,看看例子吧:
public class ImageOutputFormat extends TextOutputFormat<Text, DistanceVector> { protected static class Writer extends RecordWriter<Text, DistanceVector> { private LineRecordWriter<Text, Text> w; public Writer(DataOutputStream dos, String codec) { w = new LineRecordWriter<Text, Text>(dos, codec); } public Writer(DataOutputStream dos) { w = new LineRecordWriter<Text, Text>(dos); } // 这里控制自己的输出style public synchronized void write(Text key, DistanceVector value) throws IOException { StringBuffer sb = new StringBuffer(); System.out.println(value.getV2()); java.util.Iterator<Double> iter=value.getV1().iterator(); while(iter.hasNext()) { sb.append(iter.next());new Text(key.toString()); sb.append(","); } sb.append(" distance:"+value.getV2()); w.write(new Text(key), new Text(sb.substring(0, sb.length() - 1) + "\n")); } @Override public void close(TaskAttemptContext context) throws IOException, InterruptedException { w.close(context); } ; } @Override public RecordWriter<Text, DistanceVector> getRecordWriter( TaskAttemptContext job) throws IOException, InterruptedException { Configuration conf = job.getConfiguration(); boolean isCompressed = getCompressOutput(job); String keyValueSeparator = conf.get( "mapred.textoutputformat.separator", "\t");//key value的分隔符自己可以设置 CompressionCodec codec = null; String extension = ""; if (isCompressed) { Class<? extends CompressionCodec> codecClass = getOutputCompressorClass( job, GzipCodec.class); codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf); extension = codec.getDefaultExtension(); } Path file = getDefaultWorkFile(job, extension); FileSystem fs = file.getFileSystem(conf); if (!isCompressed) { FSDataOutputStream fileOut = fs.create(file, false); return new Writer(fileOut, keyValueSeparator); } else { FSDataOutputStream fileOut = fs.create(file, false); return new Writer(new DataOutputStream(codec.createOutputStream(fileOut)), keyValueSeparator); } } }
相关文章推荐
- hadoop---自定义输出文件格式以及输出到不同目录
- hadoop自定义输出格式
- 自定义记录格式输出HDFS文件
- Hadoop的多文件输出及自定义文件名
- Hadoop(十):简单了解Hadoop数据类型,输入输出格式及用户如何自定义。
- 【Hadoop】利用MultipleOutputs,MultiOutputFormat实现以不同格式输出到多个文件
- Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)
- Hadoop 实例15 MultipleInputs实战2:多种自定义文件格式的文件输入处理
- [Hadoop--基础]--用户自定义mapreduce输出的文件名称
- Hadoop学习----直接使用FileSystem以标准输出格式显示Hadoop文件系统中的文件
- JAVA中自定义日志输出格式及自定义文件处理器的实现
- hadoop自定义多文件及多文件夹输出
- hadoop自定义文件的输入格式
- 自定义实现InputFormat、OutputFormat、输出到多个文件目录中去、hadoop1.x api写单词计数的例子、运行时接收命令行参数,代码例子
- Hadoop基于WordCount的Mapper、Reducer、Combiner、Partitioner和自定义多文件输出
- UStore-自定义JDF文件格式输出
- hadoop生态系统学习之路(四)MR支持的输入文件格式以及输出文件格式
- Hadoop实验:用java直接使用FileSystem以标准输出格式显示Hadoop文件系统中的文件