How to carry out data read and write operations in HBase?
HBase is a distributed, column-oriented NoSQL database that allows data to be read and written through HBase Shell, Java API, or other client tools.
In HBase Shell, you can use the following commands for reading and writing data:
- Insert data:
put into ‘table_name’ with ‘row_key’ under ‘column_family:column_qualifier’ with ‘value’ - Retrieve data:
fetch data from ‘table_name’ using ‘row_key’ - Remove data:
Delete the data in the specified ‘table_name’ with ‘row_key’ and ‘column_family:column_qualifier’.
You can use HBase’s Java API in the Java API to perform data read and write operations. Here is a simple Java code example:
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("table_name"));
Put put = new Put(Bytes.toBytes("row_key"));
put.addColumn(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier"), Bytes.toBytes("value"));
table.put(put);
Get get = new Get(Bytes.toBytes("row_key"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("column_family"), Bytes.toBytes("column_qualifier"));
System.out.println(Bytes.toString(value));
table.close();
connection.close();
With the code above, you can insert and retrieve data from HBase.
In addition to the methods mentioned above, other client tools such as Apache Phoenix, HBase REST API, can also be used to perform data read and write operations. Each method has its own suitable scenarios and advantages, so choose the appropriate method based on specific requirements to achieve data reading and writing operations.