对于HBase的MapReduce性能提升方案之BulkLoad
我们知道,在第一次海量数据批量入库时,我们会选择使用BulkLoad的方式。
简单介绍一下BulkLoad原理方式:(1)通过MapReduce的方式,在Map或者Reduce端将输出格式化为HBase的底层存储文件HFile。(2)调用BulkLoad将第一个Job生成的HFile导入到相应的HBase表中。
ps:请注意(1)HFile方式是全部的载入方案里面是最快的,前提是:数据必须第一个导入,表示空的!假设表中已经有数据,HFile再次导入的时候,HBase的表会触发split切割操作。(2)终于输出结果,不管是Map还是Reduce,输出建议仅仅使用<ImmutableBytesWritable, KeyValue>。
如今我们開始正题:BulkLoad固然是写入HBase最快的方式,可是,假设我们在做业务分析的时候,而数据又已经在HBase的时候,我们採用普通的针对HBase的方式,例如以下demo所看到的:
import com.yeepay.bigdata.bulkload.TableCreator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.mapreduce.Job;
import org.apache.log4j.Logger; import java.io.IOException; public class HBaseMapReduceDemo { static Logger LOG = Logger.getLogger(HBaseMapReduceDemo.class); static class Mapper1 extends TableMapper<ImmutableBytesWritable, ImmutableBytesWritable> { @Override
public void map(ImmutableBytesWritable row, Result values, Context context) throws IOException { try {
// context.write(key, value);
} catch (Exception e) {
LOG.error(e);
}
}
} public static class Reducer1 extends TableReducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable> { public void reduce(ImmutableBytesWritable key, Iterable<ImmutableBytesWritable> values, Context context) throws IOException, InterruptedException {
try { Put put = new Put(key.get());
// put.add();
context.write(key, put); } catch (Exception e) {
LOG.error(e);
return ;
} // catch
} // reduce function
} // reduce class public static void main(String[] args) throws Exception { HBaseConfiguration conf = new HBaseConfiguration();
conf.set("hbase.zookeeper.quorum", "yp-name02,yp-name01,yp-data01");
conf.set("hbase.zookeeper.property.clientPort", "2181");
// conf.set(TableInputFormat.INPUT_TABLE,"access_logs");
Job job = new Job(conf, "HBaseMapReduceDemo");
job.setJarByClass(HBaseMapReduceDemo.class);
// job.setNumReduceTasks(2);
Scan scan = new Scan();
scan.setCaching(2500);
scan.setCacheBlocks(false); TableMapReduceUtil.initTableMapperJob("srcHBaseTableName", scan, Mapper1.class, ImmutableBytesWritable.class, ImmutableBytesWritable.class, job);
// TableCreator.createTable(20, true, "OP_SUM");
TableMapReduceUtil.initTableReducerJob("destHBasetableName", Reducer1.class, job);
System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
这个时候在对海量数据的插入过程中,会放生Spliter,写入速度很的,及其的慢。可是此种情况适合,对已有的HBase表进行改动时候的使用。
针对例如以下情况HBase -> MapReduce 分析 -> 新表,我们採用 (HBase -> MapReduce 分析 -> bulkload -> 新表)方式。
demo例如以下:
Mapper例如以下:
public class MyReducer extends Reducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable, KeyValue> { static Logger LOG = Logger.getLogger(MyReducer.class); public void reduce(ImmutableBytesWritable key, Iterable<ImmutableBytesWritable> values, Context context) throws IOException, InterruptedException {
try {
context.write(key, kv);
} catch (Exception e) {
LOG.error(e);
return;
} // catch
} // reduce function }
Reducer例如以下:
public class MyReducer extends Reducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable, KeyValue> { static Logger LOG = Logger.getLogger(MyReducer.class); public void reduce(ImmutableBytesWritable key, Iterable<ImmutableBytesWritable> values, Context context) throws IOException, InterruptedException {
try {
context.write(key, kv);
} catch (Exception e) {
LOG.error(e);
return;
} // catch
} // reduce function }
Job and BulkLoad:
public abstract class JobBulkLoad { public void run(String[] args) throws Exception {
try {
if (args.length < 1) {
System.err.println("please set input dir");
System.exit(-1);
return;
} String srcTableName = args[0];
String destTableName = args[1];
TableCreator.createTable(20, true, destTableName); // 设置 HBase 參数
HBaseConfiguration conf = new HBaseConfiguration();
conf.set("hbase.zookeeper.quorum", "yp-name02,yp-name01,yp-data01");
// conf.set("hbase.zookeeper.quorum", "nn01, nn02, dn01");
conf.set("hbase.zookeeper.property.clientPort", "2181"); // 设置 Job 參数
Job job = new Job(conf, "hbase2hbase-bulkload");
job.setJarByClass(JobBulkLoad.class);
HTable htable = new HTable(conf, destTableName); // 依据region的数量来决定reduce的数量以及每一个reduce覆盖的rowkey范围 // ----------------------------------------------------------------------------------------
Scan scan = new Scan();
scan.setCaching(2500);
scan.setCacheBlocks(false);
TableMapReduceUtil.initTableMapperJob(srcTableName, scan, MyMapper.class, ImmutableBytesWritable.class, ImmutableBytesWritable.class, job);
// TableMapReduceUtil.initTableReducerJob(destTableName, Common_Reducer.class, job); job.setReducerClass(MyReducer.class);
Date now = new Date();
Path output = new Path("/output/" + destTableName + "/" + now.getTime());
System.out.println("/output/" + destTableName + "/" + now.getTime()); HFileOutputFormat.configureIncrementalLoad(job, htable);
FileOutputFormat.setOutputPath(job, output);
HFileOutputFormat.configureIncrementalLoad(job, htable);
job.waitForCompletion(true); //----- 运行BulkLoad -------------------------------------------------------------------------------
HdfsUtil.chmod(conf, output.toString());
HdfsUtil.chmod(conf, output + "/" + YeepayConstant.COMMON_FAMILY);
htable = new HTable(conf, destTableName);
new LoadIncrementalHFiles(conf).doBulkLoad(output, htable);
System.out.println("HFile data load success!");
} catch (Throwable t) {
throw new RuntimeException(t);
}
} }
对于HBase的MapReduce性能提升方案之BulkLoad的更多相关文章
- mapreduce性能提升2
mapreduce性能提升2mapreduce性能提升2mapreduce性能提升2
- ElasticStack系列之十七 & 大文本搜索性能提升方案
1. 什么是大文本?具体是什么? 首先需要理解,ElasticSearch 建立索引完成全文检索的前提是将待检索的信息导入到 ElasticSearch 中.而有的信息对应的正文内容会非常的打,可能达 ...
- [转]XCache 3.0.0 发布,PHP 性能提升方案
From : http://www.oschina.net/news/34304/xcache-3-0-0 XCache 3.0.0 发布,该版本除了 bug 修复,对 XCache 管理页面做了很多 ...
- JS执行效率与性能提升方案
如果是追加字符串,最好使用s+=anotherStr操作,而不是要使用s=s+anotherStr.如果要连接多个字符串,应该少使用+=,如 s+=a;s+=b;s+=c;应该写成s+=a + b + ...
- VNF网络性能提升解决方案及实践
VNF网络性能提升解决方案及实践 2016年7月 作者: 王智民 贡献者: 创建时间: 2016-7-20 稳定程度: 初稿 修改历史 版本 日期 修订人 说明 1.0 20 ...
- 我是如何将一个老系统的kafka消费者服务的性能提升近百倍的
☞☞☞ 我是如何将一个老系统的kafka消费者服务的性能提升近百倍的 ☜☜☜ ○○○○○○○○○○○○○○○ 大家好,又见面了~ kafka作为一种高吞吐量的分布式发布订阅消息系统,在业务系统中被广泛 ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案
Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案 1. 业务场景 android+webview h5 css背景图性能提升1 2. ...
- 【转载】HBase 数据库检索性能优化策略
转自:http://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html 高性能 HBase 数据库 本文首先介绍了 HBase 数据库基本 ...
随机推荐
- 题解报告:hdu 1863 畅通工程
Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可 ...
- 给定的逗号分隔的数字字符串转换为Table
--将给定的逗号分隔的数字字符串转换为Table CREATE FUNCTION [dbo].[fu_Split](@strString nvarchar(4000)) RETURNS @Result ...
- CSS——盒子居中显示
嵌套中个的子盒子使用了绝对定位,父盒子使用了相对定位.那么子盒子如何居中显示: 1.距离左偏离50% 2.margin-right子盒子宽度的一半 <!DOCTYPE html> < ...
- Go 时间相关
>获取当前时间: t := time.Now() >获取当天开始.结束时间: tm1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, ...
- Logstash_Apache日志采集
[root@Cagios logstash-]# cat /usr/local/logstash-/logstash_agent.conf input { file { type => &quo ...
- PHP 数据库连接 (Mysql Mysqli PDO)
1.PHP与Mysql扩展(本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除),PHP原生的方式去连接数据库,是面向过程的 <?php $mysql_conf = array( 'hos ...
- Cookie, LocalStorage 与 SessionStorage说明
一.Cookie Cookie 大小限制为4KB左右,不适合大量数据的存储.因为它们由每个对服务器的请求来传递,这使得 cookie 速度很慢而且效率也不高.它的主要用途有保存登录信息,比如你 ...
- 学习Android
=========================================today start to learn Android================= 我们学习需要的软件: jd ...
- fork()调用使子进程先于父进程被调度
由于内核使用写时复制机制,fork之后父子进程是共享页表描述符的,如果让父进程先执行,那么有很大几率父进程会修改共享页表指向的数据,那么内核此时必须给父进程分配并复制新的页表供父进程修改使用,那么如果 ...
- PAT 1093. Count PAT's
The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...