Last active
May 22, 2018 18:20
-
-
Save danhealy/92c8b236c1c53a18c535 to your computer and use it in GitHub Desktop.
Parallel DynamoDB Scan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
batch_size = 100 # Items per request | |
parallel_requests = 4 | |
total_items_to_request = 4000 | |
iterations = (total_items_to_request / (parallel_requests * batch_size)) # => 10 | |
parallel_requests.times do |seg| | |
Process.fork do | |
c = AWS::DynamoDB::Client.new( | |
:access_key_id => "...", | |
:secret_access_key => "...", | |
:api_version => '2012-08-10' | |
) | |
result = nil | |
iterations.times do |i| | |
req = { | |
table_name: "my_table", | |
limit: batch_size, | |
total_segments: parallel_requests, | |
segment: seg | |
} | |
if result && result[:last_evaluated_key] | |
req[:exclusive_start_key] = result[:last_evaluated_key] | |
end | |
result = c.scan(req) | |
# result[:member] is an array of batch_size items to process | |
end | |
end | |
end | |
Process.waitall |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😄
A thread safe implementation: