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

Inserting Data

Function Description

HBase is a column-based database. A row of data may have multiple column families, and a column family may contain multiple columns. When writing data, you must specify the columns (including the column family names and column names) to which data is written. HBase allows you to insert data (a row of data or data sets) using the put method of HTable.

Sample Code

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

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

    // Specify the column family name.
    byte[] familyName = Bytes.toBytes("info");
    // Specify the column name.
    byte[][] qualifiers = {Bytes.toBytes("name"), Bytes.toBytes("gender"), Bytes.toBytes("age"),
            Bytes.toBytes("address")};

    Table table = null;
    try {
        // Instantiate an HTable object.
        table = conn.getTable(tableName);
        List<Put> puts = new ArrayList<Put>();
        // Instantiate a Put object.
        Put put = new Put(Bytes.toBytes("012005000201"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Zhang San"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("19"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Shenzhen, Guangdong"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000202"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Li Wanting"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("23"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Shijiazhuang, Hebei"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000203"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Wang Ming"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("26"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Ningbo, Zhejiang"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000204"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Li Gang"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("18"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Xiangyang, Hubei"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000205"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Zhao Enru"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("21"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Shangrao, Jiangxi"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000206"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Chen Long"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("32"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Zhuzhou, Hunan"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000207"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Zhou Wei"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("29"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Nanyang, Henan"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000208"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Yang Yiwen"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Female"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("30"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Kaixian, Chongqing"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000209"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Xu Bing"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("26"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Weinan, Shaanxi"));
        puts.add(put);

        put = new Put(Bytes.toBytes("012005000210"));
        put.addColumn(familyName, qualifiers[0], Bytes.toBytes("Xiao Kai"));
        put.addColumn(familyName, qualifiers[1], Bytes.toBytes("Male"));
        put.addColumn(familyName, qualifiers[2], Bytes.toBytes("25"));
        put.addColumn(familyName, qualifiers[3], Bytes.toBytes("Dalian, Liaoning"));
        puts.add(put);

        // Submit a put request.
        table.put(puts);

        LOG.info("Put successfully.");
    } catch (IOException e) {
        LOG.error("Put failed ", e);
    } finally {
        if (table != null) {
            try {
                // Close the HTable object.
                table.close();
            } catch (IOException e) {
                LOG.error("Close table failed ", e);
            }
        }
    }
    LOG.info("Exiting testPut.");
}

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.