Hbase Java API示例

Ama*_*mar 4 java hadoop hbase

我对HBase开发非常陌生。我正在跟踪链接。我正在使用Hbase-1.1.2版本。使用示例代码时,我会收到警告。不推荐使用几种方法(例如,新的HBaseAdmin(conf);)。我看到HBaseAdmin类具有3个构造函数。在3个构造函数中,有2个已弃用。只有一个接受“ ClusterConnection”作为参数的构造函数。我不知道我是否遵循正确的链接来使用HBase。可以使用最新的hbase库提供示例示例吗?我将HBase作为独立模式运行。

Ram*_*mzy 5

这应该有帮助

    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();
    }
Run Code Online (Sandbox Code Playgroud)