hbaseHexStringSplit预分区
创建region,官⽅提供4种重载
hbase shell ⾥创建table 就不说了,简单资料也多,最⼤的坑是版本冲突,或包缺失
例
create 'ns_test:table_test', {NAME => 'cf', COMPRESSION => 'SNAPPY', BLOCKCACHE => 'false'}, {NUMREGIONS => 100, SPLITALGO => 'HexStringSplit'}
为了java代码和命令⾏创建⼀致使⽤
def createHbaseTable(admin: Admin, name: TableName)={
val table = new HTableDescriptor(targetTable)
val family = new HColumnDescriptor(cf)
family.setBlockCacheEnabled(true)
family.setCompressionType(Compression.Algorithm.SNAPPY)
table.addFamily(family)
val algo = new HexStringSplit()
val splits = algo.split(300)
}
注意 HexStringSplit 类,这个类是在 hbase-server包⾥
RegionSplitter.HexStringSplit()
但实际是很纯粹的算法,没有外部依赖
为了调这个⽅法,再引个包(如果打fat包,jar⽂件会⽐较⼤),没必要,可以直接提取这个类
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
<scope>provided</scope>
</dependency>
package org.apache.hadoop.hbase.util;
lemon.base.Preconditions;
import java.math.BigInteger;
public class HexStringSplit{
static final String DEFAULT_MIN_HEX = "00000000";
static final String DEFAULT_MAX_HEX = "FFFFFFFF";
String firstRow = "00000000";
BigInteger firstRowInt;
String lastRow;
BigInteger lastRowInt;
int rowComparisonLength;
public HexStringSplit() {
this.firstRowInt = BigInteger.ZERO;
this.lastRow = "FFFFFFFF";
this.lastRowInt = new BigInteger(this.lastRow, 16);
}
public byte[] split(byte[] start, byte[] end) {
BigInteger s = vertToBigInteger(start);
BigInteger e = vertToBigInteger(end);
Preconditions.checkArgument(!e.equals(BigInteger.ZERO));
vertToByte(this.split2(s, e));
}
public byte[][] split(int n) {
Preconditions.checkArgument(this.lastRowIntpareTo(this.firstRowInt) > 0, "last row (%s) is configured less than first row (%s)", new Object[]{this.lastRow, this.firstRow}); BigInteger range = this.lastRowInt.subtract(this.firstRowInt).add(BigInteger.ONE);
Preconditions.checkState(rangepareTo(BigInteger.valueOf((long)n)) >= 0, "split granularity (%s) is greater than the range (%s)", new Object[]{n, range});
BigInteger[] splits = new BigInteger[n - 1];
BigInteger sizeOfEachSplit = range.divide(BigInteger.valueOf((long)n));
for(int i = 1; i < n; ++i) {
splits[i - 1] = this.firstRowInt.add(sizeOfEachSplit.multiply(BigInteger.valueOf((long)i)));
}
vertToBytes(splits);
}
public byte[] firstRow() {
vertToByte(this.firstRowInt);
}
public byte[] lastRow() {
vertToByte(this.lastRowInt);
}
public void setFirstRow(String userInput) {
this.firstRow = userInput;
this.firstRowInt = new BigInteger(this.firstRow, 16);
}
public void setLastRow(String userInput) {
this.lastRow = userInput;
this.lastRowInt = new BigInteger(this.lastRow, 16);
}
public byte[] strToRow(String in) {
vertToByte(new BigInteger(in, 16));
}
public String rowToStr(byte[] row) {
StringBinary(row);
}
public String separator() {
return " ";
}
public void setFirstRow(byte[] userInput) {
this.firstRow = String(userInput);
}
public void setLastRow(byte[] userInput) {
this.lastRow = String(userInput);
}
public BigInteger split2(BigInteger a, BigInteger b) {
return a.add(b).divide(BigInteger.valueOf(2L)).abs();
}
public byte[][] convertToBytes(BigInteger[] bigIntegers) {
byte[][] returnBytes = new byte[bigIntegers.length][];
for(int i = 0; i < bigIntegers.length; ++i) {
hbase官方文档returnBytes[i] = vertToByte(bigIntegers[i]);
}
return returnBytes;
}
public static byte[] convertToByte(BigInteger bigInteger, int pad) {
String bigIntegerString = String(16);
bigIntegerString = org.apachemons.lang.StringUtils.leftPad(bigIntegerString, pad, '0');
Bytes(bigIntegerString);
}
public byte[] convertToByte(BigInteger bigInteger) {
return convertToByte(bigInteger, wComparisonLength);
}
public BigInteger convertToBigInteger(byte[] row) {
return row.length > 0 ? new String(row), 16) : BigInteger.ZERO;
}
@Override
public String toString() {
Class().getSimpleName() + " [" + wToStr(this.firstRow()) + "," + wToStr(this.lastRow()) + "]"; }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论