HBase开发
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. } |
TableMapper输入的Key为RowKey的字节码数据,输入的Value为Result类型,表示一行数据。开发者需要重载TableMapper类的map方法来实现自己的Map任务。
TableReducer类
TableReducer类也是一个抽象类,继承自Reducer类,如下所示:
/* @param <KEYIN> The type of the input key. |
可见,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){ String sourceTable = argv[0]; Configuration config = HBaseConfiguration.create(); Scan scan = new Scan(); TableMapReduceUtil.initTableMapperJob( boolean b = job.waitForCompletion(true); public static class MyMapper private final IntWritable ONE = new IntWritable(1); public void map(ImmutableBytesWritable row, Result value, Context context) public static class MyTableReducer public void reduce(Text key, Iterable<IntWritable> values, Context context) |
最新文章
- win10常用帮助
- Mysql字符集知识总结
- Hbase split的三种方式和split的过程
- JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总
- Ubuntu下实用命令收集 --新增 删除 环境变量
- [未完成]plugin.xml文件
- Java基础之集合:概览
- stm32通过电调带动电机(可按键调速)
- UOJ#348 州区划分
- eee
- rocketmq在linux搭建双master遇到的坑
- BZOJ1423 : Optimus Prime
- runltp&;ltp-pan
- No symbol ";xxx"; in current context解决
- 在html页面添加一个隐藏域,并渲染一个需要保存的数值,在js中需要再获取,而不影响页面结构
- 前端框架 vue 和 react 的区别
- windows phone 手机解锁失败问题
- 344. Reverse String 最基础的反转字符串
- jira项目管理平台搭建
- Zookeeper选举算法原理