Hbase 预分区建表

![](https://upload-images.jianshu.io/upload_images/9028759-735db08803d91f7c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

```

    private static Admin admin = null;

    private static final String user = "admin/[email protected]";

    private static final String keyPath = "/etc/security/keytabs/admin.keytab";

    static {

        System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");

        System.setProperty("sun.security.krb5.debug", "false");

        Configuration conf = new Configuration();

        conf.addResource("hbase-site.xml");

        conf.set("hbase.zookeeper.quorum", "storm4.demo.com,storm2.demo.com,storm3.demo.com");

        conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181");

        conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase-secure");

        conf.set("hbase.security.authentication", "kerberos");

        conf.set("hadoop.security.authentication", "kerberos");

        UserGroupInformation.setConfiguration(conf);

        try {

            UserGroupInformation.loginUserFromKeytab(user, keyPath);

        } catch (IOException e) {

            e.printStackTrace();

        }

        Connection connection= null;

        try {

            connection = ConnectionFactory.createConnection(conf);

        } catch (IOException e) {

            e.printStackTrace();

        }

        try {

            admin = connection.getAdmin();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public synchronized static Table getTable(String tableName) {

        try {

            return TABLE_MAP.get(tableName);

        } catch (ExecutionException e) {

            e.printStackTrace();

        }

        return null;

    }

    public synchronized static void createTable(String tableName) throws HBaseException {

        TableName table = TableName.valueOf(tableName);

        TableDescriptorBuilder tableDesc = TableDescriptorBuilder.newBuilder(table)

        tableDesc.setValue(TableDescriptorBuilder.SPLIT_POLICY, classOf[DelimitedKeyPrefixRegionSplitPolicy].getName)

        tableDesc.setValue(DelimitedKeyPrefixRegionSplitPolicy.DELIMITER_KEY, "|") //以|作为分区前缀

        ColumnFamilyDescriptor extCF = ColumnFamilyDescriptorBuilder.newBuilder("ext".getBytes()).setTimeToLive("90 DAYS").build();

        ColumnFamilyDescriptor deviceCF = ColumnFamilyDescriptorBuilder.newBuilder("device".getBytes()).setTimeToLive("90 DAYS").build();

        ColumnFamilyDescriptor locationCF = ColumnFamilyDescriptorBuilder.newBuilder("location".getBytes()).setTimeToLive("90 DAYS").build();

        tableDesc.setColumnFamilies(Arrays.asList(extCF, locationCF, deviceCF));

        try {

            val regionServers = 8

    val splitKeys = (0 until 256 by regionServers).map(Integer.toHexString).map(i => s"0$i".takeRight(2)).map(Bytes.toBytes).toArray

            admin.createTable(tableDesc.build(), splitKeys); //预分区

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    public static void main(String[] args) {

        try {

            createTable("DSP_LOG_TABLE");

        } catch (HBaseException e) {

            e.printStackTrace();

        }

    }

```

你可能感兴趣的:(Hbase 预分区建表)