Hbase Java API:检索与部分行键匹配的所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21842469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hbase Java API: Retrieving all rows that match a Partial Row Key
提问by Matthew Moisen
In the Python module happybase, I can retrieve all rows that have a row key starting with a given string (i.e, search using a partial row key).
在 Python 模块happybase 中,我可以检索具有以给定字符串开头的行键的所有行(即,使用部分行键进行搜索)。
Let's say I have a rowkey in the format of (ID|TYPE|DATE), I would be able to find all rows with an ID of 1 and a TYPE of A by:
假设我有一个 (ID|TYPE|DATE) 格式的行键,我将能够通过以下方式找到 ID 为 1 和 TYPE 为 A 的所有行:
import happybase
connection = happybase.Connection('hmaster-host.com')
table = connection.table('table_name')
for key, data in table.scan(row_prefix="1|A|"):
print key, data
This is what I have so far as a totally client side Java program for anyone trying to do the basics using the Java HBase API, but I can only search for a row using the full row key:
到目前为止,这就是我拥有的完全客户端 Java 程序,适用于任何尝试使用Java HBase API进行基础操作的人,但我只能使用完整的行键搜索一行:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
//class foo {
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.addResource(new Path("C:\core-site.xml"));
conf.addResource(new Path("C:\hbase-site.xml"));
HTable table = new HTable(conf, "table_name");
Result row = table.get(new Get(Bytes.toBytes("1|A|2014-01-01 00:00")));
printRow(row);
}
public static void printRow(Result result) {
String returnString = "";
returnString += Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("id"))) + ", ";
returnString += Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("type"))) + ", ";
returnString += Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("date")));
System.out.println(returnString);
}
//}
Where "cf" is the name of the column family.
其中“cf”是列族的名称。
ANSWER:
回答:
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.client.HTable;
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.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
//class foo {
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.addResource(new Path("C:\core-site.xml"));
conf.addResource(new Path("C:\hbase-site.xml"));
HTable table = new HTable(conf, "table_name");
byte[] prefix = Bytes.toBytes("1|A|");
Scan scan = new Scan(prefix);
Filter prefixFilter = new PrefixFilter(prefix);
scan.setFilter(prefixFilter);
ResultScanner resultScanner = table.getScanner(scan);
printRows(resultScanner);
//Result row = table.get(new Get(Bytes.toBytes("1|A|2014-01-01 00:00")));
//printRow(row);
}
public static void printRows(ResultScanner resultScanner) {
for (Iterator<Result> iterator = results.iterator(); iterator.hasNext();) {
printRow(iterator.next();
}
}
public static void printRow(Result result) {
String returnString = "";
returnString += Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("id"))) + ", ";
returnString += Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("type"))) + ", ";
returnString += Bytes.toString(result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("date")));
System.out.println(returnString);
}
//}
Note that I use the setFilter
method, whereas the answer below uses the addFilter
method, on account of us using different APIs.
请注意,我使用了该setFilter
方法,而下面的答案使用了该addFilter
方法,因为我们使用了不同的 API。
采纳答案by Arnon Rotem-Gal-Oz
You are using the HTable get
operation so you're only getting back one row (note that you can specify a prefix here as well and you don't have to give the complete key)
您正在使用 HTableget
操作,因此您只返回一行(请注意,您也可以在此处指定前缀,而不必提供完整的键)
If you want to get back multiple rows you should use a Scan
如果你想取回多行,你应该使用 Scan
byte[] prefix=Bytes.toBytes("1|A|");
Scan scan = new Scan(prefix);
PrefixFilter prefixFilter = new PrefixFilter(prefix);
scan.addFilter(prefixFilter);
ResultScanner resultScanner = table.getScanner(scan);