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

步骤 4:查询和扫描数据

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

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

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

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

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

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

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

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

以下程序将检索 year 1985 发行的所有电影。

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

    # # 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. # require "aws-sdk" Aws.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }) dynamodb = Aws::DynamoDB::Client.new table_name = "Movies" params = { table_name: table_name, key_condition_expression: "#yr = :yyyy", expression_attribute_names: { "#yr" => "year" }, expression_attribute_values: { ":yyyy" => 1985 } } puts "Querying for movies from 1985."; begin result = dynamodb.query(params) puts "Query succeeded." result.items.each{|movie| puts "#{movie["year"].to_i} #{movie["title"]}" } rescue Aws::DynamoDB::Errors::ServiceError => error puts "Unable to query table:" puts "#{error.message}" end

    注意

    • expression_attribute_names 提供名称替换功能。我们使用此参数是因为 year 是 DynamoDB 中的保留字 — 您不能直接在任何表达式中使用它,包括 KeyConditionExpression。您可使用表达式属性名称 #yr 来解决此问题。

    • expression_attribute_values 提供值替换功能。使用此参数是因为您不能在任何表达式 (包括 key_condition_expression) 中使用文本。您可使用表达式属性值 :yyyy 来解决此问题。

  2. 要运行该程序,请键入以下命令:

    ruby MoviesItemQuery01.rb

注意

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

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

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

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

    # # 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. # require "aws-sdk" Aws.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }) dynamodb = Aws::DynamoDB::Client.new table_name = "Movies" params = { table_name: table_name, projection_expression: "#yr, title, info.genres, info.actors[0]", key_condition_expression: "#yr = :yyyy and title between :letter1 and :letter2", expression_attribute_names: { "#yr" => "year" }, expression_attribute_values: { ":yyyy" => 1992, ":letter1" => "A", ":letter2" => "L" } } puts "Querying for movies from 1992 - titles A-L, with genres and lead actor"; begin result = dynamodb.query(params) puts "Query succeeded." result.items.each{|movie| print "#{movie["year"].to_i}: #{movie["title"]} ... " movie['info']['genres'].each{|gen| print gen + " " } print " ... #{movie["info"]["actors"][0]}\n" } rescue Aws::DynamoDB::Errors::ServiceError => error puts "Unable to query table:" puts "#{error.message}" end
  2. 要运行该程序,请键入以下命令:

    ruby MoviesQuery02.rb

步骤 4.3:扫描

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

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

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

    # # 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. # require "aws-sdk" Aws.config.update({ region: "us-west-2", endpoint: "http://localhost:8000" }) dynamodb = Aws::DynamoDB::Client.new table_name = "Movies" params = { table_name: table_name, projection_expression: "#yr, title, info.rating", filter_expression: "#yr between :start_yr and :end_yr", expression_attribute_names: {"#yr"=> "year"}, expression_attribute_values: { ":start_yr" => 1950, ":end_yr" => 1959 } } puts "Scanning Movies table." begin loop do result = dynamodb.scan(params) result.items.each{|movie| puts "#{movie["year"].to_i}: " + "#{movie["title"]} ... " + "#{movie["info"]["rating"].to_f}" } break if result.last_evaluated_key.nil? puts "Scanning for more..." params[:exclusive_start_key] = result.last_evaluated_key end rescue Aws::DynamoDB::Errors::ServiceError => error puts "Unable to scan:" puts "#{error.message}" end

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

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

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

  2. 要运行该程序,请键入以下命令:

    ruby MoviesScan.rb

注意

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