MapReduce接口

HBase提供了TableInputFormat、TableOutputFormat、TableMapper和TableReducer类来支持使用MapReduce框架处理HBase上的数据,并提供了TableMapReduceUtil类来初始化一个HBase-MapReduce任务。下面介绍一下这些接口。
TableInputFormat类
TableInputFormat负责将HBase数据按Region进行切片,该类继承自TableInputFormatBase类,TableInputFormatBase类实现了InputFormat类的大部分功能,TableInputFormat只是在其上添加了几个配置接口。TableInputFormat类通过setConf接口进行配置。如果需要自定义HBase的InputFormat类,可以通过重载TableInputFormatBase类的方法进行开发。
TableOutputFormat类
TableOutputFormat类负责将MapReduce任务输出的数据写入HBase表中。TableOutputFormat类同样通过setConf方法进行配置,如通过设置 TableOutputFormat.OUTPUT_TABLE来设置输出的目标表格。
TableMapper类
TableMapper类是一个抽象类,继承自Mapper类,如下所示:

/* @param <KEYOUT> The type of the key. 
@param <VALUEOUT> The type of the value. 
@see org.apache.hadoop.mapreduce.Mapper 
*/ 
public abstract class TableMapper<KEYOUT, VALUEOUT> 
extends Mapper<ImmutableBytesWritable, Result, KEYOUT, VALUEOUT> {

}

TableMapper输入的Key为RowKey的字节码数据,输入的Value为Result类型,表示一行数据。开发者需要重载TableMapper类的map方法来实现自己的Map任务。
TableReducer类
TableReducer类也是一个抽象类,继承自Reducer类,如下所示:

/* @param <KEYIN> The type of the input key. 
@param <VALUEIN> The type of the input value. 
@param <KEYOUT> The type of the output key. 
@see org.apache.hadoop.mapreduce.Reducer 
*/ 
public abstract class TableReducer<KEYIN, VALUEIN, KEYOUT> 
extends Reducer<KEYIN, VALUEIN, KEYOUT, Writable> { 
}

可见,TableReducer与普通的Reducer类没有区别,开发者需要重载TableReducer类的reduce方法来实现自己的Map任务。
TableMapReduceUtil类
TableMapReduceUtil是一个辅助类,用来简化一个HBase-MapReduce作业的配置过程。该类提供了多个方法来初始化map任务和reduce任务。常见的方法如下 :
static void initTableMapperJob(byte[] table, Scan scan, 
Class<? extends TableMapper> mapper, 
Class<?> outputKeyClass, 
Class<?> outputValueClass,
org.apache.hadoop.mapreduce.Job job)
static void initTableReducerJob(String table, 
Class<? extends TableReducer> reducer, 
org.apache.hadoop.mapreduce.Job job)
void setNumReduceTasks(String table,
org.apache.hadoop.mapreduce.Job job)

MapReduce接口示例

下面给出了一个HBase-MapReduce应用实例,该例将一张表中不同的值进行统计,将结果输出到另一张表中。

public class HBaseMapReduceDemo {

public static void main(String[] argv){

if(argv.length < 2){ 
System.exit(0); 
}

String sourceTable = argv[0]; 
String targetTable = argv[1];

Configuration config = HBaseConfiguration.create(); 
Job job = new Job(config,"ExampleReadWrite"); 
job.setJarByClass(HBaseMapReduceDemo.class);

Scan scan = new Scan(); 
scan.setCaching(500); // 在MR作业中,适当设置该值可提升性能 
scan.setCacheBlocks(false); // 在MR作业中,应总为false

TableMapReduceUtil.initTableMapperJob
sourceTable, // 输入表 
scan, // 扫描表配置 
MyMapper.class, // mapper类 
Text.class, // mapper输出Key 
IntWritable.class, // mapper输出Value 
job); 
TableMapReduceUtil.initTableReducerJob
targetTable, // 输出表 
MyTableReducer.class, // reducer类 
job); 
job.setNumReduceTasks(0);

boolean b = job.waitForCompletion(true); 
if (!b) { 
throw new IOException("error with job!"); 

}

