本文是HBase Java编程(2)——创建表、插入数据、删除数据。
创建一个名为WATER_BILL的表,包含一个列蔟C1。
实现步骤:
1.判断表是否存在——存在,则退出
2.使用TableDescriptorBuilder.newBuilder构建表描述构建器
3.使用ColumnFamilyDescriptorBuilder.newBuilder构建列蔟描述构建器
4.构建列蔟描述,构建表描述
5.创建表
参考代码:
// 创建一个名为WATER_BILL的表,包含一个列蔟C1
@Test
public void createTableTest() throws IOException {// 表名String TABLE_NAME = "WATER_BILL";// 列蔟名String COLUMN_FAMILY = "C1";// 1. 判断表是否存在if(admin.tableExists(TableName.valueOf(TABLE_NAME))) {return;}// 2. 构建表描述构建器TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(TABLE_NAME));// 3. 构建列蔟描述构建器ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(COLUMN_FAMILY));// 4. 构建列蔟描述ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();// 5. 构建表描述// 添加列蔟tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);TableDescriptor tableDescriptor = tableDescriptorBuilder.build();// 6. 创建表admin.createTable(tableDescriptor);
}
实现步骤:
1.判断表是否存在
2.如果存在,则禁用表
3.再删除表
参考代码:
// 删除表
@Test
public void dropTable() throws IOException {// 表名TableName tableName = TableName.valueOf("WATER_BILL");// 1. 判断表是否存在if(admin.tableExists(tableName)) {// 2. 禁用表admin.disableTable(tableName);// 3. 删除表admin.deleteTable(tableName);}
}
创建包
1.在 test 目录中创建 cn.itcast.hbase.data.api_test 包
2.创建DataOpTest类
初始化Hbase连接
在@BeforeTest中初始化HBase连接,在@AfterTest中关闭Hbase连接。
参考代码:
public class DataOpTest {private Configuration configuration;private Connection connection;@BeforeTestpublic void beforeTest() throws IOException {configuration = HBaseConfiguration.create();connection = ConnectionFactory.createConnection(configuration);}@AfterTestpublic void afterTest() throws IOException {connection.close();}
}
插入姓名列数据
在表中插入一个行,该行只包含一个列。
ROWKEY | 姓名(列名:NAME) |
---|---|
4944191 | 登卫红 |
实现步骤:
1.使用Hbase连接获取Htable
2.构建ROWKEY、列蔟名、列名
3.构建Put对象(对应put命令)
4.添加姓名列
5.使用Htable表对象执行put操作
6.关闭Htable表对象
参考代码:
@Test
public void addTest() throws IOException {// 1.使用Hbase连接获取HtableTableName waterBillTableName = TableName.valueOf("WATER_BILL");Table waterBillTable = connection.getTable(waterBillTableName);// 2.构建ROWKEY、列蔟名、列名String rowkey = "4944191";String cfName = "C1";String colName = "NAME";// 3.构建Put对象(对应put命令)Put put = new Put(Bytes.toBytes(rowkey));// 4.添加姓名列put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colName), Bytes.toBytes("登卫红"));// 5.使用Htable表对象执行put操作waterBillTable.put(put);// 6. 关闭表waterBillTable.close();
}
查看HBase中的数据
get 'WATER_BILL','4944191',{FORMATTER => 'toString'}
插入其他列
列名 | 说明 | 值 |
---|---|---|
ADDRESS | 用户地址 | 贵州省铜仁市德江县7单元267室 |
SEX | 性别 | 男 |
PAY_DATE | 缴费时间 | 2020-05-10 |
NUM_CURRENT | 表示数(本次) | 308.1 |
NUM_PREVIOUS | 表示数(上次) | 283.1 |
NUM_USAGE | 用量(立方) | 25 |
TOTAL_MONEY | 合计金额 | 150 |
RECORD_DATE | 查表日期 | 2020-04-25 |
LATEST_DATE | 最迟缴费日期 | 2020-06-09 |
参考代码:
@Test
public void addTest() throws IOException {// 1.使用Hbase连接获取HtableTableName waterBillTableName = TableName.valueOf("WATER_BILL");Table waterBillTable = connection.getTable(waterBillTableName);// 2.构建ROWKEY、列蔟名、列名String rowkey = "4944191";String cfName = "C1";String colName = "NAME";String colADDRESS = "ADDRESS";String colSEX = "SEX";String colPAY_DATE = "PAY_DATE";String colNUM_CURRENT = "NUM_CURRENT";String colNUM_PREVIOUS = "NUM_PREVIOUS";String colNUM_USAGE = "NUM_USAGE";String colTOTAL_MONEY = "TOTAL_MONEY";String colRECORD_DATE = "RECORD_DATE";String colLATEST_DATE = "LATEST_DATE";// 3.构建Put对象(对应put命令)Put put = new Put(Bytes.toBytes(rowkey));// 4.添加姓名列put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colName), Bytes.toBytes("登卫红"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colADDRESS), Bytes.toBytes("贵州省铜仁市德江县7单元267室"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colSEX), Bytes.toBytes("男"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colPAY_DATE), Bytes.toBytes("2020-05-10"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colNUM_CURRENT), Bytes.toBytes("308.1"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colNUM_PREVIOUS), Bytes.toBytes("283.1"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colNUM_USAGE), Bytes.toBytes("25"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colTOTAL_MONEY), Bytes.toBytes("150"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colRECORD_DATE), Bytes.toBytes("2020-04-25"));put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(colLATEST_DATE), Bytes.toBytes("2020-06-09"));// 5.使用Htable表对象执行put操作waterBillTable.put(put);// 6. 关闭表waterBillTable.close();
}