使用 Ruby 删除 DynamoDB table 中的所有项目
Delete all items in DynamoDB table using Ruby
我正在尝试编写一个简单的 ruby 脚本来删除 DynamoDB table 中的所有项目,但我无法理解将哪个参数传递给 "delete_items",这是我目前所拥有的:
dynamoDB = Aws::DynamoDB::Resource.new(region: 'us-west-2')
dynamoDB.tables.each do |table|
puts "Table #{table.name}"
scan_output = table.scan({
select: "ALL_ATTRIBUTES"
})
scan_output.items.each do |item|
keys = item.keys
table.delete_item({
key: ???
})
end
end
我试过传递项目,或者 item.keys - 都没有用。
谢谢!
这是扫描和删除 DynamoDB table 中所有项目的代码,但我不确定为什么你不能删除 table 并重新创建,如果你想删除所有来自 table.
的项目
请注意,这不是推荐的方法,除非您有一些非常具体的用例。这会让您付出代价,因为代码会从 table 读取项目,然后删除该项目。
代码:-
您可能需要更改以下代码中的 table 名称和键值。在下面的代码中,使用的 table 名称是 files
,它的键值是 fileName
.
如果您同时拥有分区键和排序键,则需要同时设置这两个值。 files
table 只有分区键。
#! /usr/bin/ruby
require "aws-sdk-core"
# Configure SDK
# use credentials file at .aws/credentials
Aws.config[:credentials] = Aws::SharedCredentials.new
Aws.config[:region] = "us-west-2"
# point to DynamoDB Local, comment out this line to use real DynamoDB
Aws.config[:dynamodb] = { endpoint: "http://localhost:8000" }
dynamodb = Aws::DynamoDB::Client.new
tableName = "files"
scanParams = {
table_name: tableName
}
puts "Scanning files table."
begin
loop do
result = dynamodb.scan(scanParams)
result.items.each{|files|
puts "Item :" + "#{files}"
puts "Going to delete item :" + "#{files["fileName"]}"
deleteParams = {
table_name: tableName,
key: {
fileName: files["fileName"]
}
}
begin
deleteResult = dynamodb.delete_item(deleteParams)
puts "Deleted item." + files["fileName"]
rescue Aws::DynamoDB::Errors::ServiceError => error
puts "Unable to delete item:"
puts "#{error.message}"
end
}
break if result.last_evaluated_key.nil?
puts "Scanning for more..."
scanParams[:exclusive_start_key] = result.last_evaluated_key
end
rescue Aws::DynamoDB::Errors::ServiceError => error
puts "Unable to scan:"
puts "#{error.message}"
end
我最终写了这个脚本,它删除了所有表中的所有记录(在大多数情况下不是很有用,但对我来说这正是我所需要的,因为我在专用测试帐户中使用它):
#!/usr/bin/env ruby
require 'aws-sdk'
dynamoDB = Aws::DynamoDB::Resource.new(region: 'us-west-2')
dynamoDB.tables.each do |table|
puts "Table #{table.name}"
scan_output = table.scan({
select: "ALL_ATTRIBUTES"
})
scan_output.items.each do |item|
item_key = Hash.new
table.key_schema.each do |k|
item_key[k.attribute_name] = item[k.attribute_name]
end
table.delete_item({
key: item_key
})
end
end
我正在尝试编写一个简单的 ruby 脚本来删除 DynamoDB table 中的所有项目,但我无法理解将哪个参数传递给 "delete_items",这是我目前所拥有的:
dynamoDB = Aws::DynamoDB::Resource.new(region: 'us-west-2')
dynamoDB.tables.each do |table|
puts "Table #{table.name}"
scan_output = table.scan({
select: "ALL_ATTRIBUTES"
})
scan_output.items.each do |item|
keys = item.keys
table.delete_item({
key: ???
})
end
end
我试过传递项目,或者 item.keys - 都没有用。
谢谢!
这是扫描和删除 DynamoDB table 中所有项目的代码,但我不确定为什么你不能删除 table 并重新创建,如果你想删除所有来自 table.
的项目请注意,这不是推荐的方法,除非您有一些非常具体的用例。这会让您付出代价,因为代码会从 table 读取项目,然后删除该项目。
代码:-
您可能需要更改以下代码中的 table 名称和键值。在下面的代码中,使用的 table 名称是 files
,它的键值是 fileName
.
如果您同时拥有分区键和排序键,则需要同时设置这两个值。 files
table 只有分区键。
#! /usr/bin/ruby
require "aws-sdk-core"
# Configure SDK
# use credentials file at .aws/credentials
Aws.config[:credentials] = Aws::SharedCredentials.new
Aws.config[:region] = "us-west-2"
# point to DynamoDB Local, comment out this line to use real DynamoDB
Aws.config[:dynamodb] = { endpoint: "http://localhost:8000" }
dynamodb = Aws::DynamoDB::Client.new
tableName = "files"
scanParams = {
table_name: tableName
}
puts "Scanning files table."
begin
loop do
result = dynamodb.scan(scanParams)
result.items.each{|files|
puts "Item :" + "#{files}"
puts "Going to delete item :" + "#{files["fileName"]}"
deleteParams = {
table_name: tableName,
key: {
fileName: files["fileName"]
}
}
begin
deleteResult = dynamodb.delete_item(deleteParams)
puts "Deleted item." + files["fileName"]
rescue Aws::DynamoDB::Errors::ServiceError => error
puts "Unable to delete item:"
puts "#{error.message}"
end
}
break if result.last_evaluated_key.nil?
puts "Scanning for more..."
scanParams[:exclusive_start_key] = result.last_evaluated_key
end
rescue Aws::DynamoDB::Errors::ServiceError => error
puts "Unable to scan:"
puts "#{error.message}"
end
我最终写了这个脚本,它删除了所有表中的所有记录(在大多数情况下不是很有用,但对我来说这正是我所需要的,因为我在专用测试帐户中使用它):
#!/usr/bin/env ruby
require 'aws-sdk'
dynamoDB = Aws::DynamoDB::Resource.new(region: 'us-west-2')
dynamoDB.tables.each do |table|
puts "Table #{table.name}"
scan_output = table.scan({
select: "ALL_ATTRIBUTES"
})
scan_output.items.each do |item|
item_key = Hash.new
table.key_schema.each do |k|
item_key[k.attribute_name] = item[k.attribute_name]
end
table.delete_item({
key: item_key
})
end
end