Last active
May 29, 2016 11:00
-
-
Save cliftonc/8ea74bf5671feedd625c8c8e7faa7786 to your computer and use it in GitHub Desktop.
Load to redis
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
var _ = require('lodash'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var written = 0; | |
var redis = require('redis'); | |
var client = redis.createClient(); | |
var stream = require('stream'); | |
var util = require('util'); | |
function encode1(key) { | |
return key; | |
} | |
function encode2(key) { | |
return (+key).toString(36); | |
} | |
function RedisStream () { | |
stream.Writable.call(this); | |
this.buffer = []; | |
}; | |
util.inherits(RedisStream, stream.Writable); | |
RedisStream.prototype.write = function (chunk) { | |
var self = this; | |
var data = JSON.parse(chunk); | |
if(self.buffer.length === 500) { | |
written += self.buffer.length; | |
client.multi(self.buffer).exec(function() { | |
self.buffer = []; | |
self.emit('drain'); | |
}); | |
if(written > 2000000) { process.exit(0); } | |
return false; | |
} else { | |
self.buffer.push(['hmset',encode2(data[0]), encode2(data[1]), 1]); | |
return true; | |
} | |
} | |
function load(config, next) { | |
console.log('Loading ...'); | |
read = 0; | |
written = 0; | |
var logger = setInterval(function() { | |
console.log('Loaded ' + written + ' ...'); | |
}, 5000); | |
var redisStream = new RedisStream(); | |
var headers = ['user_id','resource_id','download_date'] | |
var filePath = path.resolve('rd.csv'); | |
var csv = require('fast-csv'); | |
csv | |
.fromPath(filePath, {objectMode: false}) | |
.pipe(redisStream) | |
.on('end', function() { | |
clearInterval(logger); | |
client.quit(); | |
}) | |
} | |
load(); |
encode2:
used_memory_human:53.80M
db0:keys=393664,expires=0,avg_ttl=0
debug object qnyj
Value at:0x7fde75fd4740 refcount:1 encoding:ziplist serializedlength:85 lru:4901775 lru_seconds_idle:2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using encode1: