如何从Hbase读取数据?

发布于 2024-08-24 16:38:55 字数 96 浏览 4 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光下慵懒的猫 2024-08-31 16:38:55

来自 网站

// 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();
}

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();
}
回首观望 2024-08-31 16:38:55

我想提供不使用已弃用方法的解决方案

    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();
    });

I would like to offer solution without deprecated methods

    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();
    });
半暖夏伤 2024-08-31 16:38:55

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

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

I used that but to get the String value you must use method getValue from Result.

byte[] bytes = rr.getValue(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"));
System.out.println(new String(bytes));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文