如何用java连接maprdb来检索记录

yftpprvb  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(232)

我正在尝试连接到 MaprdbJava . 我能创造 Maprdb 表的java代码,但我无法扫描表。

<dependencies>
<dependency>
  <groupId>com.mapr.db</groupId>
  <artifactId>maprdb</artifactId>
  <version>5.2.0-mapr</version>
</dependency>
<dependency>
  <groupId>com.mapr.hadoop</groupId>
  <artifactId>maprfs-core</artifactId>
  <version>5.2.0-mapr</version>
</dependency>
<dependency>
  <groupId>com.mapr.hadoop</groupId>
  <artifactId>maprfs</artifactId>
  <version>5.2.0-mapr</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.8-mapr-1703</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.8-mapr-1703</version>

样本代码

Configuration conf = HBaseConfiguration.create();
    String Tablepath = "Tablelocationpath";
    HTable hTable= new HTable(conf,Tablepath);

    Get get2 = new Get(Bytes.toBytes("Rowname"));
    Result r = hTable.get(get2);
    byte[] value = r.getValue(Bytes.toBytes("CF"),
            Bytes.toBytes("COlumnqualifier"));
    String valueStr = Bytes.toString(value);
    System.out.println("GET: " + valueStr);

我已经在关注这个了
Github code 带有java github的maprdb
这方面的任何线索都会有帮助。

mccptt67

mccptt671#

在这个层次上,它与hbase非常相似。
为了扫描,你需要一个 Scan 示例-指定要执行的扫描类型,然后使用该类型获取 Scanner 然后迭代。
大致情况如下:

// Configuration...
String tablePath = "Tablelocationpath";
Configuration conf; 

// set up your Scan object
Scan scan = new Scan();
scan.setStartRow(...);
scan.setStopRow(...);

Connection connection = null;
Table table = null;
ResultScanner scanner = null;
try {
  connection = ConnectionFactory.createConnection(conf);
  table = connection.getTable(TableName.valueOf(tablePath));
  scanner = table.getScanner(scan);
  Iterator it = scanner.iterator();

  while (it.hasNext()) {
    Result e1 = (Result) it.next();
    // do whatever you want to do with your Result, before getting next one
  }

} catch (IOException e) {
  // handle IOException during connecting/scanning

} finally {
  try {
    if (scanner != null) {
      scanner.close();
    }

    if (table != null) {
      table.close();
    }

    if (connection != null) {
      connection.close();
    }
  } catch (IOException e2) {
    // handle error on close
  }

}

相关问题