public static class MyMapper 
extends TableMapper<Text, IntWritable> {

private final IntWritable ONE = new IntWritable(1); 
private Text text = new Text();

public void map(ImmutableBytesWritable row, Result value, Context context) 
throws IOException, InterruptedException { 
String val = new String(value.getValue(Bytes.toBytes("cf"), 
Bytes.toBytes("attr1"))); 
text.set(val); 
context.write(text, ONE); 

}

public static class MyTableReducer 
extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {

public void reduce(Text key, Iterable<IntWritable> values, Context context) 
throws IOException, InterruptedException { 
int i = 0; 
for (IntWritable val : values) { 
i += val.get(); 

Put put = new Put(Bytes.toBytes(key.toString())); 
put.add(Bytes.toBytes("cf"), Bytes.toBytes("count"), Bytes.toBytes); 
context.write(null, put); 


}

HBase开发的更多相关文章

  1. eclipse+hbase开发环境部署

    一.前言 1. 前提 因为hbase的运行模式是伪分布式,需要用到hdfs,所以在此之前,我已经完成了hadoop-eclipse的开发环境搭建,详细看另一篇文章:hadoop开发环境部署——通过ec ...

  2. HBase开发错误记录(一):java.net.UnknownHostException: unknown host: master

    windows下开发HBase应用程序.HBase部署在linux环境中, 在执行调试时可能会出现无法找到主机,类似异常信息例如以下: java.net.UnknownHostException: u ...

  3. eclipse+HBASE开发环境搭建(已实践)

    开发准备: jdk1.8.45 hbase-1.2.2(windows下和linux个留一份) hadoop-2.7.2(linux一份) Linux系统(centos或其它) Hadoop安装环境 ...

  4. HBase 开发环境搭建(Eclipse\MyEclipse + Maven)

    写在前面的话 首先, 搭建基于MyEclipse的Hadoop开发环境 相信,能看此博客的朋友,想必是有一定基础的了.我前期写了大量的基础性博文.可以去补下基础. 比如, CentOS图形界面下如何安 ...

  5. HBase学习3(win下使用Eclipse搭建hbase开发环境)

    第一步:创建一个java project命名为wujiadong_hbase 第二步:在该工程下创建一个folder命名为lib(储存依赖的jar包) 第三步:将集群中的hbase安装目录下载一份到w ...

  6. HBase开发错误记录(java.net.UnknownHostException: unknown host: hadoop111)

    windows下开发HBase应用程序,HBase部署在linux环境中, 在运行调试时可能会出现无法找到主机,类似异常信息如下: java.net.UnknownHostException: unk ...

  7. hbase开发实例

    1.put/checkAndPut package com.testdata; import java.io.IOException; import org.apache.hadoop.conf.Co ...

  8. HBase学习(十四)LINUX下用Eclipse构建HBase开发环境

    Eclipse,HBase版本号眼下没有发现须要特别指定 1:从HBase集群中复制一份Hbase部署文件,放置在开发端某一文件夹下(如在/app/hadoop/hbase096文件夹下). 2:在e ...

  9. 基于MapReduce的HBase开发

    在伪分布式模式和全分布式模式下 HBase 是架构在 HDFS 上的,因此完全可以将MapReduce 编程框架和 HBase 结合起来使用.也就是说,将 HBase 作为底层“存储结构”, MapR ...

随机推荐

  1. JSONP简单示例

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  2. babel ---- presets字段设定转码规则

    presets字段设定转码规则,官方提供以下的规则集,你可以根据需要安装. # ES2015转码规则 $ npm install --save-dev babel-preset-es2015 # re ...

  3. codevs——2956 排队问题

    2956 排队问题  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3 ...

  4. android添加桌面悬浮窗

    1. 添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 2. ...

  5. ios 6.0模拟器页面调出pop窗口消失后无法使用键盘

    ios 6模拟器上,点击事件调用出pop窗口,这个窗口新创建了window,在pop窗口消失的函数中使用了makeKeyWindow,这个是将要显示的window放到最前端.发现 屏蔽这个方法后可以了 ...

  6. 织梦dede如何去除Power by DedeCms

    自从dedecms织梦系统更新到6.7日的版本,底部版权信息调用标签{dede:global.cfg_powerby/}会自动加上织梦官方的链接[Power by DedeCms ],想必很多新用户使 ...

  7. 调用tf.softmax_cross_entropy_with_logits函数出错解决

    原来这个函数,不能按以前的方式进行调用了,只能使用命名参数的方式来调用.原来是这样的: tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y ...

  8. [UIDevice currentDevice]

    获取iphone的系统信息使用[UIDevice currentDevice],信息例如以下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone O ...

  9. Kaggle的Outbrain点击预测比赛分析

    https://yq.aliyun.com/articles/293596 https://www.kaggle.com/c/outbrain-click-prediction https://www ...

  10. react className 有多个值时的处理 / react 样式使用 百分比(%) 报错

    1.react className 有多个值时的处理 <fieldset className={`${styles.formFieldset} ${styles.formItem}`}> ...