Created
January 11, 2011 14:50
-
-
Save siculars/774501 to your computer and use it in GitHub Desktop.
populate riak and return some of that data
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
/* | |
// paginate-riak.js | |
// use riak-js to paginate a set of data from a bucket in riak | |
// | |
// call the file like so: | |
// | |
// node paginate-riak bucketname start end | |
// | |
// ie. | |
// | |
// node populate-riak test 10 20 | |
// | |
*/ | |
var sys = require('sys'), | |
riak = require('/usr/local/lib/node/riak-js').getClient(), //the lib can be anywhere - use the correct path! | |
bucket = ( (process.ARGV[2]) ) ? process.ARGV[2] : 'default' , | |
start = ( !isNaN(parseFloat(process.ARGV[3])) && isFinite(process.ARGV[3]) ) ? process.ARGV[3] : 0 , //start should be a number | |
end = ( !isNaN(parseFloat(process.ARGV[4])) && isFinite(process.ARGV[4]) ) ? process.ARGV[4] : 10 ; //end should be a number | |
//dump the passed in args, sanity | |
sys.log( JSON.stringify(process.ARGV) ) | |
riak | |
//the bucket provided is used as the input | |
.add(bucket) | |
//using the internal json value mapper supplied by riak | |
.map('Riak.mapValuesJson') | |
//using the internal sort wrapper supplied by riak | |
//need to provide a sort function that has insight into our json object | |
//here we are sorting ascending by key | |
//descending would be function(a,b){ return a.key-b.key } | |
//if we wanted to sort by the unix timestamp replace key with ts | |
.reduce('Riak.reduceSort', 'function(a,b){ return a.key-b.key }' ) | |
//we are pushing an array of objects down the stack | |
//simply indicate a start and end to 'slice' a section of that array | |
//here we are supplying that on the command line | |
//the paramaters could easily be supplied by a web form or links | |
.reduce('Riak.reduceSlice', [start, end]) | |
.run(function(err, d) { | |
if (err) sys.log('error in m/r: ' + err) | |
//d is the data the m/r responds with | |
//do stuff here , i'm just dumping the returned meta | |
sys.log( JSON.stringify(d) ) | |
}); |
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
/* | |
// populate-riak.js | |
// use riak-js to populate riak with some default keys | |
// check to see if the keys exist first, if not write the keys | |
// | |
// call the file like so: | |
// | |
// node populate-riak bucketname volume | |
// | |
// ie. | |
// | |
// node populate-riak test 100 | |
// | |
*/ | |
var sys = require('sys'), | |
riak = require('/usr/local/lib/node/riak-js').getClient(), //the lib can be anywhere - use the correct path! | |
bucket = ( (process.ARGV[2]) ) ? process.ARGV[2] : 'default' , | |
volume = ( !isNaN(parseFloat(process.ARGV[3])) && isFinite(process.ARGV[3]) ) ? process.ARGV[3] : 0 ; //volume should be a number | |
//dump the passed in args, sanity | |
sys.log( JSON.stringify(process.ARGV) ) | |
for ( var i = 1; i <= volume; i++ ){ | |
sys.log(i) | |
riak.exists(bucket, i, {"i": i}, function(err, d, m){ | |
if (err) sys.log('problem with exists: ' + err) | |
//dumping return val + meta | |
sys.log( d + ' ' + JSON.stringify(m) ) | |
//d will be true or false | |
if ( !d ) { | |
sys.log( m['usermeta']['i'] ) | |
//keys will be numbered, val is a json object with the key and a unix time | |
riak.save(bucket, m['usermeta']['i'], {key: m['usermeta']['i'], ts: (new Date).getTime()}, function(err, d, m){ | |
if (err) sys.log('problem with save: ' + err) | |
//do stuff here , i'm just dumping the returned meta | |
sys.log( JSON.stringify(m) ) | |
}) | |
} | |
} ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment