在hbase里面,如果我们建表不预分区,那么一个表的数据都会被一个region处理,如果数据过多就会执行region的split,如果数据量很大这样会很费性能,所以最好我们先根据业务的数据量在建表的时候就能指定region个数并且进行预先分区,下面说说两种创建表并且建立预分区的方法。

  • 1.shell createTable并预分区:
hbase(main):002:0> create 'split01','cf1',SPLITS=>['1000000','2000000','3000000']
0 row(s) in 0.7880 seconds=> Hbase::Table - split01

利用上面的命令创建表,会预先创建4个regin,每个regin都有个startKey和endKey,第一个region没有startKey,最后一个没有endKey:
第一个region:“ to 1000000”
第二个region:“1000000 to 2000000”
第三个region:“2000000to 3000000”
第四个region:“3000000 to ”
如下图所示:

也可以使用如下命令创建:

hbase(main):003:0> create 'split02','cf1',SPLITS_FILE=>'/usr/java/split.txt'
0 row(s) in 1.2420 seconds=> Hbase::Table - split02

这里需要指定一个文件’/usr/java/split.txt’,文件里的内容如下(这里会创建6个region):

  • 2.javaAPI createTable并预分区:

在hbase包的Admin类中提供了4个create表的方法(前三个为同步创建,第四个为异步):

一.直接根据描述创建表

这里是直接根据表描述创建表,不指定分区。

  /*** Creates a new table. Synchronous operation.** @param desc table descriptor for table* @throws IllegalArgumentException if the table name is reserved* @throws MasterNotRunningException if master is not running* @throws org.apache.hadoop.hbase.TableExistsException if table already exists (If concurrent* threads, the table may have been created between test-for-existence and attempt-at-creation).* @throws IOException if a remote or network exception occurs*/void createTable(HTableDescriptor desc) throws IOException;

二.根据描述和region个数以及startKey以及endKey自动分配

根据表描述以及指定startKey和endKey和region个数创建表,这里hbase会自动创建region个数,并且会为你的每一个region指定key的范围,但是所有的范围都是连续的且均匀的,如果业务key的某些范围内数据量很多有的很少,这样就会造成数据的数据的倾斜,这样的场景就必须自己指定分区的范围,可以用第三种或者第四种方式预分区。

/*** Creates a new table with the specified number of regions.  The start key specified will become* the end key of the first region of the table, and the end key specified will become the start* key of the last region of the table (the first region has a null start key and the last region* has a null end key). BigInteger math will be used to divide the key range specified into enough* segments to make the required number of total regions. Synchronous operation.** @param desc table descriptor for table* @param startKey beginning of key range* @param endKey end of key range* @param numRegions the total number of regions to create* @throws IllegalArgumentException if the table name is reserved* @throws MasterNotRunningException if master is not running* @throws org.apache.hadoop.hbase.TableExistsException if table already exists (If concurrent* threads, the table may have been created between test-for-existence and attempt-at-creation).* @throws IOException*/void createTable(HTableDescriptor desc, byte[] startKey, byte[] endKey, int numRegions)throws IOException;

三.根据表的描述和自定义的分区设置创建表(同步)

根据表的描述和自定义的分区设置创建表,这个就可以自己自定义指定region执行的key的范围,比如:

byte[][] splitKeys = new byte[][] { Bytes.toBytes("100000"),
                Bytes.toBytes("200000"), Bytes.toBytes("400000"),
                Bytes.toBytes("500000") };

调用接口的时候splitKeys传入上面的值,那么他会自动创建5个region并且为之分配key的分区范围。
startKey,最后一个没有endKey:
第一个region:“ to 100000”
第二个region:“100000 to 200000”
第三个region:“200000 to 400000” 这里的key的跨度是其他的两倍(根据业务需求可以自己定义)
第四个region:“400000 to 500000”
第五个region:“500000 to ”

/*** Creates a new table with an initial set of empty regions defined by the specified split keys.* The total number of regions created will be the number of split keys plus one. Synchronous* operation. Note : Avoid passing empty split key.** @param desc table descriptor for table* @param splitKeys array of split keys for the initial regions of the table* @throws IllegalArgumentException if the table name is reserved, if the split keys are repeated* and if the split key has empty byte array.* @throws MasterNotRunningException if master is not running* @throws org.apache.hadoop.hbase.TableExistsException if table already exists (If concurrent* threads, the table may have been created between test-for-existence and attempt-at-creation).* @throws IOException*/void createTable(final HTableDescriptor desc, byte[][] splitKeys) throws IOException;

四.根据表的描述和自定义的分区设置创建表(异步)

同上面的三是一样的,不过是异步执行。

/*** Creates a new table but does not block and wait for it to come online. Asynchronous operation.* To check if the table exists, use {@link #isTableAvailable} -- it is not safe to create an* HTable instance to this table before it is available. Note : Avoid passing empty split key.** @param desc table descriptor for table* @throws IllegalArgumentException Bad table name, if the split keys are repeated and if the* split key has empty byte array.* @throws MasterNotRunningException if master is not running* @throws org.apache.hadoop.hbase.TableExistsException if table already exists (If concurrent* threads, the table may have been created between test-for-existence and attempt-at-creation).* @throws IOException*/void createTableAsync(final HTableDescriptor desc, final byte[][] splitKeys) throws IOException;

shell和javaAPI两种方式创建hbase表并预分区相关推荐

  1. 两种方式创建纯代码的 iOS 项目,不使用 storyboard

    两种方式 1. 去掉 storyboard,保留 SceneDelegate,改用纯代码开发 删除 .storyboard 文件 删除 Deployment Info -> Main Inter ...

  2. mysql空表_MySQL中两种快速创建空表的方式

    在MySQL中有两种方法 1.create table t_name select ... 2.create table t_name like ... 第一种会取消掉原来表的有些定义,且引擎是系统默 ...

  3. kubernetes创建资源的两种方式

    一.创建方式分类: 命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1.用 kubectl 命令行的方式直接创建,比如: kubectl run httpd-app --image ...

  4. k8s 创建资源的两种方式 - 每天5分钟玩转 Docker 容器技术(124)

    命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1. 用 kubectl 命令直接创建,比如: kubectl run nginx-deployment --image=nginx ...

  5. 第5篇K8S创建资源的两种方式

      一.创建方式分类: 命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1.用 kubectl 命令直接创建,比如: kubectl run httpd-app --image=r ...

  6. Java中实现多线程的两种方式之间的区别

    Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...

  7. java多线程区别_Java中实现多线程的两种方式之间的区别

    Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...

  8. java实例化字符串两种方式区别

    一:实例化字符串对象的两种方式的区别 这个知识点是面试中的一个经久不衰的问题,.也是一个比较麻烦的问题,对于许多同学来说也是难点,本次课我们会详细的分析.上次课说了创建字符串对象的两种方式:直接赋值( ...

  9. 创建安卓模拟器的两种方式及常用Android命令介绍

    创建安卓模拟器有以下两种方式: 1>通过图形界面创建,在Eclipse中单击Windows->Android Virtual Device Manager启动图形界面窗口 2>如果用 ...

最新文章

  1. 7.1 pdo 宝塔面板php_记宝塔面板中 PHP升级到 7.3.16安全版本概要
  2. pandas.read_csv(path_features_known_csv, header=None)的用法
  3. 服务器自动安全审计,用于Linux服务器的自动安全审计工具
  4. ***工具CC***的思路及防范方法
  5. 【渝粤题库】陕西师范大学163201 旅游科学引论作业(专升本)
  6. python手动回收内存哪家好_谈谈如何手动释放Python的内存
  7. 造谣无下限!众泰汽车被“破产”,官方声明:子虚乌有 已报案
  8. Recovering BST CodeForces - 1025D (区间dp, gcd)
  9. 17. Gradle编译其他应用代码流程(五) - 设置Task过程
  10. 陈丹琦NLP团队敢于挑战权威!谁说BERT只能Mask 15%?
  11. Linux安装Firefly
  12. php 谈谈我对session, cookies和jwt的理解
  13. Hadoop及RHadoop的初步尝试
  14. 分布式机器学习(一)之总体概述
  15. 带管理职位面试中遇到的常见经典问题的回答
  16. 已知圆上三点坐标求圆心
  17. esp32 物联网应用 01
  18. 计算机技术与软件专业技术资格(水平)考试岗位设置与描述
  19. Python Socket网络编程(二)局域网内和局域网与广域网的持续通信
  20. 《知识图谱》赵军 学习笔记

热门文章

  1. Android仿微信群聊头像合成
  2. 在node发布了一个身份证验证解析包cn-idcard-parse
  3. python利用turtle库绘制正方形绕一个顶点旋转
  4. 洛谷 魔法少女 python
  5. @所有人 “兔”个福气!飞桨兔年主题限量红包封面来咯,还有超多心动礼品等你来...
  6. 对象实例数据和对象类型数据
  7. html自动全屏js,js实现简单页面全屏
  8. 原生前端实现响应式个人简历网站设计(附源码)
  9. 蛇形走线用于什么方面,一文告诉你
  10. 少室山论道——武学修炼之道