Database Reference
In-Depth Information
Writing data
In HBase, when a write request is received, by default, the data is synchronously
written into HLog, also referred to as the write ahead log or commit log and to the
memstore. Writing data at two places ensures data durability. The memstore is a
write buffer that accumulates the data before committing it to the disk permanently
in the form of an HFile. Every time the memstore lushes the data to the disk, a new
HFile is created. In case of an HBase cluster failure, data that is not committed to an
HFile from the memstore, the write buffer is recovered from the HLog ile present
in the ilesystem. This way of writing to HBase is applicable to both row creation
and updation.
A Put class instance is used to store data in an HBase table. For storing data in a
table, create a Put instance with rowkey using any of the constructors, as follows:
Put(byte[] rowkey)
Put(byte[] rowArray, int rowOffset, int rowLength)
Put(byte[] rowkey, long ts)
Put(byte[] rowArray, int rowOffset, int rowLength, long ts)
Put p = new Put (Bytes.toBytes("John"));
HBase stores all the data, including the rowkey, in the form of a
byte array and a Java utility class, bytes deine various static utility
methods for converting Java data types to and from a byte.
Once a Put instance is created using the rowkey component, the next step is to add
the data by using either of the following method deinitions:
add(byte[] family, byte[] qualifier, byte[] value)
add(byte[] family, byte[] qualifier, long ts, byte[] value)
add (byte[] family, ByteBuffer qualifier, long ts, ByteBuffer value)
add (Cell kv)
The add() option takes a column family along with an optional timestamp or one
single cell as a parameter. In case the timestamp is not deined, the region server sets
it for the data inserted. Here is a complete example of how to write data to HBase:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
 
Search WWH ::




Custom Search