无法使用 java api 连接到 Hbase [英] fail to connect to Hbase with java api

查看:46
本文介绍了无法使用 java api 连接到 Hbase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can I use java api to connect to Hbase in a standalone mode(without Hadoop)?

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

Here is my code, and I was wondering how to make it work. Should I set some property to the variable 'config'?

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

I have these installed locally : 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();
    }
  }
}

推荐答案

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

If your hbase-site.xml in standalone mode is empty(), you don't have to set any thing. If have overridden anything in the hbase-site.xml, better add that hbase-site.xml instead of setting parameter separately.

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

这篇关于无法使用 java api 连接到 Hbase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