Hbase建表(一个列族)
Admin admin = HbaseUtils.getAdmin();
// 建表 表名 列族
TableName tb_stu1 = TableName.valueOf("ns1:tb_stu");
// 表的描述构建器
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tb_stu1);
// 列族的构建器
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder =
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1"));
// 构建列族描述器
ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
// 列族 列族描述器
builder.setColumnFamily(columnFamilyDescriptor);
// 表的描述器
TableDescriptor descriptor = builder.build();
admin.createTable(descriptor);
admin.close();
建表(多个列族)
Admin admin = HbaseUtils.getAdmin();
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_teacher2"));
// 列族的构建器
ColumnFamilyDescriptorBuilder cf1Builder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1"));
// 设置列族的属性
cf1Builder.setTimeToLive(300);
cf1Builder.setMaxVersions(3);
ColumnFamilyDescriptor cf1 = cf1Builder.build();
ColumnFamilyDescriptorBuilder cf2Builder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf2"));
ColumnFamilyDescriptor cf2 = cf2Builder.build();
ColumnFamilyDescriptorBuilder cf3Builder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf3"));
ColumnFamilyDescriptor cf3 = cf3Builder.build();
List<ColumnFamilyDescriptor> ls = new ArrayList<>();
ls.add(cf1);
ls.add(cf2);
ls.add(cf3);
// 添加列族
tableDescriptorBuilder.setColumnFamilies(ls);
TableDescriptor descriptor = tableDescriptorBuilder.build();
admin.createTable(descriptor);
admin.close();
建表(预分region)
Admin admin = HbaseUtils.getAdmin();
// 表的描述构建器
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf("tb_pre_region"));
// 列族的构建器
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder =
ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1"));
// 构建列族描述器
ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
// 列族 列族描述器
builder.setColumnFamily(columnFamilyDescriptor);
TableDescriptor descriptor = builder.build();
/**
* 预分region表: 对表的数据进行合理的规划 , 将数据存储不同的region中 避免插入热点
* 参数一 表的描述器
* 参数二 预分region的splitKey
*/
byte[][] keys = new byte[][]{"g".getBytes(), "o".getBytes()};
admin.createTable(descriptor, keys);
admin.close();