在 MySQL 中使用 Streams 和 Node [英] Using Streams in MySQL with Node

查看:55
本文介绍了在 MySQL 中使用 Streams 和 Node的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 使用 Streams2 处理结果 上的示例,我'正在尝试将结果从 MySQL 流式传输到 node.js 中的 stdout.

Following the example on Piping results with Streams2, I'm trying to stream results from MySQL to stdout in node.js.

代码如下:

connection.query('SELECT * FROM table')
      .stream()
      .pipe(process.stdout);

我收到此错误:TypeError: invalid data

推荐答案

说明

来自这个 github issue 项目:

.stream() 以objectMode"返回流.你不能把它传送到标准输出或网络套接字,因为数据"事件将行作为有效负载,而不是缓冲区块

.stream() returns stream in "objectMode". You can't pipe it to stdout or network socket because "data" events have rows as payload, not Buffer chunks

修复

您可以使用 csv-stringify 解决此问题模块.

Fix

You can fix this using the csv-stringify module.

var stringify = require('csv-stringify');

var stringifier = stringify();


connection.query('SELECT * FROM table')
    .stream()
    .pipe(stringifier).pipe(process.stdout);

注意 .pipe(process.stdout)

这篇关于在 MySQL 中使用 Streams 和 Node的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