Class: Awful::DynamoDB
- Inherits:
-
Cli
show all
- Defined in:
- lib/awful/dynamodb.rb
Constant Summary
collapse
- COLORS =
{
CREATING: :yellow,
UPDATING: :yellow,
DELETING: :red,
ACTIVE: :green,
}
Instance Method Summary
collapse
Methods inherited from Cli
#initialize, #ll, #version
Constructor Details
This class inherits a constructor from Awful::Cli
Instance Method Details
#batch_write(name) ⇒ Object
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
# File 'lib/awful/dynamodb.rb', line 369
def batch_write(name)
items = (1..25).map do |n|
{
put_request: {
item: {
"store_id" => "store#{n}",
"object_id" => "object#{n}",
"object_value" => "value#{n}"
}
}
}
end
p items
r = dynamodb.batch_write_item(request_items: {name => items})
p r
end
|
#copy(src, dst) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/awful/dynamodb.rb', line 229
def copy(src, dst)
src_table, src_region = src.split('/').reverse dst_table, dst_region = dst.split('/').reverse
src_client = Aws::DynamoDB::Client.new({region: src_region}.reject{|_,v| v.nil?})
dst_client = Aws::DynamoDB::Client.new({region: dst_region}.reject{|_,v| v.nil?})
params = {table_name: dst_table}
if options[:no_clobber]
keys = dst_client.describe_table(table_name: dst_table).table.key_schema.map(&:attribute_name)
params.merge!(condition_expression: keys.map{|key| "attribute_not_exists(#{key})"}.join(' AND '))
end
dots = options[:dots] ? ->(x){print x ? '.' : 'x'} : ->(_){}
exclusive_start_key = nil
loop do
r = src_client.scan(table_name: src_table, exclusive_start_key: exclusive_start_key, return_consumed_capacity: 'INDEXES')
puts "[#{Time.now}] [#{src_table}] scanned:#{r.count} key:#{r.last_evaluated_key || 'nil'}"
put = skipped = 0
r.items.each do |item|
begin
dst_client.put_item(params.merge(item: item))
put += 1
dots.call(true)
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException skipped += 1
dots.call(false)
end
end
print "\n" if options[:dots]
puts "[#{Time.now}] [#{dst_table}] put:#{put} skipped:#{skipped}"
exclusive_start_key = r.last_evaluated_key
break unless exclusive_start_key
end
end
|
#create_table(name, file = nil) ⇒ Object
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
|
# File 'lib/awful/dynamodb.rb', line 84
def create_table(name, file = nil)
opt = load_cfg(options, file)
params = only_keys_matching(opt, %i[attribute_definitions key_schema])
params[:table_name] = name
params[:provisioned_throughput] = only_keys_matching(opt[:provisioned_throughput], %i[read_capacity_units write_capacity_units])
if opt.has_key?(:local_secondary_indexes)
params[:local_secondary_indexes] = opt[:local_secondary_indexes].map do |lsi|
only_keys_matching(lsi, %i[index_name key_schema projection])
end
end
if opt.has_key?(:global_secondary_indexes)
params[:global_secondary_indexes] = opt[:global_secondary_indexes].map do |gsi|
only_keys_matching(gsi, %i[index_name key_schema projection]).output do |g|
if gsi[:provisioned_throughput]
g[:provisioned_throughput] = only_keys_matching(gsi[:provisioned_throughput], %i[read_capacity_units write_capacity_units])
end
end
end
end
dynamodb.create_table(params)
end
|
#delete_table(name) ⇒ Object
216
217
218
219
220
221
222
223
224
|
# File 'lib/awful/dynamodb.rb', line 216
def delete_table(name)
confirmation = ask("to delete #{name} and all its data, type the name of table to delete:", :yellow)
if confirmation == name
say("deleting table #{name}")
dynamodb.delete_table(table_name: name)
else
say("confirmation failed for #{name}", :red)
end
end
|
#dump(name) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/awful/dynamodb.rb', line 63
def dump(name)
all_matching_tables(name).map do |table_name|
dynamodb.describe_table(table_name: table_name).table.to_hash.output do |table|
puts YAML.dump(stringify_keys(table))
end
end
end
|
#enable_streams(name) ⇒ Object
209
210
211
212
213
|
# File 'lib/awful/dynamodb.rb', line 209
def enable_streams(name)
stream_specification = {stream_enabled: !options[:disable]}
stream_specification.merge!(stream_view_type: options[:stream_view_type].upcase) unless options[:disable]
dynamodb.update_table(table_name: name, stream_specification: stream_specification)
end
|
#gsi(name) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/awful/dynamodb.rb', line 114
def gsi(name)
(dynamodb.describe_table(table_name: name).table.global_secondary_indexes || []).output do |list|
if options[:long]
print_table list.map { |i|
hash = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
[i.index_name, color(i.index_status), hash, range, i.projection.projection_type,
i.provisioned_throughput.read_capacity_units, i.provisioned_throughput.write_capacity_units,
i.index_size_bytes, i.item_count]
}.sort
elsif options[:arn]
puts list.map(&:index_arn).sort
else
puts list.map(&:index_name).sort
end
end
end
|
#keys(name) ⇒ Object
77
78
79
80
81
|
# File 'lib/awful/dynamodb.rb', line 77
def keys(name)
dynamodb.describe_table(table_name: name).table.key_schema.each_with_object({}) do |schema, h|
h[schema.key_type.downcase.to_sym] = schema.attribute_name
end.output(&method(:print_table))
end
|
#ls(name = /./) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/awful/dynamodb.rb', line 48
def ls(name = /./)
tables = all_matching_tables(name)
if options[:long]
tables.map do |table|
dynamodb.describe_table(table_name: table).table
end.output do |list|
print_table list.map { |t| [ t.table_name, color(t.table_status), t.item_count, t.table_size_bytes, t.creation_date_time ] }
end
else
tables.output(&method(:puts))
end
end
|
#lsi(name) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/awful/dynamodb.rb', line 135
def lsi(name)
(dynamodb.describe_table(table_name: name).table.local_secondary_indexes || []).output do |list|
if options[:long]
print_table list.map { |i|
hash = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
[i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
}.sort
elsif options[:arn]
puts list.map(&:index_arn).sort
else
puts list.map(&:index_name).sort
end
end
end
|
#put_items(name, file = nil) ⇒ Object
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
# File 'lib/awful/dynamodb.rb', line 339
def put_items(name, file = nil)
params = {'TableName' => name}
if options[:no_clobber]
keys = dynamodb.describe_table(table_name: name).table.key_schema.map(&:attribute_name)
params.merge!('ConditionExpression' => keys.map{|key| "attribute_not_exists(#{key})"}.join(' AND '))
end
io = (file and File.open(file)) || ((not $stdin.tty?) and $stdin)
put_count = 0
skip_count = 0
io.each_line do |line|
begin
dynamodb_simple.put_item(params.merge('Item' => JSON.parse(line)))
put_count += 1
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException skip_count += 1
end
end
[put_count, skip_count].output do |put, skip|
puts "put #{put} items, skipped #{skip} items"
end
end
|
#query(name, exclusive_start_key = nil) ⇒ Object
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
# File 'lib/awful/dynamodb.rb', line 308
def query(name, exclusive_start_key = nil)
fd = options[:output] ? File.open(options[:output], 'w') : $stdout.dup exclusive_start_key = nil
count = 0
condition = "#{options[:hash_key]} = :hash_key_value"
condition += " and #{options[:range_key]} = :range_key_value" if options[:range_key]
attributes = {
':hash_key_value' => { S: options[:hash_key_value] },
':range_key_value' => { S: options[:range_key_value] },
}.reject { |_,v| v[:S].nil? }
loop do
r = dynamodb_simple.query(
'TableName' => name,
'ExclusiveStartKey' => exclusive_start_key,
'Select' => options[:count] ? 'COUNT' : 'ALL_ATTRIBUTES',
'KeyConditionExpression' => condition,
'ExpressionAttributeValues' => attributes,
)
count += r.fetch('Count', 0)
r.fetch('Items', []).each do |item|
fd.puts JSON.generate(item)
end
exclusive_start_key = r['LastEvaluatedKey']
break unless exclusive_start_key
end
fd.close
puts count if options[:count]
end
|
#scan(name, exclusive_start_key = nil) ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
# File 'lib/awful/dynamodb.rb', line 280
def scan(name, exclusive_start_key = nil)
fd = options[:output] ? File.open(options[:output], 'w') : $stdout.dup exclusive_start_key = nil
count = 0
loop do
r = dynamodb_simple.scan(
'TableName' => name,
'Select' => options[:count] ? 'COUNT' : 'ALL_ATTRIBUTES',
'ExclusiveStartKey' => exclusive_start_key
)
count += r.fetch('Count', 0)
r.fetch('Items', []).each do |item|
fd.puts JSON.generate(item)
end
exclusive_start_key = r['LastEvaluatedKey']
break unless exclusive_start_key
end
fd.close
puts count if options[:count]
end
|
#status(name) ⇒ Object
72
73
74
|
# File 'lib/awful/dynamodb.rb', line 72
def status(name)
dynamodb.describe_table(table_name: name).table.table_status.output(&method(:puts))
end
|
#tag(name, *tags) ⇒ Object
396
397
398
399
400
401
402
403
404
|
# File 'lib/awful/dynamodb.rb', line 396
def tag(name, *tags)
dynamodb.tag_resource(
resource_arn: get_arn(name),
tags: tags.map { |t|
k, v = t.split(/[=:]/)
{key: k, value: v}
}
)
end
|
387
388
389
390
391
392
393
|
# File 'lib/awful/dynamodb.rb', line 387
def tags(name)
paginate(:tags) do |token|
dynamodb.list_tags_of_resource(resource_arn: get_arn(name), next_token: token)
end.output do |tags|
print_table tags.map { |t| [t.key, t.value] }
end
end
|
#throughput(name) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/awful/dynamodb.rb', line 157
def throughput(name)
table = dynamodb.describe_table(table_name: name).table
current = table.provisioned_throughput.to_h
global_secondary_indexes = table.global_secondary_indexes || []
global_secondary_indexes.each do |gsi|
current[gsi.index_name] = gsi.provisioned_throughput.to_h
end
unless options[:read_capacity_units] or options[:write_capacity_units]
puts YAML.dump(stringify_keys(current))
return table
end
params = { table_name: name }
params[:provisioned_throughput] = {
read_capacity_units: options[:read_capacity_units] || current[:read_capacity_units],
write_capacity_units: options[:write_capacity_units] || current[:write_capacity_units]
} if options[:table]
gsis = options[:gsi]
gsis = global_secondary_indexes.map(&:index_name) if options[:all]
params[:global_secondary_index_updates] = gsis.map do |gsi|
{
update: {
index_name: gsi,
provisioned_throughput: {
read_capacity_units: options[:read_capacity_units] || current[gsi][:read_capacity_units],
write_capacity_units: options[:write_capacity_units] || current[gsi][:write_capacity_units]
}
}
}
end
params.reject! { |_,v| v.empty? } dynamodb.update_table(params)
end
|
#ttl(name, attribute = nil) ⇒ Object
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
# File 'lib/awful/dynamodb.rb', line 408
def ttl(name, attribute = nil)
if attribute
dynamodb.update_time_to_live(
table_name: name,
time_to_live_specification: {
enabled: !options[:disable],
attribute_name: attribute,
}
)
else
dynamodb.describe_time_to_live(table_name: name).time_to_live_description.output do |t|
puts YAML.dump(stringify_keys(t.to_hash))
end
end
end
|