Class: AWS::DynamoDB::BatchWrite

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/dynamo_db/batch_write.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BatchWrite

Returns a new instance of BatchWrite.



21
22
23
24
# File 'lib/aws/dynamo_db/batch_write.rb', line 21

def initialize options = {}
  super(options)
  @request_items = {}
end

Instance Method Details

#delete(table, items) ⇒ nil

Adds one or more items to the batch to delete.

# for a table w/out a range key
batch = AWS::DynamoDB::BatchWrite.new
batch.delete('table-name', %w(hk1 hk2))
batch.process!

# for a table with a range key
batch = AWS::DynamoDB::BatchWrite.new
batch.delete('table-name', [['hk1', 'rk2'], ['hk1', 'rk2']]])
batch.process!

Parameters:

  • table (Table, String)

    A Table object or table name string.

  • items (Array<String>, Array<Array>)

    A list of item keys to delete. For tables without a range key, items should be an array of hash key strings.

    batch.delete('table-name', ['hk1', 'hk2', 'hk3'])
    

    For tables with a range key, items should be an array of hash key and range key pairs.

    batch.delete('table-name', [['hk1', 'rk1'], ['hk1', 'rk2']])
    

Returns:

  • (nil)


83
84
85
86
# File 'lib/aws/dynamo_db/batch_write.rb', line 83

def delete table, items
  write(table, :delete => items)
  nil
end

#process!nil

Proccesses pending request items.

Returns:

  • (nil)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/aws/dynamo_db/batch_write.rb', line 136

def process!

  return if @request_items.empty?

  opts = { :request_items => @request_items }

  begin

    response = client.batch_write_item(opts)

    unprocessed = response.data['UnprocessedItems']

    opts[:request_items] = convert_unprocessed_items(unprocessed)

  end while opts[:request_items]

  @request_items = {}
  nil

end

#put(table, items) ⇒ nil

Adds one or more items to the batch write operation.

# adding one item at a time to the batch
batch = AWS::DynamoDB::BatchWrite.new
batch.put('table-name', :id => 'id1', :color => 'red')
batch.put('table-name', :id => 'id2', :color => 'blue')
batch.process!

# adding multiple items to a batch
batch = AWS::DynamoDB::BatchWrite.new
batch.put('table-name', [
  { :id => 'id1', :color => 'red' },
  { :id => 'id2', :color => 'blue' },
  { :id => 'id3', :color => 'green' },
])
batch.process!

Parameters:

  • table (Table, String)

    A Table object or table name string.

  • items (Array<Hash>)

    A list of item attributes to put. The hash must contain the table hash key element and range key element (if one is defined).

Returns:

  • (nil)


51
52
53
54
# File 'lib/aws/dynamo_db/batch_write.rb', line 51

def put table, items
  write(table, :put => items.flatten)
  nil
end

#write(table, options = {}) ⇒ Object

Add items to the batch. Accepts both item to put and and items to delete.

Parameters:

  • table (Table, String)

    A Table object or table name string.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :put (Array<Hash>)

    An array of items to put. Each item should be an array of attribute hashes.

    # add 3 items to the batch
    batch.write(table, :put => [
      { :id => 'abc', :color => 'red', :count => 2 },
      { :id => 'mno', :color => 'blue', :count => 3 },
      { :id => 'xyz', :color => 'green', :count => 5 },
    ])
    
  • :delete (Array<String>, Array<Array>)

    A list of item keys to delete. For tables without a range key, items should be an array of hash key strings.

    batch.write('table-name', :delete => ['hk1', 'hk2', 'hk3'])
    

    For tables with a range key, items should be an array of hash key and range key pairs.

    batch.write('table-name', :delete => [['hk1', 'rk1'], ['hk1', 'rk2']])
    


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aws/dynamo_db/batch_write.rb', line 116

def write table, options = {}

  items = table_items(table) 

  if put = options[:put]
    put.each do |attributes|
      items << { :put_request => { :item => format_put(attributes) }}
    end
  end

  if del = options[:delete]
    del.each do |keys|
      items << { :delete_request => { :key => format_delete(keys) }}
    end
  end

end