Using Streams in MySQL with Node
Explanation
From this github issue for the project:
.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
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);
notice the extra .pipe(stringifier)
before the .pipe(process.stdout)
Waylon Flinn
"A neutral good guy in a world with lawful evil tendencies." Projects weblas "The fastest way to do numerical computation in a browser. GPU + Javascript, no add-ons required." bdot "Numpy for big data", out-of-core dot products on large data sets. bquery "GROUP BY for big data", statistical analysis on large data sets. bcolz "Redshift on your laptop", a compressed columnar data store in library form. Both of the above projects are built on this.
Updated on June 08, 2022Comments
-
Waylon Flinn almost 2 years
Following the example on Piping results with Streams2, I'm trying to stream results from MySQL to stdout in node.js.
Code looks like this:
connection.query('SELECT * FROM table') .stream() .pipe(process.stdout);
I get this error:
TypeError: invalid data