---
title: "Hadoop学习三十九：HBase与MapReduce集成"
author: "zy19982004"
date: "2014-05-19T14:09:39Z"
tags: []
categories: ["企业架构"]
slug: "hadoop-hbase-mapreduce"
draft: false
lastmod: "2014-05-19T16:17:29Z"
id: 2068217
url: "https://zy19982004.iteye.com/blog/2068217"
---

**一.概述**

     HBase与MapReduce集成时，有以下三种情形

1. HBase作为数据流向。
2. HBase作为数据源。
3. HBase作为数据源和数据流向。

     阅读本文前，最好先了解[http://zy19982004.iteye.com/blog/2068112](/blog/2068112)    

 

**二.HBase作为数据流向**

      HBase作为数据流向时，如从Hdfs里向HBase里导入数据，可以有下列方式

1. map里直接调用HBase Api，往HBase插入数据。此时job.setNumReduceTasks(0)，并且job.setOutputFormatClass(NullOutputFormat.class)
2. 使用TableOutputFormat，TableOutputFormat的RecordWriter会直接往HBase写数据。
   
   1. 可以在map阶段就写入。此时job.setNumReduceTasks(0)。
   2. 也可以有reduce来写入如IdentityTableReducer。
3. 使用BulkLoad，HFileOutputFormat.configureIncrementalLoad(job, htable); 的背后job.setOutputFormatClass(HFileOutputFormat.class);

 

**三.HBase作为数据源**

      HBase作为数据源，如分析HBase里的数据

      自定义mapper继承TableMapper，实际以Result作为数据源，map和reduce阶段按业务逻辑来即可。

 

**四.HBase作为数据源和数据流向**

     HBase作为数据源和数据流向，如将一个HBase表拆分为两个HBase表。mapper继承TableMapper，main方法里TableMapReduceUtil.initTableMapperJob后至少可以以下三种方式处理

1. map阶段处理好数据，调用HBase Api插入到新HBase表。此时job.setNumReduceTasks(0)，并且job.setOutputFormatClass(NullOutputFormat.class)。
2. map阶段处理好数据，由TableOutputFormat写入到Hbase。
   
   1. 可以在map阶段就写入。此时job.setNumReduceTasks(0)。
   2. 也可以有reduce来写入如IdentityTableReducer。此时需要TableMapReduceUtil.initTableReducerJob。

**五.源代码**

      [http://platform-components.googlecode.com/svn/trunk/SourceCode/study-hadoop/src/main/java/com/jyz/study/hadoop/hbase/mapreduce/](http://platform-components.googlecode.com/svn/trunk/SourceCode/study-hadoop/src/main/java/com/jyz/study/hadoop/hbase/mapreduce/) 目录下的几个java代码可以很好的说明上述任一情况。