Amazon DynamoDB
开发人员指南 (API 版本 2012-08-10)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

步骤 4:查询和扫描数据

您可以使用 query 方法来检索表中的数据。您必须指定分区键值;排序键是可选项。

Movies 表的主键由以下部分组成:

  • year – 分区键。属性类型是数字。 

  • title – 排序键。属性类型是字符串。

要查找一年当中发行的所有电影,您只需指定 year。您还可以提供 title 以根据某种条件 (作用于排序键) 来检索电影子集;例如,查找 2014 年发行并且标题以字母“A”开头的电影。

除了 query 方法以外,您还可以使用 scan 方法检索所有表数据。

要了解有关查询和扫描数据的更多信息,请分别参阅在 DynamoDB 中使用查询在 DynamoDB 中使用扫描

步骤 4.1:查询 - 一年中发行的所有电影

此步骤中包含的程序将检索 year 1985 发行的所有电影。

  1. 将以下程序复制并粘贴到名为 MoviesQuery01.html 的文件中。

    <!-- <!-- Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script> <script> AWS.config.update({ region: "us-west-2", endpoint: 'http://localhost:8000', // accessKeyId default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. accessKeyId: "fakeMyKeyId", // secretAccessKey default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. secretAccessKey: "fakeSecretAccessKey" }); var docClient = new AWS.DynamoDB.DocumentClient(); function queryData() { document.getElementById('textarea').innerHTML += "Querying for movies from 1985."; var params = { TableName : "Movies", KeyConditionExpression: "#yr = :yyyy", ExpressionAttributeNames:{ "#yr": "year" }, ExpressionAttributeValues: { ":yyyy":1985 } }; docClient.query(params, function(err, data) { if (err) { document.getElementById('textarea').innerHTML += "Unable to query. Error: " + "\n" + JSON.stringify(err, undefined, 2); } else { document.getElementById('textarea').innerHTML += "Querying for movies from 1985: " + "\n" + JSON.stringify(data, undefined, 2); } }); } </script> </head> <body> <input id="queryData" type="button" value="Query" onclick="queryData();" /> <br><br> <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea> </body> </html>

    注意

    ExpressionAttributeNames 提供名称替换功能。使用此参数是因为 year 是 Amazon DynamoDB 中的保留字。您不能直接在任何表达式中使用它,包括 KeyConditionExpression。因此,您使用表达式属性名称 #yr

    ExpressionAttributeValues 提供值替换功能。使用此参数是因为您不能在任何表达式 (包括 KeyConditionExpression) 中使用文本。因此,您使用表达式属性值 :yyyy

  2. 在您的浏览器中打开 MoviesQuery01.html 文件。

  3. 选择 Query (查询)

注意

前面的程序演示了如何按主键属性对表进行查询。在 DynamoDB 中,您可以选择在表上创建一个或多个二级索引,并采用查询表的相同方法来查询这些索引。二级索引允许对非键属性进行查询,从而为您的应用程序带来额外的灵活性。有关更多信息,请参阅使用二级索引改进数据访问

步骤 4.2:查询 - 一年中发行的所有包含特定标题的电影

此步骤中包含的程序将检索 year 1992 发行并且 title 以字母“A”至字母“L”开头的所有电影。

  1. 将以下程序复制并粘贴到名为 MoviesQuery02.html 的文件中。

    <!-- <!-- Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script> <script> AWS.config.update({ region: "us-west-2", endpoint: 'http://localhost:8000', // accessKeyId default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. accessKeyId: "fakeMyKeyId", // secretAccessKey default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. secretAccessKey: "fakeSecretAccessKey" }); var docClient = new AWS.DynamoDB.DocumentClient(); function queryData() { document.getElementById('textarea').innerHTML += "Querying for movies from 1985."; var params = { TableName : "Movies", ProjectionExpression:"#yr, title, info.genres, info.actors[0]", KeyConditionExpression: "#yr = :yyyy and title between :letter1 and :letter2", ExpressionAttributeNames:{ "#yr": "year" }, ExpressionAttributeValues: { ":yyyy":1992, ":letter1": "A", ":letter2": "L" } }; docClient.query(params, function(err, data) { if (err) { document.getElementById('textarea').innerHTML += "Unable to query. Error: " + "\n" + JSON.stringify(err, undefined, 2); } else { document.getElementById('textarea').innerHTML += "Querying for movies from 1992 - titles A-L, with genres and lead actor: " + "\n" + JSON.stringify(data, undefined, 2); } }); } </script> </head> <body> <input id="queryData" type="button" value="Query" onclick="queryData();" /> <br><br> <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea> </body> </html>
  2. 在您的浏览器中打开 MoviesQuery02.html 文件。

  3. 选择 Query (查询)

步骤 4.3:扫描

scan 方法将读取整个表中的所有项目,并返回表中的所有数据。您可提供一个可选的 filter_expression,以便仅返回符合条件的项目。但是,筛选条件仅在扫描整个表后应用。

以下程序将扫描整个 Movies 表,表中包含约 5000 个项目。扫描时可指定可选筛选条件,以便仅检索 20 世纪 50 年代以来的电影 (约 100 个项目),同时舍弃所有其他项目。

  1. 将以下程序复制并粘贴到名为 MoviesScan.html 的文件中。

    <!-- <!-- Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <html> <head> <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script> <script> AWS.config.update({ region: "us-west-2", endpoint: 'http://localhost:8000', // accessKeyId default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. accessKeyId: "fakeMyKeyId", // secretAccessKey default can be used while using the downloadable version of DynamoDB. // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead. secretAccessKey: "fakeSecretAccessKey" }); var docClient = new AWS.DynamoDB.DocumentClient(); function scanData() { document.getElementById('textarea').innerHTML += "Scanning Movies table." + "\n"; var params = { TableName: "Movies", ProjectionExpression: "#yr, title, info.rating", FilterExpression: "#yr between :start_yr and :end_yr", ExpressionAttributeNames: { "#yr": "year", }, ExpressionAttributeValues: { ":start_yr": 1950, ":end_yr": 1959 } }; docClient.scan(params, onScan); function onScan(err, data) { if (err) { document.getElementById('textarea').innerHTML += "Unable to scan the table: " + "\n" + JSON.stringify(err, undefined, 2); } else { // Print all the movies document.getElementById('textarea').innerHTML += "Scan succeeded. " + "\n"; data.Items.forEach(function(movie) { document.getElementById('textarea').innerHTML += movie.year + ": " + movie.title + " - rating: " + movie.info.rating + "\n"; }); // Continue scanning if we have more movies (per scan 1MB limitation) document.getElementById('textarea').innerHTML += "Scanning for more..." + "\n"; params.ExclusiveStartKey = data.LastEvaluatedKey; docClient.scan(params, onScan); } } } </script> </head> <body> <input id="scanData" type="button" value="Scan" onclick="scanData();" /> <br><br> <textarea readonly id= "textarea" style="width:400px; height:800px"></textarea> </body> </html>

    在代码中,请注意以下情况:

    • ProjectionExpression 用于指定要在扫描结果中包含的属性。

    • FilterExpression 用于指定一个条件,以便仅返回符合条件的项目。所有其他项目都将被舍弃。

  2. 在您的浏览器中打开 MoviesScan.html 文件。

  3. 选择 Scan (扫描)

注意

您还可以对在表上创建的任意二级索引使用 Scan 操作。有关更多信息,请参阅使用二级索引改进数据访问