HBase 读取数据 returns 空
HBase read data returns null
我创建了一个 java 应用程序来从 HBase 读取数据。我检查了 link1 link2 link3 and link4。即使我的 table.
中有数据,程序 returns null
hbase shell:
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 Framework 中。你有什么想法吗?
提前致谢。
试试这个:
// Instantiating Configuration class
Configuration config = HBaseConfiguration.create();
// Instantiating HTable class
HTable table = new HTable(config, "tablename");
// Instantiating the Scan class
Scan scan = new Scan();
// Scanning the required columns
scan.addColumn(Bytes.toBytes("columnfamily"), Bytes.toBytes("column1"));
scan.addColumn(Bytes.toBytes("columnfamily"), Bytes.toBytes("column2"));
// Getting the scan result
ResultScanner scanner = table.getScanner(scan);
// Reading values from scan result
for (Result result = scanner.next(); result != null; result = scanner.next())
System.out.println("Found row : " + result);
//closing the scanner
scanner.close();
正如@AdamSkywalker 所说,我需要更改列名和列族的名称。当他们的名字相同时,我得到 null return 。
我重命名了它们并且代码运行良好。
我创建了一个 java 应用程序来从 HBase 读取数据。我检查了 link1 link2 link3 and link4。即使我的 table.
中有数据,程序 returns nullhbase shell:
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 Framework 中。你有什么想法吗?
提前致谢。
试试这个:
// Instantiating Configuration class
Configuration config = HBaseConfiguration.create();
// Instantiating HTable class
HTable table = new HTable(config, "tablename");
// Instantiating the Scan class
Scan scan = new Scan();
// Scanning the required columns
scan.addColumn(Bytes.toBytes("columnfamily"), Bytes.toBytes("column1"));
scan.addColumn(Bytes.toBytes("columnfamily"), Bytes.toBytes("column2"));
// Getting the scan result
ResultScanner scanner = table.getScanner(scan);
// Reading values from scan result
for (Result result = scanner.next(); result != null; result = scanner.next())
System.out.println("Found row : " + result);
//closing the scanner
scanner.close();
正如@AdamSkywalker 所说,我需要更改列名和列族的名称。当他们的名字相同时,我得到 null return 。 我重命名了它们并且代码运行良好。