Inserting Data

Inserting Data using HBase Shell

This tutorial demonstrates how to create data in an HBase table. To create data in an HBase table, the following commands and methods are used.

  1. put command,
  2. add() method of Put class, and
  3. put() method of HTable class.

Using put command, you can insert rows into a table. Its syntax is as follows

put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’
Inserting the First Row

Let us insert the first row values into the emp table as shown below.

hbase put 'emp','1','personal data:name','ashok'
0 row(s) in 0.7600 seconds
hbase> put 'emp','1','personal data:city','bhimavaram'
0 row(s) in 0.0620 seconds
hbase> put 'emp','1','professional data:designation','software engineer'
0 row(s) in 0.0290 seconds
hbase> put 'emp','1','professional data:salary','25000'
0 row(s) in 0.0310 seconds
Inserting Data Using Java API

You can insert data into Hbase using the add() method of the Put class. You can save it using the put() method of the HTable class. These classes belong to the org.apache.hadoop.hbase.client package. Below given are the steps to create data in a Table of HBase.

package com.ashok.hbase;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyInsertData {
   public static void main(String[] args) throws IOException {
      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable hTable = new HTable(config, "emp");

      // Instantiating Put class
      // accepts a row name.
      Put p = new Put(Bytes.toBytes("row1")); 

      // adding values using add() method
      // accepts column family name, qualifier/row name ,value
      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("name"),Bytes.toBytes("ashok"));

      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("city"),Bytes.toBytes("bhimavaram"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),
      Bytes.toBytes("software engineer"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),
      Bytes.toBytes("25000"));
      
      // Saving the put Instance to the HTable.
      hTable.put(p);
      System.out.println("Successfully data inserted");
      
      // closing HTable
      hTable.close();
   }
}
Inserting Data


Scroll to top