Node.js流链接承诺仅编写第一个

时间:2020-02-20 10:48:39

标签: node.js csv promise stream

我正在尝试制作一个使用异步生成器写入多个文件的流。

使用reduce promises是按顺序执行的,因此所需的输出是:

$ wc -l *
42 f1.csv
98 f2.csv
78 f3.csv

但是我得到以下结果:

$ wc -l *
42 f1.csv // only the first one is good
0  f2.csv // nothing in
0  f3.csv // same

AsyncGenerator()是ElasticSearch滚动搜索(Doc) 我对每个诺言向他发送不同的查询。

const { Readable } = require('stream');
const fs = require('fs');
const util = require('util');
const stream = require('stream');
const pipeline = util.promisify(stream.pipeline);

async function* Query(params) {

    var response = await ESClient.search(params);

    while (true) {
        const sourceHits = response.hits.hits;

        if (sourceHits.length === 0) {
            break;
        }

        for (const hit of sourceHits) {
            yield hit;
        }

        if (!response._scroll_id) {
            break;
        }

        response = await ESClient.scroll({
            scrollId: response._scroll_id,
            scroll: params.scroll
        });
    }
}

async function* AsyncGenerator() {

        const params = {
            index: 'apache-logs',
            scroll: '5s',
            _source: ["foo"],
            size: 2048,
            body: { query } // ElasticSearch DSL Query
        };

        for await (const res of Query(params)) {
            yield res;
        }
    }

async function WriteToFile(file) {

        return new Promise(async (resolve, reject) => {
            const input = Readable.from(AsyncGenerator());
            const output = fs.createWriteStream(file + '.csv');

            await pipeline(
                input,
                json2csv,
                output
            );
            resolve();   
        });
}

const lists = ["f1", "f2", "f3"];

const last = lists.reduce(async (previous, next) => {
            await previous;
            return WriteToFile(next);
}, Promise.resolve());

last.then(_ => { 
    console.log("DONE");  
}).catch((e) => {
    console.log(e);
});

(Lorem ipsum lalalaalalal(Sry stackoverflow要求提供更多详细信息,但我没有大声笑))

1 个答案:

答案 0 :(得分:0)

嗨,我不确定,因为我对NodeJ的了解很差。我认为您只调用一次创建.csv

的函数

    public class Wrapper
    {
        [JsonProperty("results")]

        public DataSet DataSet { get; set; }
    }
    public class Rootobject
    {
        public int response_code { get; set; }
        public Result[] results { get; set; }
    }

    public class Result
    {
        public string category { get; set; }
        public string type { get; set; }
        public string difficulty { get; set; }
        public string question { get; set; }
        public string correct_answer { get; set; }
        public string[] incorrect_answers { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {



            string json = (new WebClient()).DownloadString("https://opentdb.com/api.php?amount=10&type=boolean");
            DataSet ds = JsonConvert.DeserializeObject<Wrapper>(json).DataSet;

        }
    }
}