无法使用java api连接到Hbase

时间:2014-03-27 07:00:22

标签: java api hadoop hbase

我可以使用java api以独立模式(没有Hadoop)连接到Hbase吗?

这是我的代码,我想知道如何使它工作。我应该将一些属性设置为变量' config'?

我在本地安装了这些:Hbase-0.98.0 Hadoop 2.2.0

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;


public class MyLittleHBaseClient {
  public static void main(String[] args) throws IOException {

    // maybe I should do some configuration here, but I don't know how
    Configuration config = HBaseConfiguration.create();

    HTable table = new HTable(config, "myLittleHBaseTable");

    Put p = new Put(Bytes.toBytes("myLittleRow"));

    p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
      Bytes.toBytes("Some Value"));

    table.put(p);

    Get g = new Get(Bytes.toBytes("myLittleRow"));
    Result r = table.get(g);
    byte [] value = r.getValue(Bytes.toBytes("myLittleFamily"),
      Bytes.toBytes("someQualifier"));

    String valueStr = Bytes.toString(value);
    System.out.println("GET: " + valueStr);

    Scan s = new Scan();
    s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
    ResultScanner scanner = table.getScanner(s);
    try {

      for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {

        System.out.println("Found row: " + rr);
      }

    } finally {

      scanner.close();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果你的独立模式的hbase-site.xml是空的(),你不必设置任何东西。如果已覆盖hbase-site.xml中的任何内容,最好添加该hbase-site.xml,而不是单独设置参数。

Configuration config = HBaseConfiguration.create();
config.addResource("<HBASE_CONF_DIR_PATH>/hbase-site.xml");