前置博客(必读)
具体代码
向表中插入数据
- 功能代码
public static void addRowData(String tableName, String rowKey,
String columnFamily, String column, String value) throws IOException {
//创建HTable对象
Table table = conn.getTable(TableName.valueOf(tableName));
//向表中插入数据
Put put = new Put(Bytes.toBytes(rowKey));
//向Put对象中组装数据
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
table.close();
System.out.println("插入数据成功");
}
- 测试代码
@Test
public void addRowData() throws IOException {
HBaseUtil.addRowData("stu","1001","info","name","zhangsan");
HBaseUtil.addRowData("stu","1001","info","age","18");
HBaseUtil.addRowData("stu","1002","info","name","lisi");
HBaseUtil.addRowData("stu","1002","info","age","19");
}
- 结果
获取某一行指定列的数据
- 功能代码
public static void getRowColumn(String tableName, String rowKey,
String columnFamily, String column) throws IOException {
//创建HTable对象
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println("行键:" + Bytes.toString(result.getRow()));
System.out.println("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
- 测试代码
@Test
public void getRowColumn() throws IOException {
HBaseUtil.getRowColumn("stu","1001","info","name");
}
- 结果
获取一行数据
- 功能代码
public static void getRow(String tableName, String rowKey) throws IOException {
//创建HTable对象
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.print("行键:" + Bytes.toString(result.getRow()));
System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.print("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("\t时间戳:" + cell.getTimestamp());
}
table.close();
}
- 测试代码
@Test
public void getRow() throws IOException {
HBaseUtil.getRow("stu","1001");
}
- 结果
获取所有版本的数据
假设存在3个历史版本的数据
通过以下代码可以读取所有版本的数据
public static void getRow(String tableName, String rowKey) throws IOException {
//创建HTable对象
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.readAllVersions();
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.print("行键:" + Bytes.toString(result.getRow()));
System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.print("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("\t时间戳:" + cell.getTimestamp());
}
table.close();
}
测试代码:
@Test
public void getRow() throws IOException {
HBaseUtil.getRow("stu","1001");
}
结果:
获取所有数据
- 功能代码
public static void getAllRows(String tableName) throws IOException {
Table table = conn.getTable(TableName.valueOf(tableName));
//得到用于扫描region的对象
Scan scan = new Scan();
scan.readAllVersions(); // ----------① 获取所有版本的数据
//使用HTable得到resultcanner实现类的对象
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//得到rowkey
System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
//得到列族
System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
table.close();
}
- 测试代码
@Test
public void getAllRows() throws IOException {
HBaseUtil.getAllRows("stu");
}
- 结果
编号① 处代码注释起来
编号① 处代码未注释
删除多条数据
- 功能代码
public static void deleteMultiRow(String tableName, String... rows) throws IOException {
//创建HTable对象
Table table = conn.getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<>();
for (String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
System.out.println("删除数据成功");
}
- 测试代码
@Test
public void deleteMultiRow() throws IOException {
HBaseUtil.deleteMultiRow("stu","1001","1002");
}
- 结果
来源:CSDN
作者:梁云亮
链接:https://blog.csdn.net/lianghecai52171314/article/details/104778419