问题描述
我可以使用Java API连接到HBase的以单机模式(无Hadoop的)?
下面是我的code,我想知道如何使它发挥作用。我应该设置一些属性变量'配置'?
我有这些本地安装:HBase的-0.98.0的Hadoop 2.2.0
进口java.io.IOException异常;进口org.apache.hadoop.conf.Configuration;
进口org.apache.hadoop.fs.Path;
进口org.apache.hadoop.hbase.HBaseConfiguration;
进口org.apache.hadoop.hbase.client.Get;
进口org.apache.hadoop.hbase.client.HTable;
进口org.apache.hadoop.hbase.client.Put;
进口org.apache.hadoop.hbase.client.Result;
进口org.apache.hadoop.hbase.client.ResultScanner;
进口org.apache.hadoop.hbase.client.Scan;
进口org.apache.hadoop.hbase.util.Bytes;
公共类MyLittleHBaseClient {
公共静态无效的主要(字串[] args)抛出IOException //也许我应该在这里做一些配置,但我不知道如何
配置配置= HBaseConfiguration.create(); HTable表=新HTable(配置myLittleHBaseTable); 放P =认沽新(Bytes.toBytes(myLittleRow)); p.add(Bytes.toBytes(myLittleFamily),Bytes.toBytes(someQualifier),
Bytes.toBytes(一些价值)); table.put(P); 获取G =新获取(Bytes.toBytes(myLittleRow));
结果R = table.get(G);
字节[]值= r.getValue(Bytes.toBytes(myLittleFamily),
Bytes.toBytes(someQualifier)); 字符串valueStr = Bytes.toString(值);
的System.out.println(GET:+ valueStr); 扫描S =新的扫描();
s.addColumn(Bytes.toBytes(myLittleFamily),Bytes.toBytes(someQualifier));
ResultScanner扫描器= table.getScanner(多个);
尝试{ 为(结果RR = scanner.next(); RR =无效;!RR = scanner.next()){ 的System.out.println(发现的行:+ RR);
} } {最后 scanner.close();
}
}
}
如果您的HBase-site.xml中以独立模式是空的(),你没有设置任何东西。如果已经覆盖了HBase的-site.xml中任何东西,更好的补充,而不是单独设置的参数,HBase的-site.xml中。
配置配置= HBaseConfiguration.create();
config.addResource(&所述; HBASE_CONF_DIR_PATH> /hbase-site.xml);
Can I use java api to connect to Hbase in a standalone mode(without Hadoop)?
Here is my code, and I was wondering how to make it work. Should I set some property to the variable 'config'?
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();
}
}
}
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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!