使用查询 API 提取数据

可以使用 QueryRequest 构造查询来过滤 NoSQL 表中的数据。

要执行查询,请使用 NoSQLHandle.query() API。有关各种类和方法的更多详细信息,请参阅 Oracle NoSQL Java SDK API Reference

可通过两种方法获取查询结果:使用迭代器或循环访问部分结果。
  • 迭代器:使用 NoSQLHandle.queryIterable(QueryRequest) 获取包含所有结果的可迭代。
  • 部分结果:要计算和检索查询的完整结果集,通常必须多次(通过 NoSQLHandle.query(oracle.nosql.driver.ops.QueryRequest))执行同一 QueryRequest 实例。每次执行都会返回一个 QueryRequest,其中包含结果集的子集。
String sqlstmt_allrows="SELECT * FROM stream_acct";
private static void fetchRows(NoSQLHandle handle,String sqlstmt) 
                                              throws Exception {
   try (
      QueryRequest queryRequest = 
            new QueryRequest().setStatement(sqlstmt_allrows);

      QueryIterableResult results = 
            handle.queryIterable(queryRequest)){

      for (MapValue res : results) {
         System.out.println("\t" + res);
      }
   }
}
还可以使用查询中的 WHERE 子句应用筛选条件。
String sqlstmt_allrows=
"SELECT account_expiry, acct.acct_data.lastName, 
acct.acct_data.contentStreamed[].showName 
FROM stream_acct acct WHERE acct_id=1";
此处的示例下载完整代码 QueryData.java

注意:

要从子表提取数据,请在 sql 语句中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.java,以了解如何从此处的父子表中提取数据。

要执行查询,请使用 borneo.NoSQLHandle.query() 方法。

可通过两种方法获取查询结果:使用迭代器或循环访问部分结果。
  • 使用 borneo.NoSQLHandle.query_iterable() 获取包含查询所有结果的可迭代。
  • 可以使用 borneo.NoSQLHandle.query() 方法循环处理部分结果。例如,要执行 SELECT 查询以从表中读取数据,borneo.QueryResult 包含一组结果。如果 borneo.QueryRequest.is_done() 返回 False,则可能有更多结果,因此查询通常应在循环中运行。单个请求可能不返回任何结果,但查询仍未完成,这表明查询循环应继续。
sqlstmt = 'SELECT * FROM stream_acct'

def fetch_data(handle,sqlstmt):
   request = QueryRequest().set_statement(sqlstmt)
   print('Query results for: ' + sqlstmt)

   result = handle.query(request)
   for r in result.get_results():
      print('\t' + str(r))
还可以使用查询中的 WHERE 子句应用筛选条件。
sqlstmt = 'SELECT account_expiry, acct.acct_data.lastName,
acct.acct_data.contentStreamed[].showName 
FROM stream_acct acct WHERE acct_id=1'
Download the full code QueryData.py from the examples here.

注意:

要从子表提取数据,请在 sql 语句中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.py,以了解如何从此处的父子表中提取数据。

要执行查询,请使用 Client.Query 函数。在云服务上执行时,单个查询请求读取的数据量受系统默认值的限制,可以使用 QueryRequest.MaxReadKB 进一步限制。这限制了读取的数据量,而不是返回的数据量,这意味着查询可以返回零个结果,但仍有更多数据要读取。因此,查询应始终在循环中运行,获取更多结果,直到 QueryRequest.IsDone() 返回 true,指示查询已完成。

querystmt := "select * FROM stream_acct"

func fetchData(client *nosqldb.Client, err error, 
               tableName string, querystmt string)(){
   prepReq := &nosqldb.PrepareRequest{ Statement: querystmt,}

   prepRes, err := client.Prepare(prepReq)
   if err != nil {
      fmt.Printf("Prepare failed: %v\n", err)
      return
   }

   queryReq := &nosqldb.QueryRequest{
		 PreparedStatement: &prepRes.PreparedStatement,}
   var results []*types.MapValue

   for {
      queryRes, err := client.Query(queryReq)
      if err != nil {
         fmt.Printf("Query failed: %v\n", err)
	  return
      }
      res, err := queryRes.GetResults()

      if err != nil {
         fmt.Printf("GetResults() failed: %v\n", err)
  	return
      }

      results = append(results, res...)
      if queryReq.IsDone() {
         break
      }
   }
   for i, r := range results {
      fmt.Printf("\t%d: %s\n", i+1, 
                  jsonutil.AsJSON(r.Map()))
   }
}
还可以使用查询中的 WHERE 子句应用筛选条件。
querystmt := "SELECT account_expiry, acct.acct_data.lastName, 
acct.acct_data.contentStreamed[].showName 
FROM stream_acct acct where acct_id=1"
Download the full code QueryData.go from the examples here.

注意:

