-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.js
More file actions
237 lines (187 loc) · 8.38 KB
/
queries.js
File metadata and controls
237 lines (187 loc) · 8.38 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const fs = require("fs");
const fastcsv = require("fast-csv");
const promise = require('bluebird'); // or any other Promise/A+ compatible library;
const initOptions = {
promiseLib: promise // overriding the default (ES6 Promise);
};
const pgp = require('pg-promise')(initOptions);
// See also: http://vitaly-t.github.io/pg-promise/module-pg-promise.html
// Database connection details;
const cn = {
// host: '18.221.231.160', // 'localhost' is the default;
host: '18.220.23.177',
port: 5432, // 5432 is the default;
database: 'sdc',
user: 'postgres',
password: '1234',
// timeout: 10000
};
var loaded = true;
const db = pgp(cn); // database instance;
if (!loaded) {
db.query(`drop table if exists data`).then((res) => {}).then(() => {
db.query(`CREATE TABLE IF NOT EXISTS data(
id SERIAL PRIMARY KEY,
product_id INT NOT NULL,
body TEXT NOT NULL ,
date_written VARCHAR(14) NOT NULL,
asker_name TEXT NOT NULL,
asker_email TEXT NOT NULL,
reported SMALLINT NOT NULL,
helpful SMALLINT NOT NULL
)`)}).then((res) => {
let stream = fs.createReadStream("/Users/mantaqaaoheen/desktop/SDC_Dat/questions.csv");
let csvData = [];
let csvStream = fastcsv
.parse()
.on("data", function(data) {
csvData.push(data);
if (data[0] % 10000 === 0) {
console.log('a', data[0]);
}
// console.log(csvData);
})
.on("end", function() {
// remove the first line: header
csvData.shift();
// var query = "INSERT INTO data (id, product_id, body, date_written, asker_name, asker_email, reported, helpful) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)";
const cs = new pgp.helpers.ColumnSet(['id', 'product_id', 'body', 'date_written', 'asker_name', 'asker_email', 'reported', 'helpful'], {table: 'data'})
var obj;
var container = [];
for (var i = 0; i< csvData.length; i++) {
var num = csvData[i][0];
if (num % 100000 === 0){
console.log(num)
}
// console.log('length', container.length);
if (container.length <= csvData.length/10000) {
container.push({id: parseInt(csvData[i][0]), product_id: parseInt(csvData[i][1]), body: csvData[i][2], date_written: csvData[i][3], asker_name: csvData[i][4], asker_email: csvData[i][5], reported: parseInt(csvData[i][6]), helpful: parseInt(csvData[i][7])})
} else {
container.push({id: parseInt(csvData[i][0]), product_id: parseInt(csvData[i][1]), body: csvData[i][2], date_written: csvData[i][3], asker_name: csvData[i][4], asker_email: csvData[i][5], reported: parseInt(csvData[i][6]), helpful: parseInt(csvData[i][7])})
const values = container;
const query = pgp.helpers.insert(values, cs);
db.any(query)
container = []
}
}
if (container.length > 0) {
const values = container;
const query = pgp.helpers.insert(values, cs);
db.any(query)
}
});
stream.pipe(csvStream);
}).then(() => {
db.query(`drop table if exists answers`).then((res) => {
db.query(`CREATE TABLE IF NOT EXISTS answers(
id SERIAL PRIMARY KEY,
question_id INT NOT NULL,
body TEXT NOT NULL,
date_written VARCHAR(14) NOT NULL,
answerer_name TEXT NOT NULL,
answerer_email TEXT NOT NULL,
reported SMALLINT NOT NULL,
helpful SMALLINT NOT NULL
)`)
}).then((res) => {
let stream = fs.createReadStream("/Users/mantaqaaoheen/desktop/SDC_Dat/answers.csv");
let csvData = [];
let csvStream = fastcsv
.parse()
.on("data", function(data) {
csvData.push(data);
if (data[0] % 10000 === 0) {
console.log('b', data[0]);
}
// console.log(csvData);
})
.on("end", function() {
// remove the first line: header
csvData.shift();
// var query = "INSERT INTO data (id, product_id, body, date_written, asker_name, asker_email, reported, helpful) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)";
const cs = new pgp.helpers.ColumnSet(['id', 'question_id', 'body', 'date_written', 'answerer_name', 'answerer_email', 'reported', 'helpful'], {table: 'answers'})
var obj;
var container = [];
for (var i = 0; i< csvData.length; i++) {
var num = csvData[i][0];
if (num % 100000 === 0){
console.log('loading answers', num)
}
// console.log('length', container.length);
if (container.length <= csvData.length/10000) {
container.push({id: parseInt(csvData[i][0]), question_id: parseInt(csvData[i][1]), body: csvData[i][2], date_written: csvData[i][3], answerer_name: csvData[i][4], answerer_email: csvData[i][5], reported: csvData[i][6], helpful: csvData[i][7]})
} else {
container.push({id: parseInt(csvData[i][0]), question_id: parseInt(csvData[i][1]), body: csvData[i][2], date_written: csvData[i][3], answerer_name: csvData[i][4], answerer_email: csvData[i][5], reported: csvData[i][6], helpful: csvData[i][7]})
const values = container;
const query = pgp.helpers.insert(values, cs);
db.any(query)
container = []
}
}
if (container.length > 0) {
const values = container;
const query = pgp.helpers.insert(values, cs);
db.any(query);
}
});
stream.pipe(csvStream);
})
}).then(() => {
db.query(`drop table if exists pictures`).then((res) => {
db.query(`CREATE TABLE IF NOT EXISTS pictures(
id SERIAL PRIMARY KEY,
answer_id INT NOT NULL,
url TEXT NOT NULL
)`)
}).then((res) => {
let stream = fs.createReadStream("/Users/mantaqaaoheen/desktop/SDC_Dat/answers_photos.csv");
let csvData = [];
let csvStream = fastcsv
.parse()
.on("data", function(data) {
csvData.push(data);
if (data[0] % 10000 === 0) {
console.log('c', data[0]);
}
// console.log(csvData);
})
.on("end", function() {
// remove the first line: header
csvData.shift();
// var query = "INSERT INTO data (id, product_id, body, date_written, asker_name, asker_email, reported, helpful) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)";
const cs = new pgp.helpers.ColumnSet(['id', 'answer_id', 'url', ], {table: 'pictures'})
var obj;
var container = [];
for (var i = 0; i< csvData.length; i++) {
var num = csvData[i][0];
if (num % 100000 === 0){
console.log('loading pictures', num)
}
// console.log('length', container.length);
if (container.length <= csvData.length/10000) {
container.push({id: parseInt(csvData[i][0]), answer_id: csvData[i][1], url: csvData[i][2]})
} else {
container.push({id: parseInt(csvData[i][0]), answer_id: csvData[i][1], url: csvData[i][2]})
const values = container;
const query = pgp.helpers.insert(values, cs);
db.any(query)
container = []
}
}
if (container.length > 0) {
const values = container;
const query = pgp.helpers.insert(values, cs);
db.any(query);
}
});
stream.pipe(csvStream);
}).then(() => {
db.any(`create index if not exists p_id on data using btree(product_id asc nulls last)`).then(() => {
db.any(`create index if not exists q_id on answers using btree(question_id asc nulls last)`).then(() => {
db.any(`create index if not exists a_id on pictures using btree(answer_id asc nulls last)`)
})
})
})
})
};
module.exports = {db};