MapReduce任务输出到redis中

主要包括redis连接池,重写FileOutputFormat函数。

redis连接池

/**
* redis 连接池
*/
public class RedisHelper {

    private static JedisPool jedisPool;

    static {
        init();
    }

    public synchronized static Jedis getJedis() {
        if (jedisPool != null) {
            Jedis resource = jedisPool.getResource();
            return resource;
        } else {
            return null;
        }
    }

    /**
     * 释放jedis资源
     *
     * @param jedis
     */
    public static void close(final Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    public static void destroy() {
        if (jedisPool != null) {
            jedisPool.destroy();
        }
    }

public static void putValue(Jedis jedis, String key, String value) {
        jedis.set(key, value);
    }

    private static void init() {
        Properties prop = new Properties();
        try (InputStream in = RedisHelper.class.getResourceAsStream("/database_config.properties")) {
            prop.load(in);
            String redisHost = prop.getProperty("redisHost");
            int redisPort = Integer.valueOf(prop.getProperty("redisPort"));
            String redisPassword = prop.getProperty("redisPassword");
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(Integer.valueOf(prop.getProperty("redisMaxTotal")));
            config.setMaxIdle(Integer.valueOf(prop.getProperty("redisMaxIdle")));
            config.setMinIdle(Integer.valueOf(prop.getProperty("redisMinIdle")));
            config.setMaxWaitMillis(Long.valueOf(prop.getProperty("redisMaxWaitMillis")));
            config.setTestOnBorrow(Boolean.parseBoolean(prop.getProperty("redisTestOnBorrow")));

            jedisPool = new JedisPool(config, redisHost, redisPort, 2000, redisPassword);
        } catch (IOException e) {
            log.error("Failed to initialize redis parameters. ", e);
            exit(1);
        }
    }

    private RedisHelper() {
    }
}

重写FileOutputFormat函数

public class ResultOutputFormat extends FileOutputFormat {

    /**
     * 定制一个RecordWriter类,每一条reduce处理后的记录,将该记录输出到数据库中
     */
    protected static class RedisRecordWriter extends RecordWriter {

        private Jedis jedis;

        public RedisRecordWriter(Jedis jedis) {
            this.jedis = jedis;
        }

        @Override
        public void write(K key, V value) throws IOException, InterruptedException {
            if (key == null || value == null) {
                return;
            }
            RedisHelper.putValue(jedis, key.toString(), value.toString());
        }

        @Override
        public void close(TaskAttemptContext context) throws IOException, InterruptedException {
            RedisHelper.close(jedis); //关闭链接
        }
    }

    @Override
    public RecordWriter getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
        Jedis jedis = RedisHelper.getJedis();
        return new RedisRecordWriter(jedis);
    }
}

然后在job设置处写入
job.setOutputFormatClass(ResultOutputFormat.class);

完美~~

你可能感兴趣的:(MapReduce任务输出到redis中)