最近需要为一些数据增加随机读的功能,于是采用生成HFile再bulk load进HBase的方式。

运行的时候map很快完成,reduce在sort阶段花费时间很长,reducer用的是KeyValueSortReducer而且只有一个,这就形成了单reducer全排序的瓶颈。于是就想着采用TotalOrderPartitioner使得MR Job可以有多个reducer,来提高并行度解决这个瓶颈。

于是动手写代码,不仅用了TotalOrderPartitioner,还使用InputSampler.RandomSampler生成分区文件。但执行时碰到问题,查资料时无意发现HFileOutputFormat内部是使用TotalOrderPartitioner来进行全排序的,

 public static void configureIncrementalLoad(Job job, HTable table)throws IOException {Configuration conf = job.getConfiguration();Class<? extends Partitioner> topClass;try {topClass = getTotalOrderPartitionerClass();} catch (ClassNotFoundException e) {throw new IOException("Failed getting TotalOrderPartitioner", e);}job.setPartitionerClass(topClass);......

分区文件的内容就是各region的startKey(去掉最小的),

private static void writePartitions(Configuration conf, Path partitionsPath,List<ImmutableBytesWritable> startKeys) throws IOException {if (startKeys.isEmpty()) {throw new IllegalArgumentException("No regions passed");}// We're generating a list of split points, and we don't ever// have keys < the first region (which has an empty start key)// so we need to remove it. Otherwise we would end up with an// empty reducer with index 0//没有哪个rowkey会排在最小的startKey之前,所以去掉最小的startKeyTreeSet<ImmutableBytesWritable> sorted =new TreeSet<ImmutableBytesWritable>(startKeys);ImmutableBytesWritable first = sorted.first();//如果最小的region startKey不是“法定”的最小rowkey,那就报异常if (!first.equals(HConstants.EMPTY_BYTE_ARRAY)) {throw new IllegalArgumentException("First region of table should have empty start key. Instead has: "+ Bytes.toStringBinary(first.get()));}sorted.remove(first);// Write the actual fileFileSystem fs = partitionsPath.getFileSystem(conf);SequenceFile.Writer writer = SequenceFile.createWriter(fs,conf, partitionsPath, ImmutableBytesWritable.class, NullWritable.class);try {//写入分区文件中 for (ImmutableBytesWritable startKey : sorted) {writer.append(startKey, NullWritable.get());}} finally {writer.close();}}

因为我的表都是新表,只有一个region, 所以肯定是只有一个reducer了。

既然如此,使用HFileOutputFormat时reducer的数量就是HTable的region数量,如果使用bluk load HFile的方式导入巨量数据,最好的办法是在定义htable是就预先定义好各region。这种方式其实叫Pre-Creating Regions,PCR还能带来些别的优化,比如减少split region的操作:淘宝有些优化就是应用PCR并且关闭自动split,等到系统空闲时再手动split,这样可以保证系统繁忙时不会再被split雪上加霜。

关于Pre-Creating Regions: http://hbase.apache.org/book.html#precreate.regions

11.7.2. Table Creation: Pre-Creating Regions Tables in HBase are initially created with one region by default. For bulk imports, this means that all clients will write to the same region until it is large enough to split and become distributed across the cluster. A useful pattern to speed up the bulk import process is to pre-create empty regions. Be somewhat conservative in this, because too-many regions can actually degrade performance. There are two different approaches to pre-creating splits. The first approach is to rely on the default HBaseAdmin strategy (which is implemented in Bytes.split)...

byte[] startKey = ...;       // your lowest keuy
byte[] endKey = ...;           // your highest key
int numberOfRegions = ...;    // # of regions to create
admin.createTable(table, startKey, endKey, numberOfRegions);

And the other approach is to define the splits yourself...

byte[][] splits = ...;   // create your own splits
admin.createTable(table, splits);

转载于:https://www.cnblogs.com/aprilrain/archive/2013/03/27/2985064.html

HFileOutputFormat与TotalOrderPartitioner相关推荐

  1. (转载)Hadoop常用SDK系列五 TotalOrderPartitioner

    在0.19.0以前的版本中,Hadoop自身并没有提供全排序的solution,如果使用缺省的partitioner(HashPartitioner)每个reducer的输出自身是有序的,但是多个re ...

  2. HBase 数据导入功能实现方式解释

    https://www.ibm.com/developerworks/cn/opensource/os-cn-data-import/index.html 预备知识:启动 HBase 清单 1. 修改 ...

  3. hbase 源代码分析 (17)MapReduce 过程

    这一章节主要讲解Hbase的内部的Mapreduce过程. 1)hbase 可以作为数据源, 2)hbase作为输出源 3)hbase数据转移. 1)hbase 可以作为数据源,Export.java ...

  4. hbase批量入库的总结

    最近这一段时间一直在研究hbase的批量入库,看似简单的问题其实埋着无数的坑...... 接下来就把我遇到的一些问题和解决的办法分享给大家,希望能让那些新接触到的人不至于像我一样走这么多弯路. hba ...

  5. Hive对接Hbase

    大家: 好! 因项目实际需要,要求将hive中的数据对接入hbase中.在网上看的一篇博文的基础上,加上自己的理解以及相关的操作步骤,以及常见的几个错误,整理了此篇博客,希望对大家有所帮助. Bulk ...

  6. hive 的分隔符、orderby sort by distribute by的优化

    一.Hive 分号字符 分号是SQL语句结束标记,在HiveQL中也是,可是在HiveQL中,对分号的识别没有那么智慧,比如: select concat(cookie_id,concat(';',' ...

  7. Hadoop生成HFile直接入库HBase心得

    转载请标明出处:http://blackwing.iteye.com/blog/1991380 hbase自带了ImportTsv类,可以直接把tsv格式(官方教材显示,是\t分割各个字段的文本格式) ...

  8. spark hbase

    1 配置 1.1 开发环境: HBase:hbase-1.0.0-cdh5.4.5.tar.gz Hadoop:hadoop-2.6.0-cdh5.4.5.tar.gz ZooKeeper:zooke ...

  9. MapReduce 进阶:Partitioner 组件

    概述 Partitioner 组件可以让 Map 对 Key 进行分区,从而将不同分区的 Key 交由不同的 Reduce 处理.如果这么说让你觉得有一些笼统的话,那么本文可能很适合你,因为本文会依据 ...

最新文章

  1. 浅谈微博精准推荐——用户行为挖掘与相似用户挖掘
  2. 一个新手学linux!
  3. Spring-Boot使用RedisCluster
  4. Java 到底有没有析构函数呢?
  5. 基线长度中误差的计算_电子战支援实施中的测向技术
  6. java 什么是迭代器
  7. MUI 图标显示不出来 - 分析篇
  8. iptables 小提升
  9. 大数据笔试面试题(转载)
  10. CMMI-V2.0真题模拟(1)
  11. 计算机核心期刊新排名(八大学报)
  12. 『危机领导力』告诉我们如何带好团队
  13. php html转ubb,PHP HTML转UBB函数
  14. 微信小程序 保存图片 wx.saveImageToPhotosAlbum
  15. 在Android Studio 上为项目添加Git版本控制
  16. linux python 路径获取
  17. 广东省污水处理厂数字孪生平台建模_三维可视化平台_吉优赛维数字孪生_三维激光扫描_BIM建模
  18. 区分浏览器,判断浏览器版本
  19. TIFS_2013_Empirical Evaluation and New Design for Fighting Evolving Twitter Spammers
  20. 原码、反码、补码、机器数,真值,概念介绍

热门文章

  1. java项目中的classpath
  2. mac vscode 实用快捷键
  3. 算法 --- 插入排序的JS实现
  4. OPENCV-3 学习笔记
  5. centos6.4与win7双系统时间同步错误解决办法
  6. @MySQL的存储引擎
  7. VS2008系列培训教程之四:What's new in C# 3.0 Visual Basic 9.0
  8. 用DataTable.Merge()解决了一个排序问题
  9. Product文本格式说明
  10. 【TensorFlow】——expand_dims、transpose、squeeze