在Python模块happybase中,我可以检索具有以给定字符串开头的行键的所有行(即,使用部分行键进行搜索).
假设我有一个格式为(ID | TYPE | DATE)的rowkey,我可以通过以下方式找到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
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是一个完全客户端的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);
}
//}
Run Code Online (Sandbox Code Playgroud)
其中"cf"是列族的名称.
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);
}
//}
Run Code Online (Sandbox Code Playgroud)
请注意,我使用该setFilter方法,而下面的答案使用该addFilter方法,因为我们使用不同的API.
Arn*_*-Oz 21
您正在使用HTable get操作,因此您只返回一行(请注意,您也可以在此处指定前缀,而不必提供完整的密钥)
如果你想要回到多行,你应该使用a 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22006 次 |
| 最近记录: |