gpt4 book ai didi

java - 无法使用 java api 连接到 Hbase

转载 作者:可可西里 更新时间:2023-11-01 16:15:19 25 4
gpt4 key购买 nike

我可以在独立模式下(没有 Hadoop)使用 java api 连接到 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();
}
}
}

最佳答案

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

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

关于java - 无法使用 java api 连接到 Hbase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22680433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com