HBase API 之 表数据操作

前置博客(必读)

HBase API之表操作

具体代码

向表中插入数据

  • 功能代码
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");
}
  • 结果
    HBase API 之 表数据操作_第1张图片

获取某一行指定列的数据

  • 功能代码
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");
}
  • 结果
    HBase API 之 表数据操作_第2张图片

获取一行数据

  • 功能代码
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个历史版本的数据
HBase API 之 表数据操作_第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");
}
  • 结果
    编号① 处代码注释起来
    HBase API 之 表数据操作_第4张图片
    编号① 处代码未注释
    HBase API 之 表数据操作_第5张图片

删除多条数据

  • 功能代码
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");
}
  • 结果
    HBase API 之 表数据操作_第6张图片

你可能感兴趣的:(#,HBase)