Class: AWS::DynamoDB::Table
- Defined in:
- lib/aws/dynamo_db/table.rb
Overview
Represents a DynamoDB table.
Working with Tables
Dynamo DB allows you to organize data into tables. Tables have a unique name and a key schema. A key schema is comprised of a hash key and an optional range key.
Dynamo DB automatically partitions the data contained in a table across multiple nodes so that the data throughput is not constrained by the scale of a single box. You can reserve the required throughput by specifying a number of reads and writes per second to support.
Creating a Table
To get started you can create a table by supplying a name and the read/write capacity. A default schema with a hash_key of :id => :string will be provided.
dynamo_db = AWS::DynamoDB.new
dynamo_db.tables.create('mytable', 10, 5)
You can provide your own hash key and optional range key.
dynamo_db.tables.create('comments', 10, 5,
:hash_key => { :blog_post_id => :number },
:range_key => { :comment_id => :number }
)
Provisioning Throughput
You must specify the desired read and write capacity when creating a table. After a table is created you can see what has been provisioned.
table.read_capacity_units #=> 10
table.write_capacity_units #=> 5
To change these values, call #provision_throughput:
table.provision_throughput :read_capacity_units => 100, :write_capacity_units => 100
Please note that provisioned throughput can be decreased only once within a 24 hour period.
Table Status
When you create or update a table the changes can take some time to apply. You can query the status of your table at any time:
# creating a table can be a *very* slow operation
table = dynamo_db.tables.create('mytable')
sleep 1 while table.status == :creating
table.status #=> :active
Instance Attribute Summary collapse
-
#hash_key ⇒ PrimaryKeyElement
Returns the hash key element for this table.
-
#name ⇒ String
readonly
The name of this table.
- #range_key ⇒ PrimaryKeyElement
-
#read_capacity_units ⇒ Integer
The current value of read_capacity_units.
-
#status ⇒ Symbol
readonly
The current value of status.
-
#throughput_last_decreased_at ⇒ Time
The current value of throughput_last_decreased_at.
-
#throughput_last_increased_at ⇒ Time
The current value of throughput_last_increased_at.
-
#write_capacity_units ⇒ Integer
The current value of write_capacity_units.
Instance Method Summary collapse
-
#assert_schema! ⇒ nil
Raises an exception unless the table schema is loaded.
-
#batch_delete(items) ⇒ nil
Delete up to 25 items in a single batch.
-
#batch_get(attributes, items) {|Hash| ... } ⇒ Enumerable
Requets a list of attributes for a list of items in the same table.
-
#batch_put(items) ⇒ nil
Batch puts up to 25 items to this table.
-
#batch_write(options = {}) ⇒ nil
Batch writes up to 25 items to this table.
-
#composite_key? ⇒ Boolean
(also: #has_range_key?)
Returns true if the table has both a hash key and a range key.
-
#delete ⇒ nil
Deletes a table and all of its items.
-
#exists? ⇒ Boolean
Returns true if the table exists.
-
#items ⇒ ItemCollection
Returns an object representing all the items in the table.
-
#load_schema ⇒ Object
Loads the table’s schema information into memory.
-
#provision_throughput(options = {}) ⇒ Hash
Returns a hash with the current throughput provisioning (
:read_capacity_units
and:write_capacity_units
). -
#range_key_without_schema_override ⇒ PrimaryKeyElement?
Returns the range key element for this table, or nil if the table does not have a range key.
-
#schema_loaded? ⇒ Boolean
True if the table’s schema information is loaded into memory.
-
#simple_key? ⇒ Boolean
Returns true if the table has a hash key and no range key.
Instance Attribute Details
#hash_key ⇒ PrimaryKeyElement
Returns the hash key element for this table.
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def hash_key @hash_key end |
#name ⇒ String (readonly)
Returns The name of this table.
100 101 102 |
# File 'lib/aws/dynamo_db/table.rb', line 100 def name @name end |
#range_key ⇒ PrimaryKeyElement
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def range_key @range_key end |
#read_capacity_units ⇒ Integer
Returns the current value of read_capacity_units.
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def read_capacity_units @read_capacity_units end |
#status ⇒ Symbol (readonly)
Returns the current value of status.
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def status @status end |
#throughput_last_decreased_at ⇒ Time
Returns the current value of throughput_last_decreased_at.
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def throughput_last_decreased_at @throughput_last_decreased_at end |
#throughput_last_increased_at ⇒ Time
Returns the current value of throughput_last_increased_at.
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def throughput_last_increased_at @throughput_last_increased_at end |
#write_capacity_units ⇒ Integer
Returns the current value of write_capacity_units.
91 92 93 |
# File 'lib/aws/dynamo_db/table.rb', line 91 def write_capacity_units @write_capacity_units end |
Instance Method Details
#assert_schema! ⇒ nil
Raises an exception unless the table schema is loaded.
218 219 220 |
# File 'lib/aws/dynamo_db/table.rb', line 218 def assert_schema! raise "table schema not loaded" unless schema_loaded? end |
#batch_delete(items) ⇒ nil
Delete up to 25 items in a single batch.
table.batch_delete(%w(id1 id2 id3 id4 id5))
470 471 472 473 474 |
# File 'lib/aws/dynamo_db/table.rb', line 470 def batch_delete items batch = BatchWrite.new(:config => config) batch.delete(self, items) batch.process! end |
#batch_get(attributes, items) {|Hash| ... } ⇒ Enumerable
This method does not require the table schema to be loaded.
Requets a list of attributes for a list of items in the same table.
If you want to request a list of attributes for items that span multiple tables, see AWS::DynamoDB#batch_get.
You can call this method in two forms:
# block form
table.batch_get(:all, items) do |attributes|
# yeilds one hash of attribute names/values for each item
puts attributes.to_yaml
end
# enumerable return value
attribute_hashes = table.batch_get(:all, items)
attribute_hashes.each do |attributes|
# ...
end
Attributes
You can specify the list of attributes to request in 3 ways:
-
The symbol
:all
(to recieve all attributes) -
A single attribute name (e.g. ‘size’)
-
An array of attribute names (e.g. [‘size’, ‘color’])
A few exmaples:
# get all attributes
table.batch_get(:all, items)
# only get the 'color' attribute
table.batch_get('color', items)
# get 'color' and 'size' attributes
table.batch_get(['color', size'], items)
Items
You must specify an array of items to fetch attributes for. The items
param should always be an array with:
-
String hash key values
-
Arrays of string hash key and range key values
-
Item objects
Here are a few examples:
# items as a list of hash key values
items = %w(hashkey1 hashkey2 hashkey3)
table.batch_get(:all, items)
# items as a list of hash and range key values
items = [['hashkey1', 'rangekey2'], ['hashkey1', 'rangekey2']]
table.batch_get(:all, items)
# items as a list of Item objects
items = []
items << Item.new(table, 'hashkey1')
items << Item.new(table, 'hashkey2')
table.batch_get(:all, items)
Please note that you must provide both hash and range keys for tables that include a range key in the schema.
402 403 404 405 406 407 |
# File 'lib/aws/dynamo_db/table.rb', line 402 def batch_get attributes, items, &block batch = BatchGet.new(:config => config) batch.table(name, attributes, items) enum = batch.to_enum(:each_attributes) block_given? ? enum.each(&block) : enum end |
#batch_put(items) ⇒ nil
Batch puts up to 25 items to this table.
table.batch_put([
{ :id => 'id1', :color => 'red' },
{ :id => 'id2', :color => 'blue' },
{ :id => 'id3', :color => 'green' },
])
423 424 425 426 427 |
# File 'lib/aws/dynamo_db/table.rb', line 423 def batch_put items batch = BatchWrite.new(:config => config) batch.put(self, items) batch.process! end |
#batch_write(options = {}) ⇒ nil
Batch writes up to 25 items to this table. A batch may contain a mix of items to put and items to delete.
table.batch_write(
:put => [
{ :id => 'id1', :color => 'red' },
{ :id => 'id2', :color => 'blue' },
{ :id => 'id3', :color => 'green' },
],
:delete => ['id4', 'id5']
)
447 448 449 450 451 |
# File 'lib/aws/dynamo_db/table.rb', line 447 def batch_write = {} batch = BatchWrite.new(:config => config) batch.write(self, ) batch.process! end |
#composite_key? ⇒ Boolean Also known as: has_range_key?
Returns true if the table has both a hash key and a range key.
197 198 199 |
# File 'lib/aws/dynamo_db/table.rb', line 197 def composite_key? !simple_key? end |
#delete ⇒ nil
Deletes a table and all of its items. The table must be in an :active
state (see #status).
293 294 295 296 |
# File 'lib/aws/dynamo_db/table.rb', line 293 def delete client.delete_table(:table_name => name) nil end |
#exists? ⇒ Boolean
Returns true if the table exists. Note that a table exists even when it is in a :deleting
state; this method only returns false when DynamoDB no longer returns any information about the table.
308 309 310 311 312 313 |
# File 'lib/aws/dynamo_db/table.rb', line 308 def exists? get_resource true rescue Errors::ResourceNotFoundException false end |
#items ⇒ ItemCollection
Returns an object representing all the items in the table.
300 301 302 |
# File 'lib/aws/dynamo_db/table.rb', line 300 def items ItemCollection.new(self) end |
#load_schema ⇒ Object
You must load the the table schema using #load_schema, #hash_key or #range_key or configure it using #hash_key= and optionally #range_key= in order to work with DynamoDB items.
Loads the table’s schema information into memory. This method should not be used in a high-volume code path, and is intended only as a convenience for exploring the API. In general you should configure a schema with #hash_key= and #range_key= before using the table.
234 235 236 237 |
# File 'lib/aws/dynamo_db/table.rb', line 234 def load_schema hash_key self end |
#provision_throughput(options = {}) ⇒ Hash
Returns a hash with the current throughput provisioning (:read_capacity_units
and :write_capacity_units
).
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/aws/dynamo_db/table.rb', line 165 def provision_throughput = {} [:read_capacity_units] ||= read_capacity_units [:write_capacity_units] ||= write_capacity_units client_opts = {} client_opts[:table_name] = name client_opts[:provisioned_throughput] = client.update_table(client_opts) end |
#range_key_without_schema_override ⇒ PrimaryKeyElement?
Returns the range key element for this table, or nil if the table does not have a range key.
127 128 129 |
# File 'lib/aws/dynamo_db/table.rb', line 127 def range_key @range_key end |
#schema_loaded? ⇒ Boolean
You must load the the table schema using #load_schema, #hash_key or #range_key or configure it using #hash_key= and optionally #range_key= in order to work with DynamoDB items.
Returns True if the table’s schema information is loaded into memory.
210 211 212 |
# File 'lib/aws/dynamo_db/table.rb', line 210 def schema_loaded? static_attributes.include?(:hash_key) end |
#simple_key? ⇒ Boolean
Returns true if the table has a hash key and no range key.
191 192 193 |
# File 'lib/aws/dynamo_db/table.rb', line 191 def simple_key? range_key.nil? end |