Hola, estoy acostumbrado a SQL, pero necesito leer datos de una tabla HBase. Cualquier ayuda con esto sería genial. Un libro o tal vez solo un ejemplo de código para leer de la tabla. Alguien dijo que usar un escáner haría el truco, pero no sé cómo usarlo.¿Cómo leer datos de Hbase?
5
A
Respuesta
9
De 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
he usado pero para obtener el valor de cadena que debe utilizar el método de getValue resultado.
byte[] bytes = rr.getValue(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
System.out.println(new String(bytes));
3
me gustaría ofrecer solución sin métodos obsoletos
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
qué dependencia de maven contiene esa API? – Normal
+1
Uso org.apache.hbase: hbase-client: 1.1.2 –
Cuestiones relacionadas
- 1. leer archivo de texto del sistema a Hbase MapReduce
- 2. ¿Cómo mejorar HBase Scanner?
- 3. Importar datos de HDFS a HBase (cdh3u2)
- 4. Cliente Hbase Error de ConnectionLoss para/hbase
- 5. Cómo eliminar todos los datos de solr y hbase
- 6. Hbase: Cómo especificar el nombre de host para Hbase master
- 7. Cómo leer datos de NSDocumentDirectory
- 8. HBase & Mahout - Uso de HBase como almacén de datos/fuente para Mahout - Clasificación
- 9. HBase: ¿Cómo funciona la replicación?
- 10. Cómo escanear filas de HBase eficientemente
- 11. cómo diseñar el esquema Hbase?
- 12. La mejor manera de almacenar datos jerárquicos en hbase
- 13. consultas avanzadas en HBase
- 14. Leer datos de Excel
- 15. Usando HBase para almacenar datos de series de tiempo
- 16. Modelo de datos para Property Graph sobre HBase/Cassandra
- 17. Procesamiento de datos a gran escala Hbase vs Cassandra
- 18. Hbase/Hadoop Query Help
- 19. Familia de columnas Hbase
- 20. Filtro HBASE REST (SingleColumnValueFilter)
- 21. ¿cómo puedo leer datos binarios en C++?
- 22. Leer datos del puerto serie
- 23. ¿Cómo se compara Hive con HBase?
- 24. HBase connection exception
- 25. AWS DynamoDB VS HBase
- 26. Java ORM para Hbase
- 27. Integre Hbase con PHP
- 28. Hbase mapreduce error
- 29. hbase connection refused
- 30. CouchDB vs HBase
Gracias por el código de ejemplo. – WackoMax
Esto puede volverse lento ya que devuelve una fila para cada 'scanner.next()', primero puede habilitar el almacenamiento en caché con 's.setCashing (numberOfRows)' como se describe [aquí] (http: //elsoufy.blogspot. fr/2014/06/gettint-started-with-hbase.html). – bachr