【问题标题】:How to read really LARGE JSON file and insert that file's data into a MYSQL database using node.js?如何读取真正大的 JSON 文件并使用 node.js 将该文件的数据插入 MYSQL 数据库?
【发布时间】:2020-06-22 06:53:53
【问题描述】:

我有很大的 JSON 文件(22GB)。我想读取该文件并使用 node.js 将该文件的数据输入到 MySql 数据库中。我该怎么做?

这是我的数据库查询:

var query = connection.query("INSERT INTO hotels (property_id, name, address,city, state_province_name, postal_code, 
        country_code, star_rate, latitude, longitude, category, rank, collect, property_collect, featured_image, breakfast_included, free_wifi_available) 
VALUES ('" + .... + "', .........;

【问题讨论】:

  • 欢迎来到 stackOverflow。请编辑问题以显示您尝试过的内容(完整代码),并提及您遇到的错误。另外,请提及您正在使用的 mysql 客户端模块,以及您系统上安装的 mysql 版本。请阅读stackoverflow.com/help/minimal-reproducible-example
  • 至少我不知道如何做到这一点。如果有人能给我一个解决方案,我很高兴。

标签: mysql node.js json large-data


【解决方案1】:

我找到了解决方案。谢谢所有试图帮助我的人。

这是我的数据库连接(db.js

var mysql = require('mysql');

//connect to db
var dbCon  = mysql.createPool({
   connectionLimit : 50,
   host: 'xxxxx',
   user: 'xxx',
   password: 'xxx',
   database: 'xxxxx',
   waitForConnections: true,
   queueLimit: 0,
 });

 dbCon.on('connection', function (connection) {
    console.log('db pool connection');
    connection.query("SET time_zone='+5:30'");
  });

 dbCon.on('release', function (connection) {
    console.log('Connection %d released', connection.threadId);
 });

 module.exports = {
   dbCon
 };

这是我的解决方案。

var express = require('express');
var app   = express();
var dbCon = require('./config/db').dbCon;
var fs = require('fs');
var readline = require('readline');
var stream = require('stream');
var data = '';

// Create a readable stream
var readerStream = fs.createReadStream('./data/myJson.jsonl');

// Set the encoding to be utf8. 
readerStream.setEncoding('UTF8');

var outstream = new stream();
//createInterface - read through the stream line by line and print out data from it
var r1 = readline.createInterface(readerStream, outstream);
var lineCount = 0;

r1.on('line', function (line) {
  // increment line count
  lineCount++;
  data = JSON.parse(line);

saveRecord(data);
})


function saveRecord(data) {

if (typeof data["city "] !== 'undefined') {
    var cityTemp = data["city"];
} else {
    cityTemp = "";
}

var property_id = data["property_id"];
var name = data["name"];
var city = cityTemp;

var sql = "INSERT INTO hotels (property_id, name, city) VALUES (?, ?, ?)";

dbCon.query(sql, [property_id, name, city]);

console.log('data inserted');
}

console.log("Program Ended");

【讨论】:

    【解决方案2】:

    对于这个大文件,流式传输是最好的方式。有一个名为etl 的软件包可以为您完成这项工作。像这样的

    etl.file('somefile')
      .pipe(etl.mysql.upsert(pool,'testschema','testtable',{concurrency:4 }))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      相关资源
      最近更新 更多