0

如何使用 Java 中的 TableAPI 从 Oracle NoSQL 表中获取所有行?我可以通过主键值获取记录。例子:

    TableAPI tableH = kvstore.getTableAPI();
    Table myTable = tableH.getTable("myTable");
    PrimaryKey key = myTable.createPrimaryKey();
    key.put("item", "Hat");

    List<Row> myRows = null;
    try {
        myRows = tableH.multiGet(key, null, null);
    } catch (ConsistencyException ce) {
    } catch (RequestTimeoutException re) {
    }
    for (Row theRow: myRows) {
        String itemType = theRow.get("item").asString().get();
    }
    System.out.println(itemType);

但我无法获得主键值。

4

1 回答 1

0

要修改您的示例,您可以通过对表使用空 PrimaryKey 并使用 TableAPI.tableIterator() 方法之一来获取所有行。关于示例的注释,TableAPI.getTable() 将执行远程调用,因此表句柄应该被隐藏并重用。

TableAPI tableH = kvstore.getTableAPI();
/* 
 * get the Table, but be careful about doing this frequently,
 * as it performs a remote call.
 */
Table myTable = tableH.getTable("myTable");
PrimaryKey key = myTable.createPrimaryKey();

try {

    /* create and use an iterator on the Row value */
    TableIterator<Row> rowIter = tableH.tableIterator(key, null, null);
    while (rowIter.hasNext()) {
        Row row = rowIter.next();
        /* do something */
    }

    /* 
     * Or...
     * create and use an iterator on the PrimaryKey values.
     * This is much faster than fetching the data as well if
     * you only need fields in the primary key.
     */
    TableIterator<PrimaryKey> keyIter =
              tableH.tableKeysIterator(key, null, null);
    while (keyIter.hasNext()) {
        PrimaryKey key = rowIter.next();
        /* do something */
    }
} catch (FaultException fe) {
}
于 2016-03-21T14:38:32.320 回答