Configuration config = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin();
TableName tableName = TableName.valueOf("tb_step2");
TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
//定义列族对象
ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("data")).build();
//添加
tableDescriptor.setColumnFamily(family);
//创建表
admin.createTable(tableDescriptor.build());
Table table = connection.getTable(tableName);//获取Table对象
try {
Put put1 = new Put(Bytes.toBytes("row1"));
put1.addColumn(Bytes.toBytes("data"),Bytes.toBytes(String.valueOf(1)),Bytes.toBytes("张三丰"));
Put put2 = new Put(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("data"),Bytes.toBytes(String.valueOf(2)),Bytes.toBytes("张无忌"));
table.put(put1);
table.put(put2); //向表中添加数据
} finally {
//使用完了要释放资源
table.close();
}