Skip to content

Instantly share code, notes, and snippets.

@cliftonc
Last active May 29, 2016 11:00
Show Gist options
  • Save cliftonc/8ea74bf5671feedd625c8c8e7faa7786 to your computer and use it in GitHub Desktop.
Save cliftonc/8ea74bf5671feedd625c8c8e7faa7786 to your computer and use it in GitHub Desktop.
Load to redis
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();
@cliftonc
Copy link
Author

Using encode1:

used_memory_human:50.34M
# Keyspace
db0:keys=393664,expires=0,avg_ttl=0

DEBUG OBJECT 1244107
Value at:0x7fde7bfb7d20 refcount:1 encoding:ziplist serializedlength:75 lru:4901701 lru_seconds_idle:7

@cliftonc
Copy link
Author

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