我已经和rxjs
一起玩了一段时间了,我喜欢如何使用它的逻辑运算符而不是命令式编程。
然而,我也喜欢节点的流,它也是高度可组合的,所以我明显的反应是使用它们但我还没有看到它被提到很多(实际上,我还没有)除了在rxjs' s book中绑定它。
所以,我的问题是,如何利用RxJS上npm中的所有变换流?或者,甚至可能吗?
例如: -
var fs = require('fs');
var csv = require('csv-parse')({delimiter:';'});
var src = fs.createReadStream('./myFile.csv');
src.pipe(csv).pipe(process.stdout);
基本上,我想这样做: -
var fs = require('fs');
var csv = require('csv-parse')({delimiter:';'});
var rx= require('rx-node');
var src = fs.createReadStream('./myFile.csv');
var obj = rx.fromReadableStream(src);
obj.pipe(csb).map(x=>console.log(x));
过去我被告知要使用highland
,但我在此严格寻找rxjs
解决方案。
答案 0 :(得分:4)
你不必使用rx-node
,但你可以!请记住:All streams are event emitters!
。
准备:
input.txt
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
执行命令
npm install through2 split2 rx rx-node
在index.js
:
var Rx = require('rx');
Rx.Node = require('rx-node');
var fs = require('fs');
var th2 = require('through2');
var split2 = require('split2');
var file = fs.createReadStream('./input.txt').on('error', console.log.bind(console, 'fs err'));
var transform = th2(function(ch, en, cb) {
cb(null, ch.toString());
}).on('error', function(err) {
console.log(err, err.toString());
});
// All streams are event emitters ! (one way without using rx-node)
// var subs = Rx.Observable.fromEvent(transform, 'data').share();
// subs
// .map(value => 'Begin line: ' + value)
// .subscribe(value => console.log(value));
// rx-node has convenience functions (another way)
var subs = Rx.Node.fromTransformStream(transform).share()
.map(value => 'Begin line: ' + value)
.subscribe(value => console.log(value));
file.pipe(split2()).pipe(transform);
输出:
Begin line: Hello World!
Begin line: Hello World!
Begin line: Hello World!
Begin line: Hello World!
Begin line: Hello World!
答案 1 :(得分:1)
EdinM给出了一个将RxJS与节点变换流一起使用的一个很好的例子,但你原来的问题仍然没有答案。由于几天前我有几乎相同的问题,我想努力为那些不熟悉将RxJS与Node一起使用的人回答。我没有使用csv-parse
模块,而是使用csv-streamify
。让我们建立基本结构:
test_data.csv:
thing,name,owner,loc
chair,sitty,billy,san fran
table,setty,bryan,new oak
执行命令
$ npm install rx rx-node csv-streamify
index.js:
"use strict";
const Rx = require('rx');
Rx.Node = require('rx-node');
const fs = require('fs');
const csv = require('csv-streamify');
//Setting up the transform-stream CSV parser
let config = {
delimiter: ',', // comma, semicolon, whatever
newline: '\n', // newline character (use \r\n for CRLF files)
quote: '"', // what's considered a quote
empty: '', // empty fields are replaced by this
//objectMode: true, //parses csv table into an array of objects
//columns: true //uses column headers for the object fields
};
let parseCsv = csv(config);
//Setting up the RxJS Observer
function onNext (x) {
//do your side-effects here, after the data has
//gone through the observables operator chain
console.log('Next: ' + x);
};
function onError (err) {
console.log('Error: ' + err);
};
function onComplete () {
console.log('Completed');
};
let readStream = fs.createReadStream('test_files/test_data.csv');
readStream.pipe(parseCsv);
let subscription = Rx.Node.fromTransformStream(parseCsv)
//do something with the data with an operator such as:
//.map()
.subscribe(onNext, onError, onComplete);
现在让我们运行代码:
$ node index.js
我们将获得此输出:
Next: ["thing","name","owner","loc\r"]
Next: ["chair","sitty","billy","san fran\r"]
Next: ["table","setty","bryan","new oak"]
Completed
如果在csv配置对象中将objectMode
和columns
设置为true,然后将此sideEffect
函数与map运算符一起投影:
function sideEffect (v){
console.log(v)
return v;
};
let subscription = Rx.Node.fromTransformStream(parseCsv)
.map(sideEffect)
.subscribe(onNext, onError, onComplete);
您将获得此输出:
{ thing: 'chair',
name: 'sitty',
owner: 'billy',
'loc\r': 'san fran\r' }
Next: [object Object]
{ thing: 'table',
name: 'setty',
owner: 'bryan',
'loc\r': 'new oak' }
Next: [object Object]
Completed