【发布时间】:2016-02-28 17:38:48
【问题描述】:
我创建了一个 java 应用程序来从 HBase 读取数据。我检查了link1link2link3 和link4。即使我的表中有数据,程序也会返回 null。
hbase 外壳:
hbase(main):009:0> get 'login','1'
COLUMN CELL
password: password timestamp=1456588594424, value=hpassword
username: username timestamp=1456588582413, value=husername
2 row(s) in 0.0120 seconds
代码:
Configuration config = HBaseConfiguration.create();
HTable table = new HTable(config, "login");
Get g = new Get(Bytes.toBytes("row1"));
Result r = table.get(g);
byte [] value = r.getValue(Bytes.toBytes("username"),Bytes.toBytes("username"));
byte [] value1 = r.getValue(Bytes.toBytes("password"),Bytes.toBytes("password"));
String valueStr = Bytes.toString(value);
String valueStr1 = Bytes.toString(value1);
System.out.println("username: "+ valueStr+"\npassword: "+valueStr1);
Scan s = new Scan();
s.addColumn(Bytes.toBytes("username"), Bytes.toBytes("username"));
s.addColumn(Bytes.toBytes("password"), Bytes.toBytes("password"));
ResultScanner scanner = table.getScanner(s);
try
{
for (Result rnext = scanner.next(); rnext != null; rnext = scanner.next())
{
System.out.println("Found row : " + rnext);
}
}finally{
scanner.close();
}
输出:
username: null
password: null
我是否遗漏了什么,或者我需要在代码中的某个地方进行编辑?
另外,我需要将它实现到 Java Play 框架中。你有什么想法吗?
提前致谢。
【问题讨论】:
-
不清楚使用什么列族来存储数据。您对列和列族都使用“用户名”,这似乎是错误的
-
感谢您的帮助。我更改了它们并将 Bytes.toBytes("row1")) "row1" 更改为 "1"。
-
为什么要同时使用scan和get?
-
测试这些功能之间的差异
标签: java hadoop playframework hbase