Java 如何从Hbase读取数据?

Java 如何从Hbase读取数据?,java,hbase,Java,Hbase,您好,我已经习惯了SQL,但我需要从HBase表中读取数据。这方面的任何帮助都会很好。一本书或者可能只是一些从表中读取的示例代码。有人说使用扫描仪就可以了,但我不知道如何使用它。来自: 我使用了它,但要获取字符串值,必须使用方法getValue from Result byte[] bytes = rr.getValue(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier")); System.out.println(new

您好,我已经习惯了SQL,但我需要从HBase表中读取数据。这方面的任何帮助都会很好。一本书或者可能只是一些从表中读取的示例代码。有人说使用扫描仪就可以了,但我不知道如何使用它。

来自:


我使用了它,但要获取字符串值,必须使用方法getValue from Result

byte[] bytes = rr.getValue(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
System.out.println(new String(bytes));

我想提供没有弃用方法的解决方案

    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();
    });
Configuration=HBaseConfiguration.create();
Connection=ConnectionFactory.createConnection(conf);
Admin=connection.getAdmin();
//列出表格
Arrays.stream(admin.listTables()).forEach(System.out::println);
//让我们在“mytable”中插入一些数据并获取该行
TableName TableName=TableName.valueOf(“测试1”);
Table=connection.getTable(tableName);
//放
Put thePut=新的Put(字节到字节(“行键1”);
字符串columnFamily=“m”;
字符串columnQualifier1=“col1”;
字符串outValue1=“value1”;
字符串columnQualifier2=“col2”;
String outValue2=“value2”;
put.add列(Bytes.toBytes(columnFamily)、Bytes.toBytes(columnQualifier1)、Bytes.toBytes(outValue1));
put.add列(Bytes.toBytes(columnFamily)、Bytes.toBytes(columnQualifier2)、Bytes.toBytes(outValue2));
表.put(输出);
//得到
Get-theGet=new-Get(Bytes.toBytes(“rowkey1”);
结果=table.get(theGet);
//在第一列中获取值
字符串inValue1=Bytes.toString(result.value());
//按ColumnFamily和ColumnName获取值
byte[]inValueByte=result.getValue(Bytes.toBytes(columnFamily),Bytes.toBytes(columnQualifier1));
字符串inValue2=Bytes.toString(inValueByte);
//循环搜索结果
对于(单元格:result.listCells()){
字符串限定符=Bytes.toString(CellUtil.cloneQualifier(cell));
字符串值=Bytes.toString(CellUtil.cloneValue(cell));
System.out.printf(“限定符:%s:值:%s%n”,限定符,值);
}
//根据结果创建地图并打印
Map 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(“限定符:%s:Value:%s%n”,e.getKey(),e.getValue());
System.out.println(“-----------扫描------”);
扫描=新扫描();
ResultScanner resultScan=table.getScanner(扫描);
结果可获得(e->{
System.out.printf(“行\%s\%n”,Bytes.toString(e.getRow());
Map 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(“列\%s\”,值\%s\%n),d.getKey(),d.getValue());
System.out.println();
});

这可能会变慢,因为它会为每个
扫描仪返回一行。下一步()
,您可以首先使用
s.setCashing(numberOfRows)
启用缓存,如前所述。哪个maven依赖项包含该api?我使用org.apache.hbase:hbase client:1.1.2
    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();
    });