Created
December 10, 2013 08:10
-
-
Save blmarket/7887188 to your computer and use it in GitHub Desktop.
migration of Key-Value data from redis into dynamoDB
This file contains hidden or 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
| {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