Updated on 2022-09-14 GMT+08:00

Reading MOB Data

Function Description

Similar to reading of common HBase data, reading HBase MOB data is transparent to users. To use the HBase MOB function, you need to add the related configuration items to the hbase-site.xml file. For details, visit https://hbase.apache.org/book.html#hbase_mob. In addition, you need to enable the MOB function for the specified column family.

Sample Code

The following code snippets are in the testMOBDataRead method in the HBaseExample class of the com.huawei.bigdata.hbase.examples packet.

public void testMOBDataRead() {
    LOG.info("Entering testMOBDataRead.");
    ResultScanner scanner = null;
    Table table = null;
    Admin admin = null;
    try {

        // get table object representing table tableName
        table = conn.getTable(tableName);
        admin = conn.getAdmin();
        admin.flush(table.getName());
        Scan scan = new Scan();
        // get table scanner
        scanner = table.getScanner(scan);
        for (Result result : scanner) {
            byte[] value = result.getValue(Bytes.toBytes("mobcf"), Bytes.toBytes("cf1"));
            String string = Bytes.toString(value);
            LOG.info("value:" + string);
        }
        LOG.info("MOB data read successfully.");
    } catch (Exception e) {
        LOG.error("MOB data read failed ", e);
    } finally {
        if (scanner != null) {
            scanner.close();
        }
        if (table != null) {
            try {
                // Close table object
                table.close();
            } catch (IOException e) {
                LOG.error("Close table failed ", e);
            }
        }
        if (admin != null) {
            try {
                // Close the Admin object.
                admin.close();
            } catch (IOException e) {
                LOG.error("Close admin failed ", e);
            }
        }
    }
    LOG.info("Exiting testMOBDataRead.");
}