Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hbase Java API Example

Tags:

java

hadoop

hbase

I am very new to HBase development. I am following link. I am using Hbase-1.1.2 release. When I use the sample code I am getting warnings. There are several methods got deprecated (Example, new HBaseAdmin(conf);) I saw the HBaseAdmin class it has 3 constructors. Out of 3 constructors 2 got deprecated. Only one constructor which accepts "ClusterConnection" as an argument. I don't know I am following the proper link to play with the HBase. Can any please provide a sample example by using the latest hbase libraries.? I am running HBase as a standalone mode.

like image 585
Amar Avatar asked Jan 07 '23 06:01

Amar


1 Answers

This should help

    HConnection connection;
    HTableInterface hTableInterface = null;

    Configuration hBaseConfig = HBaseConfiguration.create();
    hBaseConfig.setInt("timeout", 120000);
    //zookeeper quorum, basic info needed to proceed
    hBaseConfig.set("hbase.zookeeper.quorum","host1, host2, host3");
    hBaseConfig.set("hbase.zookeeper.property.clientPort", "2181");
    hBaseConfig.set("zookeeper.znode.parent", "/hbase-unsecure");
    connection = HConnectionManager.createConnection(hBaseConfig);
    hTableInterface = connection.getTable(TableName.valueOf("amarTest"));

    try {   
        Put put = new Put(Bytes.toBytes("999"));
        put.add(Bytes.toBytes("cf"),Bytes.toBytes("name"), Bytes.toBytes("amar"));
        hTableInterface.put(put);
        System.out.println("done");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        hTableInterface.close();
        connection.close();
    }
like image 142
Ramzy Avatar answered Jan 29 '23 19:01

Ramzy