hadoop 自定义inputformat和outputformat

本文详细介绍了Hadoop中InputFormat与OutputFormat的工作原理,并通过具体示例展示了如何自定义这两种格式来处理MapReduce任务的数据输入和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blackproof.iteye.com/blog/1


hadoop的inputformat和outputformat

 

最好的例子vertica :虽然是在pig中实现的udf,但是就是hadoop的inputformat和outputformat,在hive里也可以照用,贴个下载的地址:http://blackproof.iteye.com/blog/1791995

 

再贴一个项目中,在实现hadoop join时,用的inputformat和outputformat的简单实例:

hadoop join在http://blackproof.iteye.com/blog/1757530

   自定义inputformat(泛型是maper的input)

Java代码   收藏代码
  1. public class MyInputFormat extends FileInputFormat<MultiKey,Employee> {  
  2.       
  3.     public MyInputFormat(){}  
  4.   
  5.     @Override  
  6.     public RecordReader<MultiKey, Employee> createRecordReader(  
  7.             InputSplit split, TaskAttemptContext context) throws IOException,  
  8.             InterruptedException {  
  9.         // TODO Auto-generated method stub  
  10.         return new MyRecordReader();  
  11.     }  
  12.       
  13.     public static class MyRecordReader extends RecordReader<MultiKey, Employee>{  
  14.   
  15.         public LineReader in;  
  16.         public MultiKey key;  
  17.         public Employee value;  
  18.         public StringTokenizer token = null;  
  19.           
  20.         public Text line;  
  21.           
  22.         @Override  
  23.         public void initialize(InputSplit split, TaskAttemptContext context)  
  24.                 throws IOException, InterruptedException {  
  25.             // TODO Auto-generated method stub  
  26.             FileSplit fileSplit = (FileSplit)split;  
  27.             Configuration job = context.getConfiguration();  
  28.             Path file = fileSplit.getPath();  
  29.             FileSystem fs = file.getFileSystem(job);  
  30.               
  31.             FSDataInputStream filein = fs.open(file);  
  32.             in = new LineReader(filein, job);  
  33.               
  34.             key = new MultiKey();  
  35.             value = new Employee();  
  36.             line = new Text();  
  37.         }  
  38.   
  39.         @Override  
  40.         public boolean nextKeyValue() throws IOException, InterruptedException {  
  41.   
  42.             int linesize = in.readLine(line);  
  43.             if(linesize==0)  
  44.                 return false;  
  45.             String[] pieces = line.toString().split(",");  
  46.             int i = Integer.valueOf(pieces[0]);  
  47.             switch (i) {  
  48.             case 1:  
  49.                 value.setEmpName(pieces[1]);  
  50.                 value.setFlag(1);  
  51.                 break;  
  52.   
  53.             default:  
  54.                 value.setDepartName(pieces[1]);  
  55.                 value.setFlag(2);  
  56.                 break;  
  57.             }  
  58.             value.setDepartId(pieces[2]);  
  59.             value.setDepartNo(pieces[3]);  
  60.               
  61.             key.setDepartId(value.getDepartId());  
  62.             key.setDepartNo(value.getDepartNo());  
  63.             return true;  
  64.         }  
  65.   
  66.         @Override  
  67.         public MultiKey getCurrentKey() throws IOException,  
  68.                 InterruptedException {  
  69.             // TODO Auto-generated method stub  
  70.             return key;  
  71.         }  
  72.   
  73.         @Override  
  74.         public Employee getCurrentValue() throws IOException,  
  75.                 InterruptedException {  
  76.             // TODO Auto-generated method stub  
  77.             return value;  
  78.         }  
  79.   
  80.         @Override  
  81.         public float getProgress() throws IOException, InterruptedException {  
  82.             // TODO Auto-generated method stub  
  83.             return 0;  
  84.         }  
  85.   
  86.         @Override  
  87.         public void close() throws IOException {  
  88.             // TODO Auto-generated method stub  
  89.               
  90.         }  
  91.           
  92.     }  
  93.   
  94. }  

 

 

   自定义outputformat(泛型是reduce的输出)

Java代码   收藏代码
  1. public class MyOutputFormat extends FileOutputFormat<Text, Employee> {  
  2.   
  3.     @Override  
  4.     public RecordWriter<Text, Employee> getRecordWriter(  
  5.             TaskAttemptContext job) throws IOException, InterruptedException {  
  6.         // TODO Auto-generated method stub  
  7.         Configuration conf = job.getConfiguration();  
  8.         Path file = getDefaultWorkFile(job, "");  
  9.         FileSystem fs = file.getFileSystem(conf);  
  10.         FSDataOutputStream fileOut = fs.create(file, false);  
  11.         return new MyRecordWriter(fileOut);  
  12.     }  
  13.       
  14.     public static class MyRecordWriter extends RecordWriter<Text, Employee>{  
  15.   
  16.         protected DataOutputStream out;  
  17.         private final byte[] keyValueSeparator;  
  18.          public static final String NEW_LINE = System.getProperty("line.separator");  
  19.           
  20.         public MyRecordWriter(DataOutputStream out){  
  21.             this(out,":");  
  22.         }  
  23.           
  24.         public MyRecordWriter(DataOutputStream out,String keyValueSeparator){  
  25.             this.out = out;  
  26.             this.keyValueSeparator = keyValueSeparator.getBytes();  
  27.         }  
  28.           
  29.         @Override  
  30.         public void write(Text key, Employee value) throws IOException,  
  31.                 InterruptedException {  
  32.             if(key!=null){  
  33.                 out.write(key.toString().getBytes());  
  34.                 out.write(keyValueSeparator);  
  35.             }  
  36.             out.write(value.toString().getBytes());  
  37.             out.write(NEW_LINE.getBytes());  
  38.         }  
  39.   
  40.         @Override  
  41.         public void close(TaskAttemptContext context) throws IOException,  
  42.                 InterruptedException {  
  43.             out.close();  
  44.         }  
  45.           
  46.     }  
  47.   
  48. }  

 

806263 转自
内容概要:本文介绍了多种开发者工具及其对开发效率的提升作用。首先,介绍了两款集成开发环境(IDE):IntelliJ IDEA 以其智能代码补全、强大的调试工具项目管理功能适用于Java开发者;VS Code 则凭借轻量级多种编程语言的插件支持成为前端开发者的常用工具。其次,提到了基于 GPT-4 的智能代码生成工具 Cursor,它通过对话式编程显著提高了开发效率。接着,阐述了版本控制系统 Git 的重要性,包括记录代码修改、分支管理协作功能。然后,介绍了 Postman 作为 API 全生命周期管理工具,可创建、测试文档化 API,缩短前后端联调时间。再者,提到 SonarQube 这款代码质量管理工具,能自动扫描代码并检测潜在的质量问题。还介绍了 Docker 容器化工具,通过定义应用的运行环境依赖,确保环境一致性。最后,提及了线上诊断工具 Arthas 性能调优工具 JProfiler,分别用于生产环境排障性能优化。 适合人群:所有希望提高开发效率的程序员,尤其是有一定开发经验的软件工程师技术团队。 使用场景及目标:①选择合适的 IDE 提升编码速度代码质量;②利用 AI 编程助手加快开发进程;③通过 Git 实现高效的版本控制团队协作;④使用 Postman 管理 API 的全生命周期;⑤借助 SonarQube 提高代码质量;⑥采用 Docker 实现环境一致性;⑦运用 Arthas JProfiler 进行线上诊断性能调优。 阅读建议:根据个人或团队的需求选择适合的工具,深入理解每种工具的功能特点,并在实际开发中不断实践优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值