简体   繁体   English

读取大文件并将行逐行插入Node.JS中的数据库

[英]Reading large file and inserting line by line into database in Node.JS

I have a very large file that has a ton of JSON Strings(over 100K), with one string on each line. 我有一个非常大的文件,其中包含大量的JSON字符串(超过100K),每行一个字符串。

I want to read each line, insert it into a database, and after item has been inserted, I want to update another document in another database with basic information from the initial insert. 我想阅读每一行,将其插入数据库中,插入项目后,我想使用初始插入中的基本信息更新另一个数据库中的另一个文档。 And since I am a nodejs newb, I am having trouble wrapping my head around what I am doing wrong. 而且由于我是一个nodejs newb,所以我很难将自己的头绪围绕在我做错的事情上。 Here is what I have so far. 这是我到目前为止所拥有的。

var lineReader - require("line-reader");

lineReader.eachLine(filePath, function(line, last){
    if(count == 1){
        asyncAdd(JSON.parse(line));
    }
})}

var counter = 0;

function asyncAdd(jsonString){

async.waterfall([
        //this function calls the inserter
    function(callback){
        counter++;

        addJson(jsonString, function(doc){
            callback(null, doc);
            console.log("Added " + counter);
        })

    },
    //This function calls the indexing function
    function(doc, callback){

        console.log("indexing: " + counter);

        updateDBIndex(doc, function(err, savedDocument){
            callback(err, savedDocument);
        });
    }
    ],

    function(err, results){
        if(err){
            return console.error("Error " + err);
        }
        console.log("indexed " + counter);
    });
     }

Basically, if my file looks like: 基本上,如果我的文件看起来像:

{"_id": "1", "item":"glove", "color": "red"}\n
{"_id": "4", "item":"hat", "color" : "red"}\n
{"_id": "6", "item":"hat","color" : "blue"}\n

I want the output to look like, added 1 indexing 1 indexed 1 added 2 indexing 2 indexed 2 added 3 indexing 3 indexed 3 我希望输出看起来像,添加了1索引1索引1添加了2索引2索引2添加了3索引3索引3

Any help will be more than appreciated! 任何帮助将不胜感激! Thank you! 谢谢!

you could try to following snippet 您可以尝试按照以下代码段

var lineReader = require("line-reader");
var lineNumber = 0;
lineReader.eachLine(filePath, function (line, last) {
  asyncAdd(JSON.parse(line), lineNumber); // current line number
  lineNumber++; // increment for next one
});
function asyncAdd(jsonString, lineNum/*additional parameter*/) {
  async.waterfall([
      //this function calls the inserter
      function (callback) {
        addJson(jsonString, function (doc) {
          callback(null, doc);
          console.log("Added " + lineNum);
        })
      },
      //This function calls the indexing function
      function (doc, callback) {
        console.log("indexing: " + lineNum);
        updateDBIndex(doc, function (err, savedDocument) {
          callback(err, savedDocument);
        });
      }
    ],
    function (err, results) {
    if (err) {
      return console.error("Error " + err);
    }
    console.log("indexed " + lineNum);
  });
}

hope it works, the original was kind of incomplete. 希望它能奏效,原来有点不完整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM