How to add data to a table in HBase?

There are several ways to add data to an HBase table.

  1. place
  2. the table owned by me
put 'myTable', 'rowKey1', 'columnFamily:columnQualifier1', 'value1'
put 'myTable', 'rowKey2', 'columnFamily:columnQualifier2', 'value2'
  1. By utilizing the Java API, you can write code to insert data using HBase. Here is a simple example:
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseExample {
    public static void main(String[] args) throws Exception {
        // 创建HBase连接
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);

        // 获取表对象
        TableName tableName = TableName.valueOf("myTable");
        Table table = connection.getTable(tableName);

        // 创建Put对象,并设置行键和列值
        Put put = new Put(Bytes.toBytes("rowKey1"));
        put.addColumn(Bytes.toBytes("columnFamily"), Bytes.toBytes("columnQualifier1"), Bytes.toBytes("value1"));

        // 插入数据
        table.put(put);

        // 关闭连接
        table.close();
        connection.close();
    }
}

In this context, myTable is the name of the table, rowKey1 is the row key, columnFamily is the column family, columnQualifier1 is the column qualifier, and value1 is the value to be inserted.

  1. The HBase REST API allows for inserting data by sending a POST request with the data as a JSON object to HBase. Here is an example.
curl -vi -X POST \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  --data '{
    "Row": [
        {
            "key": "cm93S2V5MQ==",
            "Cell": [
                {
                    "column": "Y29sdW1uRmFtaWx5OmNvbHVtblF1YWxpZmllcjE=",
                    "$": "dmFsdWUx"
                }
            ]
        }
    ]
}' \
  "http://localhost:8080/myTable/rowKey1"

In this case, myTable is the name of the table, rowKey1 is the row key, columnFamily:columnQualifier1 is the column, and value1 is the value to be inserted.

No matter which method you choose, it is necessary to ensure that HBase is properly installed and configured, and that the table already exists.

bannerAds