如何使用Java与Maprdb连接以检索记录

时间:2017-05-13 12:12:00

标签: java hbase mapr

我正在尝试使用MaprdbJava建立联系。我可以使用java代码创建Maprdb表,但我无法扫描表。

<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 Maprdb with Java Github

这方面的任何领导都将是完整的。

1 个答案:

答案 0 :(得分:0)

在这个级别上它与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
  }

}