Amar
Amar

Reputation: 763

Hbase Java API Example

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.

Upvotes: 4

Views: 4149

Answers (2)

derek.z
derek.z

Reputation: 967

Here is my HBase 2.0 example:

Configuration hBaseConfig = HBaseConfiguration.create();
hBaseConfig.set("hbase.zookeeper.quorum", "192.168.x.xxx");
hBaseConfig.set("hbase.zookeeper.property.clientPort", "2181");
HBaseAdmin.available(hBaseConfig);
Connection connection = ConnectionFactory.createConnection(hBaseConfig);

TableName table1 = TableName.valueOf("test");
Table table = connection.getTable(table1);

CellBuilder cb = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
cb.setRow(Bytes.toBytes("row2"));
cb.setFamily(Bytes.toBytes("cf1"));
cb.setQualifier("qualifier1".getBytes());
cb.setValue(Bytes.toBytes("cell_data2"));
cb.setType(Type.Put);
Cell cell = cb.build();

Put p = new Put(Bytes.toBytes("row2"));
p.add(cell);
table.put(p);

connection.close();

I wrote the above demo in a Spring Boot 2.0 application, gradle dependencies as following:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile ('org.apache.hbase:hbase:2.0.0')
    compile ('org.apache.hbase:hbase-client:2.0.0'){
        exclude group :'log4j',module:'log4j'
        exclude group :'org.slf4j',module:'slf4j-log4j12'
        exclude group: 'javax.servlet', module: 'servlet-api'
    }
    compile('org.springframework.boot:spring-boot-configuration-processor')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Upvotes: 3

Ramzy
Ramzy

Reputation: 7148

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();
    }

Upvotes: 6

Related Questions