How to read really LARGE JSON file and input that file's data into a MYSQL database using node.js?

sammani95 :

I have large JSON file(22GB). I want to read that file and input that file's data into a MySql database using node.js. How can I do this?

This is my database query:

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 :

I found a solution for this. Thanks, everyone who's tried to help me.

This is my DB connection(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
 };

This is my solution.

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");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read really LARGE JSON file and insert that file's data into a MYSQL database using node.js?

How to read file content in Node.js and convert the data to JSON?

How to import excel file using HTML Input file and read the file contents in Node.js (How to send the fullpath to Node.js)

Node Js : How to read and update json file

How to read a large JSON file as input for Azure Stream Analytics

How to post data to .json file using postman in node js

How to read a large file with node.js, and pull for changes constantly

How to read a section of a large file with Node.js?

How to use Node.js to read data from a json file and display the read data into html?

How to read large JSON file from Amazon S3 using Boto3

Node js CSV to JSON (Large file)

How to read data from an XML file and store it into database (MySQL)?

how can i input json file on mysql database?

Using Node.JS, how do I read a JSON file into (server) memory?

How to read content of JSON file uploaded to google cloud storage using node js

Unable to add data to MYSQL database from JSON file using PHP

React js importing a large data file (json)

Display data in html/js file using NodeJs from mysql database

how to save and read file from %temp% using node js?

how to send really really large json object as response - node.js with express

How to append JSON data to existing JSON file node.js

How do I read in data from a JSON file using dc.js?

storing JSON data value in one file using node js / javascript

Write / add data in JSON file using Node.js

How to get position in json file using JS (node.js)

Node.js: Read large text file only partialy

How to Insert the large json file into mysql with php?

Read a text file using Node.js?

Encountered - TypeError: Cannot read properties of undefined (reading 'posts') while trying to access data from a JSON file using Express(Node.js)