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

Writing Data into a MOB Table

Function Description

Similar to writing common HBase data, writing HBase medium object storage (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. The following provides sample code.

Sample Code

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

public void testCreateMOBTable() {
    LOG.info("Entering testCreateMOBTable.");

    Admin admin = null;
    try {
        // Create Admin instance
        admin = conn.getAdmin();
        HTableDescriptor tabDescriptor = new HTableDescriptor(tableName);
        HColumnDescriptor mob = new HColumnDescriptor("mobcf");
        // Open mob function
        mob.setMobEnabled(true);
        // Set mob threshold
        mob.setMobThreshold(10L);
        tabDescriptor.addFamily(mob);
        admin.createTable(tabDescriptor);
        LOG.info("MOB Table is created successfully.");

    } catch (Exception e) {
        LOG.error("MOB Table is created failed ", e);
    } finally {
        if (admin != null) {
            try {
                // Close the Admin object.
                admin.close();
            } catch (IOException e) {
                LOG.error("Close admin failed ", e);
            }
        }
    }
    LOG.info("Exiting testCreateMOBTable.");
}

Example: Using the Put interface to write the MOB data

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

public void testMOBDataInsertion() {
    LOG.info("Entering testMOBDataInsertion.");

    Table table = null;
    try {
        // set row name to "row"
        Put p = new Put(Bytes.toBytes("row"));
        byte[] value = new byte[1000];
        // set the column value of column family mobcf with the value of "cf1"
        p.addColumn(Bytes.toBytes("mobcf"), Bytes.toBytes("cf1"), value);
        // get the table object represent table tableName
        table = conn.getTable(tableName);
        // put data
        table.put(p);
        LOG.info("MOB data inserted successfully.");

    } catch (Exception e) {
        LOG.error("MOB data inserted failed ", e);
    } finally {
        if (table != null) {
            try {
                table.close();
            } catch (Exception e1) {
                LOG.error("Close table failed ", e1);
            }
        }
    }
    LOG.info("Exiting testMOBDataInsertion.");
}

Precautions

Multiple threads are not allowed to use the same HTable instance at the same time. HTable is a non-thread-safe class. If an HTable instance is used by multiple threads at the same time, exceptions will occur.