节点写入流挂起

时间:2018-03-03 09:44:32

标签: javascript node.js neo4j stream

此节点脚本在records written: 442的最后一行之后挂起。

这意味着finish事件已被触发,但我不知道如何结束整个脚本。

const N3 = require('n3');
const fs = require('fs');
const neo4j = require('neo4j-driver').v1;

const uri = 'bolt://localhost';
const user = 'neo4j';
const password = 'sms1993662';
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
const session = driver.session();

let streamParser = N3.StreamParser();

let rdfStream = fs.createReadStream('/Users/simonshapiro/Documents/javascript/rdf_graphlib_js/data/sh.tll')
let number_of_rows = 0;

function DatastoreWriter () {
    let writer = new require('stream').Writable({objectMode: true});
    writer._write = (triple, encoding, done) => {
            console.log(triple);
            const mergePromise = session.run(
                `MERGE (n:Unidentified {id:"${triple.subject}"})`
            );
            mergePromise.then(() => {
                number_of_rows += 1;
                done();
            })
            .catch((e)=>{
                console.log('error', e);
                done();
            });
    }
    return writer
};
const neo4jWriter = new DatastoreWriter();
rdfStream.pipe(streamParser);
streamParser.pipe(neo4jWriter);
neo4jWriter.on('finish', ()=>{
        session.close();
        console.log('records written: ', number_of_rows);
    });

1 个答案:

答案 0 :(得分:1)

根据documentation,驱动程序应该关闭,可以这样做:

neo4jWriter.on('finish', ()=>{
  session.close(() => driver.close());
  console.log('records written: ', number_of_rows);
});