使用Ruby了解AWS DynamoDB中的属性

 卡夫卡咯 发布于 2022-12-09 19:01

我似乎无法围绕DynamoDB的AWS Ruby SDK文档(或者更具体地说是DynamoDB数据模型的概念).

具体来说,我一直在阅读:http://docs.aws.amazon.com/AWSRubySDK/latest/frames.html#!AWS/DynamoDB.html

注意:我也已经阅读了数据模型文档,但它仍然没有下沉; 我希望在Ruby中有一个恰当的例子,清除我的困惑

在下面的代码片段中,我创建了一个名为"my_books"的表,其中有一个名为"item_id"的primary_key,它是一个哈希键(不是哈希/范围组合)......

dyn = AWS::DynamoDB::Client::V20120810.new
# => #

dyn.create_table({
  :attribute_definitions => [
    { :attribute_name => "item_id", :attribute_type => "N" }
  ],
  :table_name => "my_books",
  :key_schema => [
    { :attribute_name => "item_id", :key_type => "HASH" },
  ],
  :provisioned_throughput => {
    :read_capacity_units  => 10,
    :write_capacity_units => 10
  }
})
# => {:table_description=>{:attribute_definitions=>[{:attribute_name=>"item_id", :attribute_type=>"N"}], :table_name=>"my_books", :key_schema=>[{:attribute_name=>"item_id", :key_type=>"HASH"}], :table_status=>"ACTIVE", :creation_date_time=>2014-11-24 16:59:47 +0000, :provisioned_throughput=>{:number_of_decreases_today=>0, :read_capacity_units=>10, :write_capacity_units=>10}, :table_size_bytes=>0, :item_count=>0}}

dyn.list_tables
# => {:table_names=>["my_books"]}

dyn.scan :table_name => "my_books"
# => {:member=>[], :count=>0, :scanned_count=>0}

然后我尝试使用新项目填充表格.我的理解是我应该指定item_id(这是主键)的数值,然后我可以为我添加到表中的新项目/记录/文档指定其他属性...

dyn.put_item(
  :table_name => "my_books",
  :item => {
    "item_id" => 1,
    "item_title" => "My Book Title",
    "item_released" => false
  }
)

但是最后一个命令返回以下错误:

expected hash value for value at key item_id of option item

所以虽然我不太明白哈希会是什么,但我尝试这样做:

dyn.put_item(
  :table_name => "my_books",
  :item => {
    "item_id" => { "N" => 1 },
    "item_title" => "My Book Title",
    "item_released" => false
  }
)

但现在这会返回以下错误...

expected string value for key N of value at key item_id of option item

我尝试了不同的变化,但似乎无法弄清楚它是如何工作的?


编辑/更新:根据Uri Agassi的建议 - 我将值更改1"1".我不确定为什么必须引用它,因为我已经将类型定义为数字而不是字符串,但是好吧让我们接受这个并继续前进.

1 个回答
  • 我终于弄清楚了解DynamoDB数据模型和使用Ruby SDK所需的大部分内容.

    下面是我的示例代码,希望能帮助别人,我已经有了一个完全充实的例子在这里:https://gist.github.com/Integralist/9f9f2215e001b15ac492#file-3-dynamodb-irb-session-rb

    # https://github.com/BBC-News/alephant-harness can automate the below set-up when using Spurious
    # API Documentation http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations.html
    # Ruby SDK API Documentation http://docs.aws.amazon.com/AWSRubySDK/latest/frames.html#!AWS/DynamoDB/Client/V20120810.html
    
    require "aws-sdk"
    require "dotenv"
    require "spurious/ruby/awssdk/helper"
    
    Spurious::Ruby::Awssdk::Helper.configure
    # => <AWS::Core::Configuration>
    
    Dotenv.load(
      File.join(
        File.dirname(__FILE__), "config", "development", "env.yaml"
      )
    )
    # => {"AWS_REGION"=>"eu-west-1", "AWS_ACCESS_KEY_ID"=>"development_access", "AWS_SECRET_ACCESS_KEY"=>"development_secret", "DYNAMO_LU"=>"development_lookup", "DYNAMO_SQ"=>"development_sequence", "SQS_QUEUE"=>"development_queue", "S3_BUCKET"=>"development_bucket"}
    
    dyn = AWS::DynamoDB::Client.new :api_version => "2012-08-10"
    dyn = AWS::DynamoDB::Client::V20120810.new
    # => #<AWS::DynamoDB::Client::V20120810>
    
    dyn.create_table({
      # This section requires us to define our primary key 
      # Which will be called "item_id" and it must be a numerical value
      :attribute_definitions => [
        { :attribute_name => "item_id", :attribute_type => "N" }
      ],
      :table_name => "my_books",
      # The primary key will be a simple Hash key (not a Hash/Range which requires both key types to be provided)
      # The attributes defined above must be included in the :key_schema Array
      :key_schema => [
        { :attribute_name => "item_id", :key_type => "HASH" }
      ],
      :provisioned_throughput => {
        :read_capacity_units  => 10,
        :write_capacity_units => 10
      }
    })
    # => {:table_description=>{:attribute_definitions=>[{:attribute_name=>"item_id", :attribute_type=>"N"}], :table_name=>"my_books", :key_schema=>[{:attribute_name=>"item_id", :key_type=>"HASH"}], :table_status=>"ACTIVE", :creation_date_time=>2014-11-24 16:59:47 +0000, :provisioned_throughput=>{:number_of_decreases_today=>0, :read_capacity_units=>10, :write_capacity_units=>10}, :table_size_bytes=>0, :item_count=>0}}
    
    dyn.list_tables
    # => {:table_names=>["my_books"]}
    
    dyn.scan :table_name => "my_books"
    # => {:member=>[], :count=>0, :scanned_count=>0}
    
    dyn.put_item(
      :table_name => "my_books",
      :item => {
        "item_id" => { "N" => "1" }, # oddly this needs to be a String and not a strict Integer?
        "item_title" => { "S" => "My Book Title"},
        "item_released" => { "B" => "false" }
      }
    )
    # Note: if you use an "item_id" that already exists, then the item will be updated.
    #       Unless you use the "expected" conditional feature
    
    dyn.put_item(
      :table_name => "my_books",
      :item => {
        "item_id" => { "N" => "1" }, # oddly this needs to be a String and not a strict Integer?
        "item_title" => { "S" => "My Book Title"},
        "item_released" => { "B" => "false" }
      },
      # The :expected key specifies the conditions of our "put" operation.
      # If "item_id" isn't NULL (i.e. it exists) then our condition has failed.
      # This means we only write the value when the key "item_id" hasn't been set.
      :expected => {
        "item_id" => { :comparison_operator => "NULL" }
      }
    )
    # AWS::DynamoDB::Errors::ConditionalCheckFailedException: The conditional check failed
    
    dyn.scan :table_name => "my_books"
    # => {:member=>[{"item_id"=>{:n=>"1"}, "item_title"=>{:s=>"My Book Title"}, "item_released"=>{:b=>"false"}}], :count=>1, :scanned_count=>1}
    
    dyn.query :table_name => "my_books", :consistent_read => true, :key_conditions => {
      "item_id" => {
        :comparison_operator => "EQ",
        :attribute_value_list => [{ "n" => "1" }]
      },
      "item_title" => {
        :comparison_operator => "EQ",
        :attribute_value_list => [{ "s" => "My Book Title" }]
      }
    }
    # => {:member=>[{"item_id"=>{:n=>"1"}, "item_title"=>{:s=>"My Book Title"}, "item_released"=>{:b=>"false"}}], :count=>1, :scanned_count=>1}
    
    dyn.query :table_name => "my_books", 
      :consistent_read => true, 
      :select => "SPECIFIC_ATTRIBUTES",
      :attributes_to_get => ["item_title"],
      :key_conditions => {
      "item_id" => {
        :comparison_operator => "EQ",
        :attribute_value_list => [{ "n" => "1" }]
      },
      "item_title" => {
        :comparison_operator => "EQ",
        :attribute_value_list => [{ "s" => "My Book Title" }]
      }
    }
    # => {:member=>[{"item_title"=>{:s=>"My Book Title"}}], :count=>1, :scanned_count=>1}
    
    dyn.delete_item(
      :table_name => "my_books",
      :key => {
        "item_id" => { "n" => "1" }
      }
    )
    # => {:member=>[], :count=>0, :scanned_count=>0}
    

    2022-12-11 02:09 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有