Node.jsのを使用してMySQLデータベースにファイルのデータということは本当にLARGE JSONファイルや入力を読み込む方法?

sammani95:

私は、大規模なJSONファイル(22ギガバイト)を持っています。私は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 ('" + .... + "', .........;
sammani95:

私は、このための解決策を見つけました。おかげで、私を助けようとしただ、誰も。

これは私のDB接続(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");

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=279923&siteId=1