首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hbase Java API:检索与部分行键匹配的所有行

Hbase Java API:检索与部分行键匹配的所有行
EN

Stack Overflow用户
提问于 2014-02-18 09:11:36
回答 1查看 23.3K关注 0票数 10

在Python模块happybase中,我可以检索行键以给定字符串开头的所有行(即,使用部分行键进行搜索)。

假设我有一个( ID | TYPE |DATE)格式的行键,我可以通过以下方式找到ID为1、类型为A的所有行:

代码语言:javascript
复制
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

这是到目前为止我所拥有的一个完全的客户端Java程序,任何人都可以使用Java HBase API来做基础工作,但是我只能使用完整的行键来搜索一行:

代码语言:javascript
复制
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);
}
//}

其中"cf“是柱族的名称。

答案:

代码语言:javascript
复制
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);
}
//}

请注意,我使用setFilter方法,而下面的答案使用addFilter方法,因为我们使用不同的API。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-18 11:49:09

您使用的是HTable get操作,因此只返回一行(请注意,您也可以在此处指定前缀,而不必提供完整的键)

如果想要返回多个行,则应该使用Scan

代码语言:javascript
复制
byte[] prefix=Bytes.toBytes("1|A|");
Scan scan = new Scan(prefix);
PrefixFilter prefixFilter = new PrefixFilter(prefix);
scan.addFilter(prefixFilter);
ResultScanner resultScanner = table.getScanner(scan);
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21842469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档