热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在节点中进行管道传输。js复述,?-Howtopipelineinnode.jstoredis?

Ihavelotsofdatatoinsert(SET\INCR)toredisDB,soImlookingforpipeline\massinsertio

I have lot's of data to insert (SET \ INCR) to redis DB, so I'm looking for pipeline \ mass insertion through node.js.

我有很多数据可以插入(SET \ INCR)到redis DB,所以我正在寻找通过node.js进行的管道\ mass插入。

I couldn't find any good example/ API for doing so in node.js, so any help would be great!

在node我找不到任何这样做的好例子/ API。js,任何帮助都是非常好的!

3 个解决方案

#1


10  

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.

是的,我必须承认这方面缺乏示例,但是我成功地创建了一个流,在这个流上,我批量地发送了几个insert命令。

You should install module for redis stream:

您应该为redis流安装模块:

npm install redis-stream

And this is how you use the 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中使用redis流有几个例子。在反向流节点模块上安装js

#2


5  

In node_redis there all commands are pipelined:

在node_redis中,所有命令都是流水线的:

https://github.com/mranney/node_redis/issues/539#issuecomment-32203325

https://github.com/mranney/node_redis/issues/539 # issuecomment - 32203325

#3


2  

You might want to look at batch() too. The reason why it'd be slower with multi() is because it's transactional. If something failed, nothing would be executed. That may be what you want, but you do have a choice for speed here.

您可能也想查看batch()。使用multi()时速度较慢的原因是它是事务性的。如果失败了,什么也不会执行。这可能是你想要的,但是你可以选择速度。

The redis-stream package doesn't seem to make use of Redis' mass insert functionality so it's also slower than the mass insert Redis' site goes on to talk about with redis-cli.

Redis -stream包似乎并没有利用Redis的大规模插入功能,因此它也比mass insert Redis的站点在谈到Redis -cli时要慢。

Another idea would be to use redis-cli and give it a file to stream from, which this NPM package does: https://github.com/almeida/redis-mass

另一个想法是使用redis-cli并给它一个文件以流,这个NPM包就是这样做的:https://github.com/almeida/redis-mass

Not keen on writing to a file on disk first? This repo: https://github.com/eugeneiiim/node-redis-pipe/blob/master/example.js

不喜欢先写磁盘上的文件?这种回购:https://github.com/eugeneiiim/node-redis-pipe/blob/master/example.js

...also streams to Redis, but without writing to file. It streams to a spawned process and flushes the buffer every so often.

…也流到Redis,但没有写入文件。它流到一个衍生的进程,并经常刷新缓冲区。

On Redis' site under mass insert (http://redis.io/topics/mass-insert) you can see a little Ruby example. The repo above basically ported that to Node.js and then streamed it directly to that redis-cli process that was spawned.

在Redis' site under mass insert (http://redis.io/topics/mass-insert)中,您可以看到一个小Ruby示例。上面的repo基本上将其移植到Node。然后将它直接流到生成的redis-cli进程。

So in Node.js, we have:

所以在节点。js,我们有:

var redisPipe = spawn('redis-cli', ['--pipe']);

var redisipe = spawn('redis-cli', ['- pipe']);

spawn() returns a reference to a child process that you can pipe to with stdin. For example: redisPipe.stdin.write().

spawn()返回对子进程的引用,您可以使用stdin将该进程转换为管道。例如:redisPipe.stdin.write()。

You can just keep writing to a buffer, streaming that to the child process, and then clearing it every so often. This then won't fill it up and will therefore be a bit better on memory than perhaps the node_redis package (that literally says in its docs that data is held in memory) though I haven't looked into it that deeply so I don't know what the memory footprint ends up being. It could be doing the same thing.

您可以继续写入缓冲区,将其流到子进程,然后每隔一段时间就清除它。这就不会把它填平,因此有点记忆比也许node_redis包(在其文档,从字面上说,数据保存在内存中)虽然我没有深入,所以我不知道内存占用最终被。它也可以做同样的事情。

Of course keep in mind that if something goes wrong, it all fails. That's what tools like fluentd were created for (and that's yet another option: http://www.fluentd.org/plugins/all - it has several Redis plugins)...But again, it means you're backing data on disk somewhere to some degree. I've personally used Embulk to do this too (which required a file on disk), but it did not support mass inserts, so it was slow. It took nearly 2 hours for 30,000 records.

