zoukankan      html  css  js  c++  java
  • 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()) + "]";
        }
    }
    
  • 相关阅读:
    从GoogleClusterData统计每个用户的使用率、平均每次出价
    简单的大众点评爬虫
    导入GoogleClusterData到MySQL
    高斯分布(正态分布)
    解决Mysql无法导入存在null数据的问题
    使用Python操作MySQL
    [Vue warn]: Duplicate keys detected: '0'. This may cause an update error.
    css多行超出时,超出高度,显示省略号
    mock.js学习之路(二)easy-mock(Vue中使用)
    mock.js学习之路一(Vue中使用)
  • 原文地址:https://www.cnblogs.com/zihunqingxin/p/14916148.html
Copyright © 2011-2022 走看看