您好我用于SQL,但我需要从HBase表中读取数据。任何对此的帮助都会很大。一本书或者可能只是一些示例代码来从表中读取。有人说使用扫描仪会做伎俩,但我不知道如何使用它。如何从Hbase读取数据?
5
A
回答
9
// 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();
}
2
我使用的,但让你必须从结果使用方法GetValue字符串值。
byte[] bytes = rr.getValue(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
System.out.println(new String(bytes));
3
我想提供解决方案,而不赞成的方法
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
// list the tables
Arrays.stream(admin.listTables()).forEach(System.out::println);
// let's insert some data in 'mytable' and get the row
TableName tableName = TableName.valueOf("test_1");
Table table = connection.getTable(tableName);
//Put
Put thePut = new Put(Bytes.toBytes("rowkey1"));
String columnFamily = "m";
String columnQualifier1 = "col1";
String outValue1 = "value1";
String columnQualifier2 = "col2";
String outValue2 = "value2";
thePut.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier1), Bytes.toBytes(outValue1));
thePut.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier2), Bytes.toBytes(outValue2));
table.put(thePut);
//Get
Get theGet = new Get(Bytes.toBytes("rowkey1"));
Result result = table.get(theGet);
//get value first column
String inValue1 = Bytes.toString(result.value());
//get value by ColumnFamily and ColumnName
byte[] inValueByte = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier1));
String inValue2 = Bytes.toString(inValueByte);
//loop for result
for (Cell cell : result.listCells()) {
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.printf("Qualifier : %s : Value : %s%n", qualifier, value);
}
//create Map by result and print it
Map<String, String> getResult = result.listCells().stream().collect(Collectors.toMap(e -> Bytes.toString(CellUtil.cloneQualifier(e)), e -> Bytes.toString(CellUtil.cloneValue(e))));
getResult.entrySet().stream().forEach(e -> System.out.printf("Qualifier : %s : Value : %s%n", e.getKey(), e.getValue()));
System.out.println("---------Scan---------");
Scan scan = new Scan();
ResultScanner resultScan = table.getScanner(scan);
resultScan.forEach(e -> {
System.out.printf("Row \"%s\"%n", Bytes.toString(e.getRow()));
Map<String, String> getResultScan = e.listCells().stream().collect(Collectors.toMap(d -> Bytes.toString(CellUtil.cloneQualifier(d)), d -> Bytes.toString(CellUtil.cloneValue(d))));
getResultScan.entrySet().stream().forEach(d -> System.out.printf("column \"%s\", value \"%s\"%n", d.getKey(), d.getValue()));
System.out.println();
});
+0
哪个maven依赖包含api? – Normal 2016-05-13 15:47:52
+1
我使用org.apache.hbase:hbase-client:1.1.2 – 2016-11-25 14:04:25
相关问题
- 1. 从HBase中读取数据
- 2. 从Reducer中的HBase中读取数据
- 3. 我如何从Spark中的Hbase表读取数据?
- 4. 从HBase中读取烫伤
- 5. HBase读取数据返回null
- 6. 在Spark Streaming中读取Hbase数据
- 7. 从亚马逊hbase阅读数据
- 8. 如何从数据流读取数据
- 9. 如何将HBase表读取为pyspark数据框?
- 10. 通过使用Spark和JAVA从HBase读取数据
- 11. 通过hive插入从hbase读取数据的问题
- 12. Python Mapreduce在从hbase读取的重复数据上运行
- 13. 从hbase读取数据并插入到postgres
- 14. 使用Spark中的Get命令从Hbase读取数据
- 15. 使用python通过节俭从hbase读取数据时出错
- 16. 如何从Hive中读取hbase当前版本和以前版本的数据
- 17. 如何读取HBase中的空值java
- 18. 从HBase获取二进制数据
- 19. 从HDFS读写HBASE
- 20. 从Java的HBase中读取Avro记录
- 21. 从txt文件读取并写入HBase
- 22. 我如何使用nutch从hbase抓取数据
- 23. 如何从Sling读取数据到Jquery?
- 24. 如何从csv文件读取数据
- 25. 如何从Excel文件读取数据?
- 26. 如何从ByteBuffer读取数据
- 27. 如何从绑定bean读取数据?
- 28. 如何从WebClient.UploadData中读取数据
- 29. 如何从Amazon DynamoDB读取数据?
- 30. 如何从powerpoint读取数据
感谢您的示例代码。 – WackoMax 2010-03-15 14:14:20
这可能会变得很慢,因为它为每个'scanner.next()返回一行,你可以先用's.setCashing(numberOfRows)'启用缓存,如[here](http://elsoufy.blogspot)所述。 FR/2014/06/gettint,开始与 - hbase.html)。 – bachr 2014-06-25 10:17:52