Skip to content

Instantly share code, notes, and snippets.

@blmarket
Created December 10, 2013 08:10
Show Gist options
  • Select an option

  • Save blmarket/7887188 to your computer and use it in GitHub Desktop.

Select an option

Save blmarket/7887188 to your computer and use it in GitHub Desktop.
migration of Key-Value data from redis into dynamoDB
{AWS} = require './aws' # this is configured aws-sdk
async = require 'async'
redis = require 'redis'
client = redis.createClient(6379, '--our-redis-source--')
data = {}
dynamo = new AWS.DynamoDB()
client.keys '*', (err, res) -> # you can set your pattern
toTask = (row) ->
return (cb) ->
client.get row, (err, value) ->
return cb(err) if err?
match = row.match /^([^:]*):(.*)/ # we use colon as column seperator
(data[match[1]] ?= {})[match[2]] ?= 0
(data[match[1]] ?= {})[match[2]] += Number(value)
cb null
tasks = (toTask(row) for row in res)
async.parallel tasks, (err, results) ->
for key, updates of data
payload = {}
payload[t1] = { Value: { N: String(t2) }, Action: 'PUT' } for t1, t2 of updates
console.log payload
dynamo.updateItem {
TableName: '--your-dynamo-table-name--'
Key: Key: S: key
AttributeUpdates: payload
}, (err) ->
throw err if err?
console.log '.' # FIXME: use async.parallel and process.exit to exit gracefully
# If something goes wrong...
# dynamo.scan {
# TableName: '--table-name--'
# }, (err, res) ->
# for item in res.Items
# dynamo.deleteItem {
# TableName: '--table-name--'
# Key: { Key: item.Key }
# }, -> console.log arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment