【问题标题】:Hbase Java API ExampleHbase Java API 示例
【发布时间】:2015-10-29 09:48:29
【问题描述】:

我对 HBase 开发非常陌生。我关注link。 我正在使用 Hbase-1.1.2 版本。当我使用示例代码时,我收到了警告。有几种方法被弃用(例如,new HBaseAdmin(conf);)我看到 HBaseAdmin 类它有 3 个构造函数。在 3 个构造函数中,有 2 个被弃用。只有一个接受“ClusterConnection”作为参数的构造函数。我不知道我正在按照正确的链接来玩 HBase。任何人都可以使用最新的 hbase 库提供示例示例吗?我将 HBase 作为独立模式运行。

【问题讨论】:

标签: java hadoop hbase


【解决方案1】:

这应该会有所帮助

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

【讨论】:

    【解决方案2】:

    这是我的 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')
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2017-09-15
      相关资源
      最近更新 更多