-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathpartiql_batch.rb
More file actions
54 lines (49 loc) · 1.96 KB
/
partiql_batch.rb
File metadata and controls
54 lines (49 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
require 'aws-sdk-dynamodb'
require 'json'
require 'open-uri'
require 'zip'
require_relative '../scaffold'
# snippet-start:[ruby.example_code.ruby.DynamoDBPartiQLBatch.full]
# snippet-start:[ruby.example_code.ruby.DynamoDBPartiQLBatch.decl]
class DynamoDBPartiQLBatch
attr_reader :dynamo_resource, :table
def initialize(table_name)
client = Aws::DynamoDB::Client.new(region: 'us-east-1')
@dynamodb = Aws::DynamoDB::Resource.new(client: client)
@table = @dynamodb.table(table_name)
end
# snippet-end:[ruby.example_code.ruby.DynamoDBPartiQLBatch.decl]
# snippet-start:[ruby.example_code.dynamodb.partiql.batch_read]
# Selects a batch of items from a table using PartiQL
#
# @param batch_titles [Array] Collection of movie titles
# @return [Aws::DynamoDB::Types::BatchExecuteStatementOutput]
def batch_execute_select(batch_titles)
request_items = batch_titles.map do |title, year|
{
statement: "SELECT * FROM \"#{@table.name}\" WHERE title=? and year=?",
parameters: [title, year]
}
end
@dynamodb.client.batch_execute_statement({ statements: request_items })
end
# snippet-end:[ruby.example_code.dynamodb.partiql.batch_read]
# snippet-start:[ruby.example_code.dynamodb.partiql.batch_write]
# Deletes a batch of items from a table using PartiQL
#
# @param batch_titles [Array] Collection of movie titles
# @return [Aws::DynamoDB::Types::BatchExecuteStatementOutput]
def batch_execute_write(batch_titles)
request_items = batch_titles.map do |title, year|
{
statement: "DELETE FROM \"#{@table.name}\" WHERE title=? and year=?",
parameters: [title, year]
}
end
@dynamodb.client.batch_execute_statement({ statements: request_items })
end
# snippet-end:[ruby.example_code.dynamodb.partiql.batch_write]
end
# snippet-end:[ruby.example_code.ruby.DynamoDBPartiQLBatch.full]