如何在node.js中流水线到Redis? [英] How to pipeline in node.js to redis?
问题描述
我有很多数据要插入(SET \ INCR)到Redis DB,所以我正在寻找管道 \ <通过 node.js a href ="http://redis.io/topics/mass-insert">大量插入.
I have lot's of data to insert (SET \ INCR) to redis DB, so I'm looking for pipeline \ mass insertion through node.js.
我在node.js中找不到任何好的示例/API,因此任何帮助都将非常有用!
I couldn't find any good example/ API for doing so in node.js, so any help would be great!
推荐答案
是的,我必须同意缺少示例,但是我设法创建了一个流,在该流上我批量发送了多个插入命令.
Yes, I must agree that there is lack of examples for that but I managed to create the stream on which I sent several insert commands in batch.
您应该安装用于redis流的模块:
You should install module for redis stream:
npm install redis-stream
这是您使用流的方式:
var redis = require('redis-stream'),
client = new redis(6379, '127.0.0.1');
// Open stream
var stream = client.stream();
// Example of setting 10000 records
for(var record = 0; record < 10000; record++) {
// Command is an array of arguments:
var command = ['set', 'key' + record, 'value'];
// Send command to stream, but parse it before
stream.redis.write( redis.parse(command) );
}
// Create event when stream is closed
stream.on('close', function () {
console.log('Completed!');
// Here you can create stream for reading results or similar
});
// Close the stream after batch insert
stream.end();
此外,您可以根据需要创建许多流,并随时根据需要打开/关闭它们.
Also, you can create as many streams as you want and open/close them as you want at any time.
在
There are several examples of using redis stream in node.js on redis-stream node module
这篇关于如何在node.js中流水线到Redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!