Updated on 2024-04-02 GMT+08:00

Inserting Data

Function

HBase is a column-oriented 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. In HBase, data (a row of data or data sets) is inserted using the put method of HTable.

Example Code

The following code snippet belongs to the testPut method in the HBaseSample class of the com.huawei.bigdata.hbase.examples package.

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 = putData(familyName, qualifiers,
                Arrays.asList("012005000201", "Zhang San", "Male", "19", "Shenzhen, Guangdong"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000202", "Li Wanting", "Female", "23", "Shijiazhuang, Hebei"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000203", "Wang Ming", "Male", "26", "Ningbo, Zhejiang"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000204", "Li Gang", "Male", "18", "Xiangyang, Hubei"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000205", "Zhao Enru", "Female", "21", "Shangrao, Jiangxi"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000206", "Chen Long", "Male", "32", "Zhuzhou, Hunan"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000207", "Zhou Wei", "Female", "29", "Nanyang, Henan"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000208", "Yang Yiwen", "Female", "30", "Kaixian, Chongqing"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000209", "Xu Bing", "Male", "26", "Weinan, Shaanxi"));
            puts.add(put);

            put = putData(familyName, qualifiers,
                Arrays.asList("012005000210", "Xiao Kai", "Male", "25", "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, concurrency problems will occur.