HBase 建表API使用

 


String table = "table";
Configuration conf = HBaseConfiguration.create();
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
//删除指定的表要先disable,然后在删除
admin.disableTable(TableName.valueOf(table));
admin.deleteTable(TableName.valueOf(table));


//创建表,设置指定的属性值
//org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine StoreEngine是在2.0以后的版本才有的
//设置列族的属性
ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes())
        .setCompressTags(true)
        .setInMemoryCompaction(MemoryCompactionPolicy.ADAPTIVE)
        .setMinVersions(2)
        .setTimeToLive(60 * 60 * 24 * 7)
        .setValue("COMPRESSION","SNAPPY")
        .setValue("hbase.hstore.engine.class","org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine")
        .setValue("hbase.hstore.blockingStoreFiles","60")
        .setValue("hbase.hstore.compaction.min","2")
        .setValue("hbase.hstore.compaction.max","60")
        .setCompactionCompressionType(Compression.Algorithm.SNAPPY)
        .build();

//设置表的属性
TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf(table))
        .setMemStoreFlushSize(256 * 1024 * 1024)
        .setColumnFamily(columnFamilyDescriptor)
        .setCompactionEnabled(true)
        .setMaxFileSize(1024 * 1024 * 1024)
        .setValue("COMPRESSION","SNAPPY")
        .setValue("hbase.hstore.engine.class","org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine")
        .setValue("hbase.hstore.blockingStoreFiles","60")
        .setValue("hbase.hstore.compaction.min","2")
        .setValue("hbase.hstore.compaction.max","60")
        .setValue("BLOCKSIZE","65536").build();

//设置预分区的值,rowkey安装字典序列排序,目前是按照开头数字是1-9分成10个分区
byte[][] splitKeys = {
        Bytes.toBytes("1"),
        Bytes.toBytes("2"),
        Bytes.toBytes("3"),
        Bytes.toBytes("4"),
        Bytes.toBytes("5"),
        Bytes.toBytes("6"),
        Bytes.toBytes("7"),
        Bytes.toBytes("8"),
        Bytes.toBytes("9")
};

//创建表
admin.createTable(td,splitKeys);

最后在WebUI上查看表的描述状态如下:

'test_db_zero9', {TABLE_ATTRIBUTES => {MAX_FILESIZE => '1073741824', MEMSTORE_FLUSHSIZE => '268435456', METADATA => {'BLOCKSIZE' => '65536', 'COMPACTION_ENABLED' => 'true', 'COMPRESSION' => 'SNAPPY', 'hbase.hstore.blockingStoreFiles' => '60', 'hbase.hstore.compaction.max' => '60', 'hbase.hstore.compaction.min' => '2', 'hbase.hstore.engine.class' => 'org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine'}}, {NAME => 'cf', MIN_VERSIONS => '2', TTL => '604800 SECONDS (7 DAYS)', COMPRESSION => 'SNAPPY', METADATA => {'hbase.hstore.engine.class' => 'org.apache.hadoop.hbase.regionserver.DateTieredStoreEngine', 'COMPRESS_TAGS' => 'true', 'hbase.hstore.blockingStoreFiles' => '60', 'COMPRESSION_COMPACT' => 'SNAPPY', 'hbase.hstore.compaction.min' => '2', 'IN_MEMORY_COMPACTION' => 'ADAPTIVE', 'hbase.hstore.compaction.max' => '60'}}

Region的信息如下:

HBase 建表API使用_第1张图片

在三台机器上的分布如下:

HBase 建表API使用_第2张图片

 

你可能感兴趣的:(HBase)