Hbase使用MultiTableOutputFormat实现多表输出MapReduce job
2016-03-08 11:13
686 查看
参考:https://bigdatabuzz.wordpress.com/2012/04/24/how-to-write-to-multiple-hbase-tables-in-a-mapreduce-job/
我们经常遇到需要更新多个表从一个map中输出以减少程序的运行时间。一个简单的方法是使用一个multitableoutputformat。
要配置这个工作,我们需要设置outputformatclass到multitableoutputformat.class。
为了使输出格式类识别的表名,我们需要通过表名为context.write关键。
下面给出一个例子:
我们经常遇到需要更新多个表从一个map中输出以减少程序的运行时间。一个简单的方法是使用一个multitableoutputformat。
要配置这个工作,我们需要设置outputformatclass到multitableoutputformat.class。
为了使输出格式类识别的表名,我们需要通过表名为context.write关键。
下面给出一个例子:
In Mapper :
public class Clean_TransMapper extends TableMapper<ImmutableBytesWritable, Put> { private ImmutableBytesWritable tbl1 = new ImmutableBytesWritable (Bytes.toBytes("outPutTable")); private ImmutableBytesWritable tbl2 = new ImmutableBytesWritable (Bytes.toBytes("outTempTable")); @Override protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { Put put = new Put(key.get()); //具体的逻辑就不给你们看了,以下举例 String value = Bytes.toString(row.get() ); if ("ONE".equals(value)) { context.write(tbl1, record); } else { context.write(tbl2, record); } } }
In the Driver class,
public final class MultiTableWriteDriver extends Configured implements Tool { ................. @Override public int run (final String[] args) { Configuration conf = HBaseConfiguration.create(); conf.set("mapreduce.job.jar","WeRroot\\WEB-INF\\lib\\hadoop-veiwhigh-0.0.1-SNAPSHOT.jar"); Job job = Job.getInstance(conf, "*******"); Scan scan = new Scan(); scan.setCaching(500); scan.setCacheBlocks(false); // don't set to true for MR jobs scan.setStartRow(Bytes.toBytes(setStartRow)); scan.setStopRow(Bytes.toBytes(setStopRow)); TableMapReduceUtil.addDependencyJars(job); TableMapReduceUtil.addDependencyJars(job.getConfiguration()); job.setJarByClass(MrCleanTransJobTool.class); // TableMapReduceUtil.initTableMapperJob(inPutTable, // 输入表 scan, // Scan instance to control CF and attribute selection *****.class, // mapper class ImmutableBytesWritable.class, // mapper output key Put.class, // mapper output value job); job.setOutputFormatClass (MultiTableOutputFormat.class); job.setNumReduceTasks(0); //设置为0表示不需要reduce return job.waitForCompletion(true) ? 0 : -1; } }
相关文章推荐
- 上传图片代码(chuantouxiang.php+touxiangchuli.php)
- php学习笔记之:环境配置(一)
- PHP 面向对面规范 PSR-0 小谈
- php.ini开放禁掉的函数以及重启服务的方法
- 让PHP更快的提供文件下载
- graph shortestpaths和pregel、mapReduceTriplets等中发送消息方向问题
- PHP数据加密技术之一---URL编码加密技术
- 正则表达式之PHP篇split 与 php函数explode
- 正则表达式之PHP篇replace,filter,grep
- zend studio输出中文乱码的问题
- PHP——文本编辑器
- 正则表达式之PHP篇match,match_all
- php5.5 连接SQL SERVER 【环境配置及DLL文件可下载】
- Yii2.0 对数据库 查询的一些简单的操作
- phpStorm程序在运行时,出现IE未响应的问题
- PHP PSR-2 代码风格规范 (中文版)
- thinkphp 文章列表批量删除 / 批量修改排序
- php调试——输出变量值
- Zend Framework教程之Resource Autoloading用法实例
- Zend Framework教程之Autoloading用法详解