import java.io.DataOutput;
import java.io.IOException;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader;
import org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileRecordReader;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
 
public class TestCombine extends Configured implements Tool {
    private static class ProvinceMapper extends
            Mapper<Object, Text, Text, Text> {
        @Override
        protected void map(Object key, Text value, Context context)
                throws IOException, InterruptedException {
            System.out.println("value : " + value + " Context " + context);
            context.write(value, value);
        }
    }
 
    private static class ProvinceReducer extends
            Reducer<Text, Text, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            for (Text va : values) {
                System.out.println("reduce " + key);
                context.write(key, key);
            }
        }
    }
     
    public static class CombineSequenceFileInputFormat<K, V> extends CombineFileInputFormat<K, V> { 
        @SuppressWarnings({ "unchecked", "rawtypes" }) 
        @Override 
        public RecordReader<K, V> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException { 
            return new CombineFileRecordReader((CombineFileSplit)split, context, CombineLineRecordReader.class); 
        } 
    } 
     
    public static class CombineLineRecordReader<K, V> extends RecordReader<K, V> { 
        private CombineFileSplit split; 
        private TaskAttemptContext context; 
        private int index; 
        private RecordReader<K, V> rr; 
       
        @SuppressWarnings("unchecked") 
        public CombineLineRecordReader(CombineFileSplit split, TaskAttemptContext context, Integer index) throws IOException, InterruptedException { 
            this.index = index;
            this.split = (CombineFileSplit) split; 
            this.context = context; 
       
            this.rr = (RecordReader<K, V>) ReflectionUtils.newInstance(LineRecordReader.class, context.getConfiguration()); 
        } 
       
        @SuppressWarnings("unchecked") 
        @Override 
        public void initialize(InputSplit curSplit, TaskAttemptContext curContext) throws IOException, InterruptedException { 
            this.split = (CombineFileSplit) curSplit; 
            this.context = curContext; 
       
            if (null == rr) { 
                rr = ReflectionUtils.newInstance(SequenceFileRecordReader.class, context.getConfiguration()); 
            } 
       
            FileSplit fileSplit = new FileSplit(this.split.getPath(index), 
                    this.split.getOffset(index), this.split.getLength(index), 
                    this.split.getLocations()); 
               
            this.rr.initialize(fileSplit, this.context); 
        } 
       
        @Override 
        public float getProgress() throws IOException, InterruptedException { 
            return rr.getProgress(); 
        } 
       
        @Override 
        public void close() throws IOException { 
            if (null != rr) { 
                rr.close(); 
                rr = null; 
            } 
        } 
       
        @Override 
        public K getCurrentKey() 
        throws IOException, InterruptedException { 
            return rr.getCurrentKey(); 
        } 
       
        @Override 
        public V getCurrentValue() 
        throws IOException, InterruptedException { 
            return rr.getCurrentValue(); 
        } 
       
        @Override 
        public boolean nextKeyValue() throws IOException, InterruptedException { 
            return rr.nextKeyValue(); 
        } 
    } 
 
     
    public int run(String[] args) throws Exception {
        Configuration conf = new Configuration();
         
        Job job = new Job(conf);
        job.setJobName("TestCombine");
        job.setJarByClass(TestCombine.class);
 
        job.setMapperClass(ProvinceMapper.class);
        job.setReducerClass(ProvinceReducer.class);
         
        job.setInputFormatClass(CombineSequenceFileInputFormat.class);
         
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
         
        String inpath = "/home/hadoop/tmp/combine";
        String outpath = "/home/hadoop/tmp/combineout";
        Path p = new Path(outpath);
         
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(p)){
            fs.delete(p);
        }
        FileInputFormat.addInputPaths(job, inpath);
        FileOutputFormat.setOutputPath(job, p);
 
        return job.waitForCompletion(true) ? 0 : 1;
    }
 
    public static void main(String[] args) throws Exception {
        int ret = ToolRunner.run(new TestCombine(), args);
        System.exit(ret);
    }
}

简单 实现CombineFileInputFormat的更多相关文章

  1. 简单实现CombineFileInputFormat

    import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...

  2. Hadoop CombineFileInputFormat实现原理及源码分析

    Hadoop适用于少量的大文件场景,而不是大量的小文件场景(这里的小文件通常指文件大小显著小于HDFS Block Size的文件),其主要原因是因为FileInputFormat在为这些小文件生成切 ...

  3. hadoop old API CombineFileInputFormat

    来自:http://f.dataguru.cn/thread-271645-1-1.html 简介 本文主要介绍下面4个方面 1.为什么要使用CombineFileInputFormat 2.Comb ...

  4. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  5. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  6. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  7. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  8. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  9. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

随机推荐

  1. 曾经的ps的图片

  2. msdn我告诉你

    http://msdn.itellyou.cn/ 微软旗下所有的msdn订阅软件资源 均为ed2k资源 Business Solutions MSDN Library 工具和资源 应用程序 开发人员工 ...

  3. 邮件发布google blogger 博客

    <?php $to = "@gmail.com";$subject = "Test mail";$message = "Hello! This ...

  4. Chapter 2 Open Book——12

    I called him in when dinner was ready, and he sniffed appreciatively as he walked into the room. 当晚饭 ...

  5. Java程序输出打字

    代码: 效果:

  6. PHP数组函数试题

    使用Ctrl+A查看答案 1.将数组的键名全部转换成小写和大写的函数是什么?答:array_change_key_case($array [,CASE_LOWER|CASE_UPPER]) 2.创建一 ...

  7. wpa_supplicant wpa_cli 的使用说明

    wpa_supplicant -d -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf -iwlan0 -B   会在/data/misc/wifi/下产生 ...

  8. TextureView+SurfaceTexture+OpenGL ES来播放视频(三)

    引自:http://www.jianshu.com/p/291ff6ddc164 做好的Demo截图 opengl-video 前言 讲了这么多,可能有人要问了,播放视频用个android封装的Vid ...

  9. alert()显示中文出现乱码

    1.如果是外部调用 <script language="javascript" type="text/javascript" src="tswc ...

  10. 第一个前台页面----xflow的页面

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ tagl ...