-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathdynamodb_basics.rb
More file actions
143 lines (134 loc) · 5.05 KB
/
dynamodb_basics.rb
File metadata and controls
143 lines (134 loc) · 5.05 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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'
# snippet-start:[ruby.example_code.ruby.DynamoDBBasics.full]
# snippet-start:[ruby.example_code.ruby.DynamoDBBasics.decl]
class DynamoDBBasics
attr_reader :dynamo_resource, :table
def initialize(table_name)
client = Aws::DynamoDB::Client.new(region: 'us-east-1')
@dynamo_resource = Aws::DynamoDB::Resource.new(client: client)
@table = @dynamo_resource.table(table_name)
end
# snippet-end:[ruby.example_code.ruby.DynamoDBBasics.decl]
# snippet-start:[ruby.example_code.dynamodb.PutItem]
# Adds a movie to the table.
#
# @param movie [Hash] The title, year, plot, and rating of the movie.
def add_item(movie)
@table.put_item(
item: {
'year' => movie[:year],
'title' => movie[:title],
'info' => { 'plot' => movie[:plot], 'rating' => movie[:rating] }
}
)
rescue Aws::DynamoDB::Errors::ServiceError => e
puts("Couldn't add movie #{title} to table #{@table.name}. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
end
# snippet-end:[ruby.example_code.dynamodb.PutItem]
# snippet-start:[ruby.example_code.dynamodb.GetItem]
# Gets movie data from the table for a specific movie.
#
# @param title [String] The title of the movie.
# @param year [Integer] The release year of the movie.
# @return [Hash] The data about the requested movie.
def get_item(title, year)
@table.get_item(key: { 'year' => year, 'title' => title })
rescue Aws::DynamoDB::Errors::ServiceError => e
puts("Couldn't get movie #{title} (#{year}) from table #{@table.name}:\n")
puts("\t#{e.code}: #{e.message}")
raise
end
# snippet-end:[ruby.example_code.dynamodb.GetItem]
# snippet-start:[ruby.example_code.dynamodb.UpdateItem.UpdateExpression]
# Updates rating and plot data for a movie in the table.
#
# @param movie [Hash] The title, year, plot, rating of the movie.
def update_item(movie)
response = @table.update_item(
key: { 'year' => movie[:year], 'title' => movie[:title] },
update_expression: 'set info.rating=:r',
expression_attribute_values: { ':r' => movie[:rating] },
return_values: 'UPDATED_NEW'
)
rescue Aws::DynamoDB::Errors::ServiceError => e
puts("Couldn't update movie #{movie[:title]} (#{movie[:year]}) in table #{@table.name}\n")
puts("\t#{e.code}: #{e.message}")
raise
else
response.attributes
end
# snippet-end:[ruby.example_code.dynamodb.UpdateItem.UpdateExpression]
# snippet-start:[ruby.example_code.dynamodb.Query]
# Queries for movies that were released in the specified year.
#
# @param year [Integer] The year to query.
# @return [Array] The list of movies that were released in the specified year.
def query_items(year)
response = @table.query(
key_condition_expression: '#yr = :year',
expression_attribute_names: { '#yr' => 'year' },
expression_attribute_values: { ':year' => year }
)
rescue Aws::DynamoDB::Errors::ServiceError => e
puts("Couldn't query for movies released in #{year}. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
else
response.items
end
# snippet-end:[ruby.example_code.dynamodb.Query]
# snippet-start:[ruby.example_code.dynamodb.Scan]
# Scans for movies that were released in a range of years.
# Uses a projection expression to return a subset of data for each movie.
#
# @param year_range [Hash] The range of years to retrieve.
# @return [Array] The list of movies released in the specified years.
def scan_items(year_range)
movies = []
scan_hash = {
filter_expression: '#yr between :start_yr and :end_yr',
projection_expression: '#yr, title, info.rating',
expression_attribute_names: { '#yr' => 'year' },
expression_attribute_values: {
':start_yr' => year_range[:start], ':end_yr' => year_range[:end]
}
}
done = false
start_key = nil
until done
scan_hash[:exclusive_start_key] = start_key unless start_key.nil?
response = @table.scan(scan_hash)
movies.concat(response.items) unless response.items.empty?
start_key = response.last_evaluated_key
done = start_key.nil?
end
rescue Aws::DynamoDB::Errors::ServiceError => e
puts("Couldn't scan for movies. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
else
movies
end
# snippet-end:[ruby.example_code.dynamodb.Scan]
# snippet-start:[ruby.example_code.dynamodb.DeleteItem]
# Deletes a movie from the table.
#
# @param title [String] The title of the movie to delete.
# @param year [Integer] The release year of the movie to delete.
def delete_item(title, year)
@table.delete_item(key: { 'year' => year, 'title' => title })
rescue Aws::DynamoDB::Errors::ServiceError => e
puts("Couldn't delete movie #{title}. Here's why:")
puts("\t#{e.code}: #{e.message}")
raise
end
# snippet-end:[ruby.example_code.dynamodb.DeleteItem]
end
# snippet-end:[ruby.example_code.ruby.DynamoDBBasics.full]