Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config, "myLittleHBaseTable");


然后创建管理员后出现此错误


  打开与服务器的套接字连接不会尝试使用SASL进行身份验证(未知错误)

最佳答案

这是我的项目中的jar列表的屏幕快照,可以成功地与HBase进行读写操作:



another post中,我问如何获得一个带有时间戳的单元格的多个版本。该代码如下。它完全可以用于创建表,插入值和检索值。

关于您的问题,我想您需要向代码的配置部分添加更多信息,例如...

    //config
    Configuration config = HBaseConfiguration.create();
    config.clear();
    config.set("hbase.zookeeper.quorum", HBASE_ZOOKEEPER_QUORUM_IP);
    config.set("hbase.zookeeper.property.clientPort", HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT);
    config.set("hbase.master", HBASE_MASTER);


...在下面的完整代码中可以看到这些大写变量的值:

public static void main(String[] args)
    throws ZooKeeperConnectionException, MasterNotRunningException, IOException, InterruptedException {

    final String HBASE_ZOOKEEPER_QUORUM_IP = "localhost.localdomain"; //set ip in hosts file
    final String HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT = "2181";
    final String HBASE_MASTER = HBASE_ZOOKEEPER_QUORUM_IP + ":60010";

    //identify a data cell with these properties
    String tablename = "characters";
    String row = "johnsmith";
    String family = "capital";
    String qualifier = "A";

    //config
    Configuration config = HBaseConfiguration.create();
    config.clear();
    config.set("hbase.zookeeper.quorum", HBASE_ZOOKEEPER_QUORUM_IP);
    config.set("hbase.zookeeper.property.clientPort", HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT);
    config.set("hbase.master", HBASE_MASTER);

    //admin
    HBaseAdmin hba = new HBaseAdmin(config);

    //create a table
    HTableDescriptor descriptor = new HTableDescriptor(tablename);
    descriptor.addFamily(new HColumnDescriptor(family));
    hba.createTable(descriptor);
    hba.close();

    //get the table
    HTable htable = new HTable(config, tablename);

    //insert 10 different timestamps into 1 record
    for(int i = 0; i < 10; i++) {
        String value = Integer.toString(i);
        Put put = new Put(Bytes.toBytes(row));
        put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), System.currentTimeMillis(), Bytes.toBytes(value));
        htable.put(put);
        Thread.sleep(200); //make sure each timestamp is different
    }

    //get 10 timestamp versions of 1 record
    final int MAX_VERSIONS = 10;
    Get get = new Get(Bytes.toBytes(row));
    get.setMaxVersions(MAX_VERSIONS);
    Result result = htable.get(get);
    byte[] value = result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));  // returns MAX_VERSIONS quantity of values
    String output = Bytes.toString(value);

    //show me what you got
    System.out.println(output); //prints 9 instead of 0 through 9
}

关于java - 运行Java类与hbase建立连接的步骤是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24746976/

10-09 00:28