簡體   English   中英

Hbase Java API 示例

[英]Hbase Java API Example

我對 HBase 開發很陌生。 我正在關注鏈接 我正在使用 Hbase-1.1.2 版本。 當我使用示例代碼時,我收到警告。 有幾種方法被棄用(例如,new HBaseAdmin(conf);)我看到 HBaseAdmin 類有 3 個構造函數。 在 3 個構造函數中,2 個被棄用。 只有一個構造函數接受“ClusterConnection”作為參數。 我不知道我是按照正確的鏈接來玩 HBase。 任何人都可以使用最新的 hbase 庫提供示例示例嗎? 我將 HBase 作為獨立模式運行。

這應該有幫助

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

這是我的 HBase 2.0 示例:

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

我在 Spring Boot 2.0 應用程序中編寫了上述演示,gradle 依賴項如下:

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')
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM