Search code examples
javaapihbase

Adding values in HBase using Put.add() method


I'm writing a simple Java client code to add values in HBase table. I'm using put.add(byte[] columnFamily, byte[] columnQualifier, byte[] value), but this method is deprecated in new HBase API. Can anyone please help in what is the way of doing it using new Put API?

Using maven I have downloaded jar for HBase version 1.2.0.

I'm using the following code :

package com.NoSQL;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;


public class PopulatingData {

    public static void main(String[] args) throws IOException{

        String table = "Employee";

        Logger.getRootLogger().setLevel(Level.WARN);
        Configuration conf = HBaseConfiguration.create();
        Connection con = ConnectionFactory.createConnection(conf);
        Admin admin = con.getAdmin();

        if(admin.tableExists(TableName.valueOf(table))) {
            Table htable = con.getTable(TableName.valueOf(table));


            /*********** adding a new row ***********/

            // adding a row key


            Put p = new Put(Bytes.toBytes("row1"));



            p.add(Bytes.toBytes("ContactDetails"), Bytes.toBytes("Mobile"), Bytes.toBytes("9876543210"));
            p.add(Bytes.toBytes("ContactDetails"), Bytes.toBytes("Email"), Bytes.toBytes("[email protected]"));

            p.add(Bytes.toBytes("Personal"), Bytes.toBytes("Name"), Bytes.toBytes("Abhinav Rawat"));
            p.add(Bytes.toBytes("Personal"), Bytes.toBytes("Age"), Bytes.toBytes("21"));
            p.add(Bytes.toBytes("Personal"), Bytes.toBytes("Gender"), Bytes.toBytes("M"));

            p.add(Bytes.toBytes("Employement"), Bytes.toBytes("Company"), Bytes.toBytes("UpGrad"));
            p.add(Bytes.toBytes("Employement"), Bytes.toBytes("DOJ"), Bytes.toBytes("11:06:2018"));
            p.add(Bytes.toBytes("Employement"), Bytes.toBytes("Designation"), Bytes.toBytes("ContentStrategist"));

            htable.put(p);


            /**********************/

            System.out.print("Table is Populated");`enter code here`            

        }else {

            System.out.println("The HBase Table named "+table+" doesn't exists.");
        }

        System.out.println("Returnning Main");

    }

}

Solution

  • Use addColumn() method :

    Put put = new Put(Bytes.toBytes(rowKey));
    put.addColumn(NAME_FAMILY, NAME_COL_QUALIFIER, name);
    

    Please refer more details in below javadoc : https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Put.html