简体   繁体   English

使用 Sequelize 从 JSON 文件导入大数据到 MYSQL 数据库

[英]Import large data from JSON file to MYSQL database using Sequelize


I am trying to import data from JSON file to mysql database using Sequelize .我正在尝试使用Sequelize将数据从 JSON 文件导入到 mysql 数据库。 I have written the following javascript code to achieve the same.我编写了以下 javascript 代码来实现相同的目的。 Though it is working for the small data set but when I ran it for large file (containing millions of records) it does not work and the errors I see like.虽然它适用于小数据集,但是当我为大文件(包含数百万条记录)运行它时,它不起作用,并且我看到了类似的错误。

  1. javascript heap out of memory then I ran with this node --max-old-space-size=4096 importRecords.js then I got 2nd error javascript heap out of memory 然后我用这个节点运行 --max-old-space-size=4096 importRecords.js然后我得到了第二个错误
  2. Unhandled rejection SequelizeConnectionAcquireTimeoutError: Operation timeout at pool.acquire.catch.error (F:\\demo-sequelize\\node_modules\\sequelize\\lib\\dialects\\abstract\\connection-manager.js:282:52)未处理的拒绝 SequelizeConnectionAcquireTimeoutError:pool.acquire.catch.error 处的操作超时(F:\\demo-sequelize\\node_modules\\sequelize\\lib\\dialects\\abstract\\connection-manager.js:282:52)
var Sequelize = require('sequelize');
var JM = require('json-mapper');
const fs = require('fs');

var sequelize = new Sequelize('testdb', 'root', 'root', {
    dialect : 'mysql',
    pool: {
      max: 5,
      min: 0,
      idle: 10000
    },
});

var Profile = sequelize.define('profile', {
  firstName: Sequelize.STRING,
  lastName: Sequelize.STRING,
  email: Sequelize.STRING
});

let rawdata = fs.readFileSync('largeData.json');
let input = JSON.parse(rawdata);

for(let customer of input){
  //console.log(customer.email);

  Profile.sync({force: true}).then(function () {
    // Table created
    return Profile.create({
      firstName: customer.firstName,
      lastName: customer.lastName,
      email: customer.email
    });
  });

}

Can anyone suggest how can I achieve this with谁能建议我如何实现这一目标

1. minimum time may be using asynchronous execution. 1.最短时间可能使用异步执行。
2. In optimal way by minimizing sequelize logging while execution 2. 通过在执行时最小化 sequelize 日志以最佳方式

I don't think reading this big file synchronously in memory is a good idea.我不认为在内存中同步读取这个大文件是个好主意。 Streaming is a better option in these kind of scenarios.在这些场景中,流式传输是更好的选择。 There are many packages available which can do this kind of job.有许多可用的软件包可以完成这种工作。 I will give an example for one of them.我将举一个例子。

stream-json ( https://github.com/uhop/stream-json ) - https://github.com/uhop/stream-json/wiki/StreamArray stream-json ( https://github.com/uhop/stream-json ) - https://github.com/uhop/stream-json/wiki/StreamArray

const fs = require("fs");
const StreamArray = require('stream-json/streamers/StreamArray');

async function insertRec(row) {
  console.log(row);
  // code to insert the record
}

async function process() {
  return new Promise((resolve, reject) => {
    fs.createReadStream('path to json having array')
      .pipe(StreamArray.withParser())
      .on("data", async row => {
        await insertRec(row);
      })
      .on("error", err => {
        reject(err);
      })
      .on("end", () => {
        console.log("CSV file successfully processed");
        resolve();
      });
  });
}

process();

this assumes you have a json in this format:这假设您有以下格式的 json:

[
  {
    "id": 1,
    "field": 2
  },
  {
    "id": 2,
    "field": 5
  }
]

THis will give you an idea on how to integrate with your existing solution.这将使您了解如何与现有解决方案集成。

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

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