有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从Oracle NoSQL表中获取所有行?

如何在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);

但我无法获取主键值


共 (1) 个答案

  1. # 1 楼答案

    要修改示例,可以使用表的空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) {
    }