Class: Awful::DynamoDB
Constant Summary collapse
- COLORS =
{ CREATING: :yellow, UPDATING: :yellow, DELETING: :red, ACTIVE: :green, }
Instance Method Summary collapse
- #batch_write(name) ⇒ Object
- #copy(src, dst) ⇒ Object
- #create_table(name, file = nil) ⇒ Object
- #delete_table(name) ⇒ Object
- #dump(name) ⇒ Object
- #enable_streams(name) ⇒ Object
- #keys(name) ⇒ Object
- #ls(name = /./) ⇒ Object
- #put_items(name, file = nil) ⇒ Object
- #query(name, exclusive_start_key = nil) ⇒ Object
- #scan(name, exclusive_start_key = nil) ⇒ Object
- #status(name) ⇒ Object
- #throughput(name) ⇒ Object
Methods inherited from Cli
Constructor Details
This class inherits a constructor from Awful::Cli
Instance Method Details
#batch_write(name) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/awful/dynamodb.rb', line 324 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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/awful/dynamodb.rb', line 184 def copy(src, dst) src_table, src_region = src.split('/').reverse # parse region/table into [table, region] dst_table, dst_region = dst.split('/').reverse ## clients are potentially for different regions 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 for put_item call params = {table_name: dst_table} ## add condition not to overwrite existing primary keys (hash or composite hash AND range) if [: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 ## lame progress indicator, pass true for put, false for skip dots = [:dots] ? ->(x){print x ? '.' : 'x'} : ->(_){} ## loop on each batch of scanned items 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'}" ## loop items and put to destination 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 #item key exists skipped += 1 dots.call(false) end end print "\n" if [:dots] puts "[#{Time.now}] [#{dst_table}] put:#{put} skipped:#{skipped}" ## loop if there are more keys to scan exclusive_start_key = r.last_evaluated_key break unless exclusive_start_key end end |
#create_table(name, file = nil) ⇒ Object
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 |
# File 'lib/awful/dynamodb.rb', line 79 def create_table(name, file = nil) opt = load_cfg(, 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]) ## scrub unwanted keys from LSIs 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 ## scrub unwanted keys from GSIs 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
171 172 173 174 175 176 177 178 179 |
# File 'lib/awful/dynamodb.rb', line 171 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
58 59 60 61 62 63 64 |
# File 'lib/awful/dynamodb.rb', line 58 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
164 165 166 167 168 |
# File 'lib/awful/dynamodb.rb', line 164 def enable_streams(name) stream_specification = {stream_enabled: ![:disable]} stream_specification.merge!(stream_view_type: [:stream_view_type].upcase) unless [:disable] dynamodb.update_table(table_name: name, stream_specification: stream_specification) end |
#keys(name) ⇒ Object
72 73 74 75 76 |
# File 'lib/awful/dynamodb.rb', line 72 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
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/awful/dynamodb.rb', line 43 def ls(name = /./) tables = all_matching_tables(name) if [: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 |
#put_items(name, file = nil) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/awful/dynamodb.rb', line 294 def put_items(name, file = nil) params = {'TableName' => name} ## set a condition not to overwrite items with existing primary key(s) if [: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 ## input data 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 #item key exists skip_count += 1 end end ## return counts [put_count, skip_count].output do |put, skip| puts "put #{put} items, skipped #{skip} items" end end |
#query(name, exclusive_start_key = nil) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/awful/dynamodb.rb', line 263 def query(name, exclusive_start_key = nil) fd = [:output] ? File.open([:output], 'w') : $stdout.dup # open output file or stdout exclusive_start_key = nil count = 0 condition = "#{[:hash_key]} = :hash_key_value" condition += " and #{[:range_key]} = :range_key_value" if [:range_key] attributes = { ':hash_key_value' => { S: [:hash_key_value] }, ':range_key_value' => { S: [:range_key_value] }, }.reject { |_,v| v[:S].nil? } loop do r = dynamodb_simple.query( 'TableName' => name, 'ExclusiveStartKey' => exclusive_start_key, 'Select' => [: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 [:count] end |
#scan(name, exclusive_start_key = nil) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/awful/dynamodb.rb', line 235 def scan(name, exclusive_start_key = nil) fd = [:output] ? File.open([:output], 'w') : $stdout.dup # open output file or stdout exclusive_start_key = nil count = 0 loop do r = dynamodb_simple.scan( 'TableName' => name, 'Select' => [: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 [:count] end |
#status(name) ⇒ Object
67 68 69 |
# File 'lib/awful/dynamodb.rb', line 67 def status(name) dynamodb.describe_table(table_name: name).table.table_status.output(&method(:puts)) end |
#throughput(name) ⇒ Object
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/awful/dynamodb.rb', line 112 def throughput(name) table = dynamodb.describe_table(table_name: name).table ## current is hash of current provisioned throughput current = table.provisioned_throughput.to_h ## loop-safe version of GSIs (in case nil) global_secondary_indexes = table.global_secondary_indexes || [] ## get throughput for each GSI global_secondary_indexes.each do |gsi| current[gsi.index_name] = gsi.provisioned_throughput.to_h end ## if no updates requested, just print throughput and return table details unless [:read_capacity_units] or [:write_capacity_units] puts YAML.dump(stringify_keys(current)) return table end ## parameters for update request params = { table_name: name } ## add table throughput unless told not to params[:provisioned_throughput] = { read_capacity_units: [:read_capacity_units] || current[:read_capacity_units], write_capacity_units: [:write_capacity_units] || current[:write_capacity_units] } if [:table] ## list of requested GSIs, or all for this table gsis = [:gsi] gsis = global_secondary_indexes.map(&:index_name) if [:all] params[:global_secondary_index_updates] = gsis.map do |gsi| { update: { index_name: gsi, provisioned_throughput: { read_capacity_units: [:read_capacity_units] || current[gsi][:read_capacity_units], write_capacity_units: [:write_capacity_units] || current[gsi][:write_capacity_units] } } } end ## make the update request params.reject! { |_,v| v.empty? } # sdk hates empty global_secondary_index_updates dynamodb.update_table(params) end |