-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertProductRecords.js
79 lines (65 loc) · 2.9 KB
/
insertProductRecords.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
lineReader will extract the records from amazon-meta.txt one at a time as
file is too large to read all at once. In order to add records to a database you need to add code below to insert records
This code depnds on "line-reader"
You need to install line-reader by using the following command:
npm install line-reader
*/
//This assumes that you're using mysql. You'll need to change this if you're using another database
var mysql = require('mysql'),
co = require('co'),
wrapper = require('co-mysql');
var query;
var jsonRecord;
var execute = true;
var query;
var totalRecords = 0;
var lineReader = require('line-reader');
/*************************You need to change this to be appropriate for your system************************************************************/
var connection = mysql.createConnection({
host : 'sampleprojects.clpsfhfwzha1.us-east-1.rds.amazonaws.com',
user : 'summer2016user',
password : 'EDISS_2016',
database : 'SampleProjects',
multipleStatements: 'true'
});
var sql = wrapper(connection);
var values = ""; //The records read from the file.
var numRecords = 0; //The current number of records read from the file.
/********************************You might need to adjust the block size. This specifies how many records to insert at once***********************/
var recordBlock = 5000; //The number of records to write at once.
lineReader.eachLine('./InputData/jsonRecords.json', function(line, last) {
execute = false;
currentLine = line.toString().replace(/'/g, "\"", "g");
try{
jsonRecord = JSON.parse(currentLine);
if (numRecords) {
values += ', ';
}
if(jsonRecord.description == null){
jsonRecord.description = "";
}
values += `('${jsonRecord.title}', '${jsonRecord.categories[0]}', '${jsonRecord.description}', '${jsonRecord.asin}')`;
numRecords++;
//****************************************************Change the query to align with your schema******************************************************/
if (numRecords == recordBlock) {
query = `INSERT INTO products (Name, PGroup, Description, ASIN) VALUES ${values};`; //Template, replaces ${values} with the value of values.
values = "";
numRecords = 0;
execute = true;
console.log(query);
}
}catch(err) {
execute = false;//there was a quote in the text and the parse failed ... skip insert
console.log(err);
}
if(execute){
co(function* () {
console.log("****************************************************in execute**************************************************************************************");
var resp = yield sql.query(query);
console.log("resp = " + resp);
totalRecords += recordBlock;
console.log(totalRecords + " records inserted.");
});
}//if(execute)
});//lineReader.eachLine