HBase优化之—在建表时预先分regions 代码

来源:互联网 发布:如何注销淘宝账号 编辑:程序博客网 时间:2024/06/05 09:59
import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;import java.math.BigInteger;/** * */public class TestCreateHBase {    private static final Logger LOG = LoggerFactory.getLogger(TestCreateHBase.class);    public static void main(String[] args) throws IOException {        Configuration configuration = HBaseConfiguration.create();        configuration.set("hbase.zookeeper.quorum", "10.100.2.92,10.100.2.93");        HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);        HTableDescriptor tableDescriptor = new HTableDescriptor("rt_user_tags_test");        tableDescriptor.addFamily(new HColumnDescriptor("tags"));        byte[][] splits = getHexSplits("1", "100", 10);        createTable(hBaseAdmin, tableDescriptor, splits);        for (int i = 0; i < 100; i++) {            insertData("rt_user_tags_test", configuration, String.valueOf(i), "tags");        }    }    public static boolean createTable(HBaseAdmin hBaseAdmin, HTableDescriptor tableDescriptor, byte[][] splits) throws IOException {        try {            hBaseAdmin.createTable(tableDescriptor, splits);            return true;        } catch (Exception e) {            LOG.info("table" + tableDescriptor.getNameAsString() + "already exists!");            return false;        }    }    public static byte[][] getHexSplits(String startKey, String endKey, int numRegions) {        //startKey:001, endKey:100, 10regions[001, 010], [011, 020],...        byte[][] splits = new byte[numRegions - 1][];        BigInteger lowestKey = new BigInteger(startKey, 16);        BigInteger highestKey = new BigInteger(endKey, 16);        BigInteger rangge = highestKey.subtract(lowestKey);        BigInteger regionIncrement = rangge.divide(BigInteger.valueOf(numRegions));        lowestKey = lowestKey.add(regionIncrement);        for (int i = 0; i < numRegions - 1; i++) {            BigInteger key = lowestKey.add(regionIncrement.multiply(BigInteger.valueOf(i)));            byte[] b = String.format("%016x", key).getBytes();            splits[i] = b;        }        return splits;    }    public static void insertData(String tableName, Configuration configuration, String rowkey, String columnFamily) throws IOException {        System.out.println("start insert data ......");        HTable table = new HTable(configuration, "rt_user_tags_test");        Put put = new Put(rowkey.getBytes());        put.add(columnFamily.getBytes(), null, "Java".getBytes());        try {            table.put(put);        } catch (IOException e) {            e.printStackTrace();        }        System.out.println("end insert data ......");    }}
原创粉丝点击