当然要记住,如果出了问题,一切都会失败。这就是像fluentd这样的工具被创建的目的(这是另一个选项:http://www.fluentd.org/plugins/all——它有几个Redis插件)……但同样,这也意味着在某种程度上你在备份磁盘上的数据。我个人也使用Embulk进行此操作(这需要磁盘上的文件),但是它不支持大量插入,所以速度很慢。3万张唱片用了近2个小时。

One benefit to a streaming approach (not backed by disk) is if you're doing a huge insert from another data source. Assuming that data source returns a lot of data and your server doesn't have the hard disk space to support all of it - you can stream it instead. Again, you risk failures.

流处理方法的一个好处(不支持磁盘)是如果您正在从另一个数据源进行大量插入。假设数据源返回大量数据,并且您的服务器没有硬盘空间来支持所有这些数据,那么您可以对其进行流处理。你失败的风险。

I find myself in this position as I'm building a Docker image that will run on a server with not enough disk space to accommodate large data sets. Of course it's a lot easier if you can fit everything on the server's hard disk...But if you can't, streaming to redis-cli may be your only option.

我发现自己处于这个位置,因为我正在构建一个Docker映像,它将在一个没有足够的磁盘空间来容纳大型数据集的服务器上运行。当然,如果你能把所有东西都安装到服务器的硬盘上,那就容易多了。但如果不能,则可以将其流到redis-cli中。

If you are really pushing a lot of data around on a regular basis, I would probably recommend fluentd to be honest. It comes with many great features for ensuring your data makes it to where it's going and if something fails, it can resume.

如果你真的定期发布大量数据,我可能会建议fluentd说实话。它提供了许多很棒的特性,可以确保数据到达它要去的地方,如果失败了,它可以继续。

One problem with all of these Node.js approaches is that if something fails, you either lose it all or have to insert it all over again.

所有这些节点都存在一个问题。js的方法是,如果某件事失败了,要么全部丢失,要么重新插入。


推荐阅读
  • 本文探讨了Web开发与游戏开发之间的主要区别,旨在帮助开发者更好地理解两种开发领域的特性和需求。文章基于作者的实际经验和网络资料整理而成。 ... [详细]
  • 基于Node.js、Express、MongoDB和Socket.io的实时聊天应用开发
    本文详细介绍了使用Node.js、Express、MongoDB和Socket.io构建的实时聊天应用程序。涵盖项目结构、技术栈选择及关键依赖项的配置。 ... [详细]
  • 本文深入探讨了Redis中的两种主要持久化方式——RDB(Redis Database)和AOF(Append Only File),并详细解析了两者的实现机制、优缺点以及在实际应用中的选择策略。 ... [详细]
  • 使用 NDB 提升 Node.js 应用调试体验
    本文介绍了由 Google Chrome 实验室推出的新一代 Node.js 调试工具 NDB,旨在为开发者提供更加高效和便捷的调试解决方案。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • MySQL缓存机制深度解析
    本文详细探讨了MySQL的缓存机制,包括主从复制、读写分离以及缓存同步策略等内容。通过理解这些概念和技术,读者可以更好地优化数据库性能。 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • Redis Hash 数据结构详解
    本文详细介绍了 Redis 中的 Hash 数据类型及其常用命令。Hash 类型用于存储键值对集合,支持多种操作如插入、查询、更新和删除字段值。此外,文章还探讨了 Hash 类型在实际业务场景中的应用,并提供了优化建议。 ... [详细]
  • 本文探讨了如何使用pg-promise库在PostgreSQL中高效地批量插入多条记录,包括通过事务和单一查询两种方法。 ... [详细]
  • 本文深入探讨了 Prototype.js 框架及其与 JavaScript 原生 toString() 方法之间的区别,适合对前端开发感兴趣的开发者阅读。文章将帮助读者理解两者在功能实现和应用场景上的不同,从而更好地利用这些工具进行高效编程。 ... [详细]
  • Node.js 开发入门:环境搭建与配置
    随着Node.js技术的日益成熟及其即将发布的1.0稳定版,越来越多的开发者开始关注并尝试这一高性能的服务器端JavaScript平台。本文将引导读者如何在Windows环境下安装配置Node.js,并介绍一些常用的开发工具和框架。 ... [详细]
  • 本文详细介绍了C++标准模板库(STL)中各容器的功能特性,并深入探讨了不同容器操作函数的异常安全性。 ... [详细]
  • 每位开发者都应该拥有一个展示自我技能与分享知识的空间——个人技术博客。本文将指导你如何使用静态网站生成器Hexo结合GitHub Pages搭建这样一个平台。 ... [详细]
author-avatar
零食君
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有