如何从Hbase读取数据? [英] How to read data from Hbase?

查看:499
本文介绍了如何从Hbase读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我用于SQL,但我需要从HBase表中读取数据。任何对此的帮助都会很大。一本书或者可能只是一些示例代码来从表中读取。有人说使用扫描仪可以做到这一点,但我不知道如何使用它。 解决方案

From 网站

  //有时,您不会知道您要查找的行。在这种情况下,您
//使用扫描仪。这会给你表格的内容
//的游标式界面。要设置一个扫描仪,像上面做的那样做一个Put
//一个Get,创建一个扫描。用列名等来装饰它
Scan s = new Scan();
s.addColumn(Bytes.toBytes(myLittleFamily),Bytes.toBytes(someQualifier));
ResultScanner scanner = table.getScanner(s);
尝试{
//扫描仪返回结果实例。
//现在,对于实际的迭代。一种方法是使用如下所示的while循环:
for(Result rr = scanner.next(); rr!= null; rr = scanner.next()){
//输出行我们发现和我们寻找的列
System.out.println(Found row:+ rr);
}

//另一种方法是使用foreach循环。扫描仪是可迭代的!
// for(Result rr:scanner){
// System.out.println(Found row:+ rr);
//}
}最后{
//确保您完成后关闭扫描仪!
//这就是为什么我们把它放在try / finally子句中
scanner.close();
}


Hi there I'm use to SQL, but I need to read data from a HBase table. Any help on this would be great. A book or maybe just some sample code to read from the table. Someone said using a scanner would do the trick, but I do not know how to use it.

解决方案

From the website:

// Sometimes, you won't know the row you're looking for. In this case, you
// use a Scanner. This will give you cursor-like interface to the contents
// of the table.  To set up a Scanner, do like you did above making a Put
// and a Get, create a Scan.  Adorn it with column names, etc.
Scan s = new Scan();
s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
ResultScanner scanner = table.getScanner(s);
try {
  // Scanners return Result instances.
  // Now, for the actual iteration. One way is to use a while loop like so:
  for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
    // print out the row we found and the columns we were looking for
    System.out.println("Found row: " + rr);
  }

  // The other approach is to use a foreach loop. Scanners are iterable!
  // for (Result rr : scanner) {
  //   System.out.println("Found row: " + rr);
  // }
} finally {
  // Make sure you close your scanners when you are done!
  // Thats why we have it inside a try/finally clause
  scanner.close();
}

这篇关于如何从Hbase读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