要从子表提取数据,请在 sql 语句中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.go,以了解如何从此处的父子表中提取数据。
可以使用以下方法之一查询 NoSQL 表中的数据。有关方法详细信息,请参见 NoSQLClient 类。
  1. 使用 query 方法执行查询。此方法返回 QueryResult承诺,这是一个包含结果行数组的纯 JavaScript 对象以及续行键。可以通过两种方式使用 query 方法:
    • 对于最多只能访问一行的查询,只能调用 query 方法一次。这些查询只能包括基于主键的 SELECT 语句(where 子句必须基于完整的主键指定相等性)。在所有其他情况下,可以在循环中使用 queryqueryIterable 方法。
    • 可以在循环中调用 query 方法来检索多行。由于查询返回的数据量受系统缺省值的限制,可以通过在 queryQueryOpt 参数中设置 maxReadKB 属性进一步限制,因此 query 方法的一次调用无法返回所有可用结果。要解决此问题,请以循环方式运行查询,直到 QueryResult 中的 continuationKey 变为 null/undefined
  2. 使用 queryIterable 方法迭代查询结果。此方法返回一个可通过 for-await-of 循环迭代的可迭代对象。您无需在此方法中管理延续。

    注意:

    使用 queryIterable 方法时,还可以将 QueryOpt 参数与 continuationKey 以外的属性一起使用。
JavaScript: Download the full code QueryData.js from the examples here.
const querystmt = 'SELECT * FROM stream_acct';

async function fetchData(handle,querystmt) {
    const opt = {};

    try {
        do {
            const result = await handle.query(querystmt, opt);
            for(let row of result.rows) {
                console.log('  %O', row);
            }

            opt.continuationKey = result.continuationKey;
        } while(opt.continuationKey);
    } catch(error) {
        console.error('  Error: ' + error.message);
    }
}
还可以使用查询中的 WHERE 子句应用筛选条件。
const querystmt = 
'SELECT account_expiry, acct.acct_data.lastName, 
acct.acct_data.contentStreamed[].showName 
FROM stream_acct acct WHERE acct_id=1';
TypeScript:You can use the same methods described in JavaScript above for TypeScript. You can also supply an optional query result schema as the type parameter to the query method to provide type hints for the rows returned in the QueryResult. This need not be the same as table row schema (unless using SELECT * query) as the query can include projections, name aliases, aggregate values, and so forth. Download the full code QueryData.ts from the examples here.
interface StreamInt {
   acct_Id: Integer;
   profile_name: String;
   account_expiry: TIMESTAMP;
   acct_data: JSON;
}
/* fetches data from the table */
async function fetchData(handle: NoSQLClient,querystmt: string) {
   const opt = {};
   try {
      do {
         const result = await handle.query<StreamInt>(querystmt, opt);
         for(let row of result.rows) {
            console.log('  %O', row);
         }
         opt.continuationKey = result.continuationKey;
      } while(opt.continuationKey);
   } catch(error) {
      console.error('  Error: ' + error.message);
   }
}
还可以使用查询中的 WHERE 子句应用筛选条件。
const querystmt = 
'SELECT account_expiry, acct.acct_data.lastName, 
acct.acct_data.contentStreamed[].showName 
FROM stream_acct acct WHERE acct_id=1';

注意:

要从子表提取数据,请在 sql 语句中指定表 (parent_tablename.child_tablename) 的全名。下载完整的 JavaScript 代码 TableJoins.js 此处和完整的 TypeScript 代码 TableJoins.ts 此处,了解如何从父子表中提取数据。

要执行查询,可以调用 QueryAsync 方法或调用 GetQueryAsyncEnumerable 方法并迭代生成的异步可枚举。可以将选项作为 QueryOptions 传递给每个方法。QueryAsync 方法返回 Task<QueryResult<RecordValue>>QueryResult 将查询结果作为 RecordValue 实例列表以及其他信息。当查询指定完整的主键时,只需调用一次 QueryAsync 就足够了。查询返回的数据量受系统限制。也可以通过设置 QueryOptionsMaxReadKB 属性来进一步限制。这意味着对 QueryAsync 的一次调用可能不会返回所有可用结果。这种情况是通过使用延续键来解决的。QueryResult 中的连续关键字非空值表示可能有更多的查询结果可用。这意味着查询应以循环方式运行,循环直到继续键变为 Null。有关所有类和方法的更多详细信息,请参阅 Oracle NoSQL Dotnet SDK API Reference

private const string querystmt ="SELECT * FROM stream_acct";

private static async Task fetchData(NoSQLClient client,String querystmt){
   var queryEnumerable = client.GetQueryAsyncEnumerable(querystmt);
   await DoQuery(queryEnumerable);
 }

//function to display result
private static async Task 
   DoQuery(IAsyncEnumerable<QueryResult<RecordValue>> queryEnumerable){
   Console.WriteLine("  Query results:");

   await foreach (var result in queryEnumerable) {
      foreach (var row in result.Rows)
      {
         Console.WriteLine();
         Console.WriteLine(row.ToJsonString());
      }
   }
}
还可以使用查询中的 WHERE 子句应用筛选条件。
private const string querystmt =
"SELECT account_expiry, acct.acct_data.lastName, 
acct.acct_data.contentStreamed[].showName 
FROM stream_acct acct WHERE acct_id=1";
Download the full code QueryData.cs from the examples here.

注意:

要从子表提取数据,请在 sql 语句中指定表 (parent_tablename.child_tablename) 的全名。从示例下载完整代码 TableJoins.cs,以了解如何从此处的父子表中提取数据。