Xin chào, tôi đang sử dụng SQL, nhưng tôi cần đọc dữ liệu từ bảng HBase. Bất kỳ trợ giúp về điều này sẽ là tuyệt vời. Một cuốn sách hoặc có thể chỉ là một số mã mẫu để đọc từ bảng. Có người nói rằng sử dụng máy quét sẽ thực hiện thủ thuật, nhưng tôi không biết cách sử dụng nó.Cách đọc dữ liệu từ HBase?
5
A
Trả lời
9
Từ 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();
}
2
tôi sử dụng đó nhưng để có được những giá trị String bạn phải sử dụng phương thức getValue từ quả.
byte[] bytes = rr.getValue(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
System.out.println(new String(bytes));
3
tôi muốn cung cấp giải pháp mà không phương pháp phản
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 phụ thuộc có chứa api? – Normal
+1
Tôi sử dụng org.apache.hbase: hbase-client: 1.1.2 –
Các vấn đề liên quan
- 1. Nhập dữ liệu từ HDFS sang HBase (cdh3u2)
- 2. Cách xóa tất cả dữ liệu khỏi solr và hbase
- 3. Điền dữ liệu bảng từ trình đọc dữ liệu
- 4. Đọc dữ liệu từ DataGridView trong C#
- 5. Đọc siêu dữ liệu từ nupkg
- 6. Đọc dữ liệu CSV từ một tệp
- 7. Đọc dữ liệu nhị phân từ Console.In
- 8. Đọc dữ liệu từ USB bằng C#?
- 9. Đọc dữ liệu từ Flight Simulator
- 10. Đọc dữ liệu từ thẻ NFC (IsoDep)
- 11. đọc dữ liệu từ Matlab vào Java
- 12. Đọc dữ liệu từ một tệp plist
- 13. Quyền đọc dữ liệu từ Thẻ SD
- 14. cách đọc dữ liệu từ một cột của datagridview
- 15. Cách đọc dữ liệu GET từ URL bằng JavaScript?
- 16. Cách đọc dữ liệu từ tệp XLS (Excel) [Java, Android]
- 17. Cách đọc dữ liệu hdf trong Octave
- 18. Đọc dữ liệu từ bảng tính tài liệu google
- 19. Cách tốt nhất để lưu trữ dữ liệu phân cấp trong HBase
- 20. Cách nhanh nhất để tải dữ liệu hàng loạt vào HBase theo chương trình là gì?
- 21. Cách lấy dữ liệu từ cơ sở dữ liệu Parse.com
- 22. Android - Đọc dữ liệu HTML5 localStorage trực tiếp từ Java
- 23. Đọc dữ liệu từ Microsoft SQL Server vào R
- 24. Làm thế nào để đọc siêu dữ liệu từ mp3?
- 25. Đọc dữ liệu từ tệp Excel trong Objective-C (iPhone)
- 26. Đọc cấu hình .NET từ cơ sở dữ liệu
- 27. Đọc dữ liệu từ phương tiện bị hư hại
- 28. Đọc dữ liệu từ excel 2010 bằng Microsoft.Office.Interop.Excel
- 29. phương pháp Generic để đọc dữ liệu từ DataReader
- 30. Làm thế nào để đọc dữ liệu từ cơ sở dữ liệu SQLite?
Cảm ơn các mẫu mã. – WackoMax
Điều này có thể trở nên chậm khi nó trả về một hàng cho mỗi 'scanner.next()', trước tiên bạn có thể kích hoạt bộ nhớ đệm với 's.setCashing (numberOfRows)' như được mô tả [ở đây] (http: //elsoufy.blogspot. fr/2014/06/gettint-started-with-hbase.html). – bachr