hbase HexStringSplit 預分區


創建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)
            admin.createTable(table,splits)
          }

注意 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;

import com.google.common.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);
        this.rowComparisonLength = this.lastRow.length();
    }

    
    public byte[] split(byte[] start, byte[] end) {
        BigInteger s = this.convertToBigInteger(start);
        BigInteger e = this.convertToBigInteger(end);
        Preconditions.checkArgument(!e.equals(BigInteger.ZERO));
        return this.convertToByte(this.split2(s, e));
    }

    public byte[][] split(int n) {
        Preconditions.checkArgument(this.lastRowInt.compareTo(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(range.compareTo(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)));
        }

        return this.convertToBytes(splits);
    }

    public byte[] firstRow() {
        return this.convertToByte(this.firstRowInt);
    }

    public byte[] lastRow() {
        return this.convertToByte(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);
        this.rowComparisonLength = this.lastRow.length();
    }

    
    public byte[] strToRow(String in) {
        return this.convertToByte(new BigInteger(in, 16));
    }

    
    public String rowToStr(byte[] row) {
        return Bytes.toStringBinary(row);
    }

    
    public String separator() {
        return " ";
    }

    
    public void setFirstRow(byte[] userInput) {
        this.firstRow = Bytes.toString(userInput);
    }

    
    public void setLastRow(byte[] userInput) {
        this.lastRow = Bytes.toString(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) {
            returnBytes[i] = this.convertToByte(bigIntegers[i]);
        }

        return returnBytes;
    }

    public static byte[] convertToByte(BigInteger bigInteger, int pad) {
        String bigIntegerString = bigInteger.toString(16);
        bigIntegerString = org.apache.commons.lang.StringUtils.leftPad(bigIntegerString, pad, '0');
        return Bytes.toBytes(bigIntegerString);
    }

    public byte[] convertToByte(BigInteger bigInteger) {
        return convertToByte(bigInteger, this.rowComparisonLength);
    }

    public BigInteger convertToBigInteger(byte[] row) {
        return row.length > 0 ? new BigInteger(Bytes.toString(row), 16) : BigInteger.ZERO;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + " [" + this.rowToStr(this.firstRow()) + "," + this.rowToStr(this.lastRow()) + "]";
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM