Hbase Region 均衡设置

2022年6月14日12:04:41 发表评论 1,995 views

package hbaseapi;
        import org.apache.hadoop.conf.Configuration;
        import org.apache.hadoop.hbase.*;
        import org.apache.hadoop.hbase.client.*;
        import org.apache.hadoop.hbase.util.Bytes;

        import java.io.IOException;
        import java.text.DecimalFormat;
        import java.util.ArrayList;
        import java.util.Iterator;
        import java.util.List;
        import java.util.TreeSet;

/**
 * 尝试将表改为绝对均衡(修改key值前面的两个数字为有序数字而不是随机数)
 */
public class absBalance{
    public static Configuration conf;
    static Connection connection = null;
    static HBaseAdmin admin = null;


    static {

        try {
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "bigdata166");
            conf.set("hbase.zookeeper.property.clientport", "2181");

            connection = ConnectionFactory.createConnection(conf);
            admin = (HBaseAdmin) connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }




    /**
     * 分区键
     *
     * @param regions region个数
     * @return splitKeys
     */
    private static byte[][] genSplitKeys(int regions) {
        //第一步:生成分区键
        String[] keys = new String[regions];
        //格式化分区键的形式  00 01 02
        DecimalFormat df = new DecimalFormat("00");
        for (int i = 0; i < regions; i++) {
            keys[i] = df.format(i*10) + "";
        }


        //第二步:分区键排序
        //排序 保证你这个分区键是有序得
        TreeSet<byte[]> treeSet = new TreeSet<>(Bytes.BYTES_COMPARATOR);
        for (int i = 0; i < regions; i++) {
            treeSet.add(Bytes.toBytes(keys[i]));
        }


        //第三步:迭代输出
        byte[][] splitKeys = new byte[regions][];
        Iterator<byte[]> iterator = treeSet.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            byte[] next = iterator.next();
            splitKeys[index++] = next;
        }

        return splitKeys;
    }

    public static boolean isTableExist(String tableName) throws IOException {
        return admin.tableExists(tableName);
    }

    /**
     * (判断表)创建表
     *
     * @param tableName    表名
     * @param columnFamily 列族<=3 多个  可变
     */
    public static void createRegionsTable(String tableName, int regions, String... columnFamily) throws Exception {

        if (isTableExist(tableName)) {
            System.out.println("表已存在");
        } else {
            //通过表描述器
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //循环创建列族
            for (String cf : columnFamily) {
                HColumnDescriptor hcd = new HColumnDescriptor(cf);
                descriptor.addFamily(hcd);
            }

            admin.createTable(descriptor, genSplitKeys(regions)); //  根据二维数组创建region
            System.out.println("表创建成功");

        }

    }


    private static String getRandomNumber() {
        return (Math.random() + "").substring(2, 4);
    }

    private static List<Put> batchPut() {
        List<Put> list = new ArrayList<Put>();
        for (int i = 1; i <= 100000; i++) {
            String frs = Math.ceil((double) i/1000)+"";
            byte[] rowkey = Bytes.toBytes(frs + "-" + System.currentTimeMillis() + "-" + i);
            Put put = new Put(rowkey);
            put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("name"), Bytes.toBytes("andy" + i));
            list.add(put);
        }


        return list;
    }

    /**
     * 获取此row在哪一个region上
     *
     * @param tableName
     * @param row
     * @throws IOException
     */
    private static void getRegionLocation(String tableName, byte[] row) throws IOException {
        RegionLocator location = connection.getRegionLocator(TableName.valueOf(tableName));
        HRegionInfo rg = location.getRegionLocation(row).getRegionInfo();
        String regionname = Bytes.toString(rg.getRegionName());
        String strkey = Bytes.toString(rg.getStartKey());
        String endkey = Bytes.toString(rg.getEndKey());
        System.out.println(regionname);
        System.out.println(strkey);
        System.out.println(endkey);
    }



    public static void main(String[] args) throws Exception {
//////////////////////////////////////////////////////测试代码/////////////////////////////////////////////
        byte[][] test = genSplitKeys(10);
        for (byte[] bytes : test) {
            System.out.println(Bytes.toString(bytes));
        }

        //getRow("student", "1001");

//        getRowQualifier("student", "1001", "cf1", "sex");


//        initNameSpace("ccc");
//        dropTable("cndy");
        createRegionsTable("fndy", 10, "cf1");
        Table table = connection.getTable(TableName.valueOf("fndy"));
        table.put(batchPut());  // 自动将数据均匀存储到里面
        //genSplitKeys(6);
//        getRegionLocation("cndy", Bytes.toBytes("75-1649578056056-90350"));
//        getRegionLocation("cndy", Bytes.toBytes("97-xxx"));
//        getRegionLocation("cndy", Bytes.toBytes("97-999"));
//        getRegionLocation("cndy", Bytes.toBytes("97-777"));
//        getRegionLocation("cndy", Bytes.toBytes("44-777"));
//        getRegionLocation("cndy", Bytes.toBytes("44***777"));
//        getRegionLocation("cndy", Bytes.toBytes("s4***777"));
//        getRegionLocation("cndy", Bytes.toBytes("24***777"));
    }
}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: