I seem to be unable to plunge my head into the AWS Ruby SDK documentation for DynamoDB (or, more specifically, the DynamoDB data model concept).
In particular, I read: http://docs.aws.amazon.com/AWSRubySDK/latest/frames.html#!AWS/DynamoDB.html
Note. I also read the data model documentation and still don't dive; I am hoping for the right example in Ruby by clearing my confusion.
In the following code snippet, I create a table called "my_books" that has a primary_key named "item_id" and this is a hash key (not a Hash / Range combination) ...
dyn = AWS::DynamoDB::Client::V20120810.new # => #<AWS::DynamoDB::Client::V20120810> 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}
Then I will try to populate the table with a new element. I understand that I have to specify a numeric value for item_id
(this is the primary key), and then I could specify other attributes for the new item / record / document that I add to the table ...
dyn.put_item( :table_name => "my_books", :item => { "item_id" => 1, "item_title" => "My Book Title", "item_released" => false } )
But this last command returns the following error:
expected hash value for value at key item_id of option item
So, although I do not quite understand what the hash is made of, I try to do this:
dyn.put_item( :table_name => "my_books", :item => { "item_id" => { "N" => 1 }, "item_title" => "My Book Title", "item_released" => false } )
But now it returns the following error ...
expected string value for key N of value at key item_id of option item
I tried different options but can't figure out how this works?
EDIT / UPDATE : as suggested by Uri Agassi - I changed the value from 1
to "1"
. I'm not sure why this should be indicated, as I defined the type as a number, not a string, but OK, just to accept it and move on.