-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulate.java
640 lines (531 loc) · 26.6 KB
/
Populate.java
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package coen280_hw3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author shrut
*/
public class Populate {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws SQLException, ParseException, FileNotFoundException, IOException {
// TODO code application logic here
Populate pop = new Populate();
Connection connection = pop.ConnectToDB();
pop.CreateStatements(connection);
// pop.ClearTableData(connection);
// pop.InsertBusiness(connection);
// pop.InsertFriends(connection);
// pop.InsertMainCategories(connection);
pop.InsertSubCategories(connection);
// pop.InsertonlyMainCategories(connection);
// pop.InsertOnlySubCategories(connection);
//
// pop.InsertUsers(connection);
// pop.InsertReviews(connection);
// pop.InsertAttributes(connection);
// pop.closeDBConnection(connection);
}
public Connection ConnectToDB(){
String host = "localhost";
String dbName = "orcl";
int port = 1521;
String oracleURL = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbName;
String username = "Scott";
String password = "tiger";
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try {
connection = DriverManager.getConnection(oracleURL,username,password); //?user=root&password=xyz");
}
catch (SQLException E) {
System.out.println("SQLException:" + E.getMessage());
System.out.println("SQLState:" + E.getSQLState());
System.out.println("VendorError:" + E.getErrorCode());
}
return connection;
}
public void closeDBConnection(Connection connection) throws SQLException{
try{
connection.close();
}
catch(Exception E){
E.printStackTrace();
}
}
public void CreateStatements(Connection connection)throws SQLException{
try{
Statement s = connection.createStatement();
// System.out.println("==>"+connection);
int age = 20;
String query = "SELECT * FROM MainCategories";
// String dropquery = "DROP TABLE TEST";
s.executeQuery(query);
// s.execute(dropquery);
try{
// String update = "INSERT INTO TEST VALUES(\'"+pname+"\',"+age+")";
// s.executeUpdate(update);
}
catch (Exception e){
e.printStackTrace();
}
ResultSet res=s.getResultSet();
if (res!=null) {
while(res.next()){
// System.out.println("\n"+res.getString(1));
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void SelectStatements(Connection connection) throws SQLException{
Statement s = connection.createStatement();
try{
}
catch(Exception E){
E.printStackTrace();
}
}
public void InsertBusiness(Connection connection) throws ParseException, SQLException, IOException{
String data = "";
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM Business");
// prevstmt.executeUpdate();
JSONParser parser = new JSONParser();
try{
BufferedReader myReader = new BufferedReader(new FileReader("yelp_business.json"));
// BufferedReader myReader = new BufferedReader(new FileReader("BusinessTestJSON.json"));
String line = myReader.readLine();
while (line != null) {
data = line;
Object obj=null;
try{
// System.out.println(data);
obj = parser.parse(data);
}
catch(Exception e){
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
//reading json line by line
String business_id = (String) jsonObject.get("business_id");
String full_address =(String) jsonObject.get("full_address");
boolean open = (boolean)jsonObject.get("open");
// String main_category = ();
String city = (String) jsonObject.get("city");
long review_count = (long)jsonObject.get("review_count");
String name = (String)jsonObject.get("name");
double longitude = (double)jsonObject.get("longitude");
String state = (String) jsonObject.get("state");
double stars = (double)jsonObject.get("stars");
double latitude = (double)jsonObject.get("latitude");
String type = (String)jsonObject.get("type");
JSONObject attributes = (JSONObject)jsonObject.get("attributes");
// System.out.println(attributes.entrySet());
PreparedStatement stmt = connection.prepareStatement("INSERT INTO Business VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
try{
stmt.setString(1, business_id);
stmt.setString(2, full_address);
stmt.setBoolean(3, open);
stmt.setString(4, city);
stmt.setLong(5, review_count);
stmt.setString(6, name);
stmt.setDouble(7, longitude);
stmt.setString(8, state);
stmt.setDouble(9, stars);
stmt.setDouble(10, latitude);
stmt.setString(11, type);
stmt.setString(12, attributes.toString());
stmt.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
stmt.close();
line = myReader.readLine();
}
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public void InsertAttributes(Connection connection) throws IOException, SQLException{
//begin
String data = "";
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM Business");
// prevstmt.executeUpdate();
JSONParser parser = new JSONParser();
try{
BufferedReader myReader = new BufferedReader(new FileReader("yelp_business.json"));
// BufferedReader myReader = new BufferedReader(new FileReader("BusinessTestJSON.json"));
String line = myReader.readLine();
while (line != null) {
data = line;
Object obj=null;
try{
// System.out.println(data);
obj = parser.parse(data);
}
catch(Exception e){
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject) obj;
//reading json line by line
String business_id = (String) jsonObject.get("business_id");
JSONObject attributes = (JSONObject)jsonObject.get("attributes");
// JSONObject extends HashMap class
PreparedStatement stmt = connection.prepareStatement("INSERT INTO Attributes VALUES(?,?)");
Iterator<Map.Entry<Object, Object>> iterator = attributes.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<Object, Object> attrEntries = iterator.next();
if(attrEntries.getValue() instanceof JSONObject)
{
JSONObject jsonObj = (JSONObject) attrEntries.getValue();
Iterator<Map.Entry<Object, Object>> iterator1 = jsonObj.entrySet().iterator();
while (iterator1.hasNext()) {
Map.Entry<Object, Object> entry1 = iterator1.next();
String attribute_string=attrEntries.getKey()+""+entry1.getKey() + "" + entry1.getValue();
stmt.setString(1, jsonObject.get("business_id").toString());
stmt.setString(2,attribute_string);
stmt.executeUpdate();
}
}//end of iterator1 while loop
else{
String attribute_string=attrEntries.getKey()+"_"+attrEntries.getValue();
stmt.setString(1, jsonObject.get("business_id").toString());
stmt.setString(2,attribute_string);
stmt.executeUpdate();
}
}
stmt.close();
line = myReader.readLine();
}
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
//end
}
public void InsertMainCategories(Connection connection) throws FileNotFoundException, ParseException, SQLException{
// inserting the main category list statically on application run.
String[] mainCatSet = {"Active Life" ,"Arts & Entertainment", "Automotive", "Car Rental", "Cafes",
"Beauty & Spas", "Convenience Stores", "Dentists", "Doctors", "Drugstores", "Department Stores", "Education",
"Event Planning & Services", "Flowers & Gifts", "Food", "Health & Medical", "Home Services", "Home & Garden",
"Hospitals", "Hotels & Travel", "Hardware Stores", "Grocery", "Medical Centers", "Nurseries & Gardening",
"Nightlife", "Restaurants","Shopping","Transportation"};
ArrayList<String> mainCatArr = new ArrayList<>();
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM MainCategories");
// prevstmt.executeUpdate();
// inserting maincategories into the table
for(String s : mainCatSet){
PreparedStatement stmt = connection.prepareStatement("INSERT INTO MainCategories VALUES(?)");
// System.out.println(s);
stmt.setString(1,s);
stmt.executeUpdate();
}
PreparedStatement stmt2 = connection.prepareStatement("SELECT S.business_id FROM SubCategories S, MainCategories M WHERE M.category_name = S.sub_category_name");
stmt2.executeQuery();
ResultSet res =stmt2.getResultSet();
if (res!=null) {
while(res.next()){
// System.out.println("\n"+res.getString(1));
}
}
JSONParser parser = new JSONParser();
// File myfile = new File("yelp_business.json");
File myfile = new File("BusinessTestJSON.json");
Scanner myReader = new Scanner(myfile);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
Object obj = parser.parse(data);
JSONObject jsonObject = (JSONObject) obj;
//reading json line by line
// PreparedStatement stmt2 = connection.prepareStatement("INSERT INTO MainCategories SELECT ?,? "
// + "FROM SubCategories S, MainCategories M "
// + "WHERE M.category_name = S.sub_category_name ");
String business_id = (String) jsonObject.get("business_id");
// stmt2.setString(1, business_id);
// stmt2.setString(2,"");
// stmt2.executeUpdate();
}
// System.out.println(mainCatArr);
}
public void InsertSubCategories(Connection connection) throws SQLException, FileNotFoundException, ParseException, IOException{
//Inserting data into subCategories Table
String data = "";
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM SubCategories");
// prevstmt.executeUpdate();
JSONParser parser = new JSONParser();
PreparedStatement stmt = null;
BufferedReader myReader = new BufferedReader(new FileReader("yelp_business.json"));
// BufferedReader myReader = new BufferedReader(new FileReader("BusinessTestJSON.json"));
String line = myReader.readLine();
while (line != null) {
data = line;
Object obj = parser.parse(data);
JSONObject jsonObject = (JSONObject) obj;
//reading json line by line
String business_id = (String) jsonObject.get("business_id");
//categories as Java ArrayList<>
//
//categories as JSONArray
JSONArray subCategories = (JSONArray) jsonObject.get("categories");
// subCategories.remove(categoriesArr.size()-1);
Iterator<String> iterator = subCategories.iterator();
while(iterator.hasNext()){
stmt = connection.prepareStatement("INSERT INTO SubCategories VALUES(?,?)");
stmt.setString(1, business_id); //business_id column
stmt.setString(2, iterator.next().toString()); //category_name column
stmt.executeUpdate(); //check data?
stmt.close();
}
// System.out.println();
stmt.close();
line = myReader.readLine();
}
}
public void InsertUsers(Connection connection) throws ParseException, SQLException, IOException{
String data = "";
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM YelpUser");
// prevstmt.executeUpdate();
JSONParser parser = new JSONParser();
try{
BufferedReader myReader = new BufferedReader(new FileReader("yelp_user.json"));
// BufferedReader myReader = new BufferedReader(new FileReader("YelpUserTestJSON.json"));
String line = myReader.readLine();
while (line != null) {
data = line;
Object obj = parser.parse(data);
JSONObject jsonObject = (JSONObject) obj;
//reading json line by line
String yelping_since = (String) jsonObject.get("yelping_since");
long review_count = (long)jsonObject.get("review_count");
String name = (String)jsonObject.get("name");
String user_id = (String) jsonObject.get("user_id");
long fans = (long) jsonObject.get("fans");
double average_stars = (double)jsonObject.get("average_stars");
String type = (String)jsonObject.get("type");
JSONObject compliment = (JSONObject) jsonObject.get("compliments");
JSONArray elite = (JSONArray)jsonObject.get("elite");
// Comment: JSONObject extends HashMap class
jsonObject.keySet().forEach(keyStr ->{
Object keyvalue = jsonObject.get(keyStr);
});
PreparedStatement stmt = connection.prepareStatement("INSERT INTO YelpUser VALUES(To_date(? ,'YYYY-MM'),?,?,?,?,?,?,?,?)");
try{
stmt.setString(1, yelping_since);
stmt.setLong(2, review_count);
stmt.setString(3, name);
stmt.setString(4, user_id);
stmt.setLong(5, fans);
stmt.setDouble(6, average_stars);
stmt.setString(7, type);
stmt.setString(8, compliment.toString());
stmt.setString(9, elite.toString());
stmt.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
stmt.close();
line = myReader.readLine();
}
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public void InsertReviews(Connection connection) throws SQLException, IOException, ParseException{
String data = "";
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM Reviews");
// prevstmt.executeUpdate();
JSONParser parser = new JSONParser();
try{
// BufferedReader myReader = new BufferedReader(new FileReader("ReviewTestJSON.json"));
BufferedReader myReader = new BufferedReader(new FileReader("yelp_review.json"));
String line = myReader.readLine();
while (line != null) {
data = line;
Object obj = parser.parse(data);
JSONObject jsonObject = (JSONObject) obj;
//reading json line by line
String user_id = (String) jsonObject.get("user_id");
String review_id = (String)jsonObject.get("review_id");
long stars = (long) jsonObject.get("stars");
String review_date = (String) jsonObject.get("date");
String text = (String)jsonObject.get("text");
String type = (String) jsonObject.get("type");
String business_id = (String)jsonObject.get("business_id");
JSONObject votes = (JSONObject) jsonObject.get("votes");
PreparedStatement stmt = connection.prepareStatement("INSERT INTO Reviews VALUES(?,?,?,To_date(? ,'YYYY-MM-DD'),?,?,?)");
try{
stmt.setString(1, user_id);
stmt.setString(2, review_id);
stmt.setLong(3, stars);
stmt.setString(4, review_date);
stmt.setString(5, text);
stmt.setString(6, type);
stmt.setString(7, business_id);
stmt.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
stmt.close();
PreparedStatement stmt2 = connection.prepareStatement("INSERT INTO Votes VALUES(?,?,?,?)");
// Comment: JSONObject extends HashMap class
jsonObject.keySet().forEach(keyStr ->{
});
stmt2.setString(1, review_id);
stmt2.setLong(2, (long)votes.get("cool"));
stmt2.setLong(3, (long)votes.get("useful"));
stmt2.setLong(4,(long)votes.get("funny"));
stmt2.executeUpdate();
stmt2.close();
line = myReader.readLine();
}
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public void InsertonlyMainCategories(Connection connection) throws SQLException{
Statement s = connection.createStatement();
String query = "select distinct s.business_id,s.SUB_CATEGORY_NAME as only_main_categories from subcategories s, maincategories m where s.SUB_CATEGORY_NAME IN (select m2.CATEGORY_NAME from maincategories m2)";
s.executeQuery(query);
ResultSet res = s.getResultSet();
PreparedStatement stmt = null;
if(res != null){
while(res.next()){
stmt = connection.prepareStatement("INSERT INTO OnlyMainCategories VALUES(?,?)");
stmt.setString(1, res.getString(1));
stmt.setString(2, res.getString(2));
stmt.executeUpdate();
stmt.close();
System.out.println(res.getString(1)+" "+res.getString(2));
}
}
}
public void InsertOnlySubCategories(Connection connection) throws SQLException{
Statement s = connection.createStatement();
String query ="select s.business_id,s.SUB_CATEGORY_NAME as only_sub_categories from subcategories s, maincategories m where s.SUB_CATEGORY_NAME NOT IN (select m2.CATEGORY_NAME from maincategories m2)";
s.executeQuery(query);
ResultSet res = s.getResultSet();
PreparedStatement stmt = null;
if(res != null){
while(res.next()){
stmt = connection.prepareStatement("INSERT INTO OnlySubCategories VALUES(?,?)");
stmt.setString(1, res.getString(1));
stmt.setString(2, res.getString(2));
stmt.executeUpdate();
stmt.close();
System.out.println(res.getString(1)+" "+res.getString(2));
}
}
}
public void InsertFriends(Connection connection) throws IOException, ParseException, SQLException{
String data = "";
// PreparedStatement prevstmt = connection.prepareStatement("DELETE FROM YelpUser");
// prevstmt.executeUpdate();
JSONParser parser = new JSONParser();
try{
BufferedReader myReader = new BufferedReader(new FileReader("yelp_user.json"));
// BufferedReader myReader = new BufferedReader(new FileReader("YelpUserTestJSON.json"));
String line = myReader.readLine();
while (line != null) {
data = line;
Object obj = parser.parse(data);
JSONObject jsonObject = (JSONObject) obj;
String user_id = (String) jsonObject.get("user_id");
PreparedStatement stmt = connection.prepareStatement("INSERT INTO Friends VALUES(?,?)");
try{
JSONArray friend = (JSONArray) jsonObject.get("friends");
Iterator<String> iterator = friend.iterator();
while(iterator.hasNext()){
stmt.setString(1, iterator.next().toString());
stmt.setString(2, user_id.toString());
stmt.executeUpdate();
}
}
catch(Exception e){
e.printStackTrace();
}
stmt.close();
line = myReader.readLine();
}
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
private void ClearTableData(Connection connection) throws SQLException {
PreparedStatement attrstmt = connection.prepareStatement("DELETE FROM Attributes");
attrstmt.executeUpdate();
PreparedStatement friendsstmt = connection.prepareStatement("DELETE FROM Friends");
friendsstmt.executeUpdate();
PreparedStatement prevstmt6 = connection.prepareStatement("DELETE FROM Votes");
prevstmt6.executeUpdate();
PreparedStatement prevstmt5 = connection.prepareStatement("DELETE FROM Reviews");
prevstmt5.executeUpdate();
PreparedStatement prevstmtsub = connection.prepareStatement("DELETE FROM onlySubCategories");
prevstmtsub.executeUpdate();
// only subcategoriess etc
PreparedStatement prevstmtonlymain = connection.prepareStatement("DELETE FROM onlyMainCategories");
prevstmtonlymain.executeUpdate();
PreparedStatement prevstmt4 = connection.prepareStatement("DELETE FROM SubCategories");
prevstmt4.executeUpdate();
PreparedStatement prevstmt3 = connection.prepareStatement("DELETE FROM MainCategories");
prevstmt3.executeUpdate();
PreparedStatement prevstmt2 = connection.prepareStatement("DELETE FROM YelpUser");
prevstmt2.executeUpdate();
PreparedStatement prevstmt1 = connection.prepareStatement("DELETE FROM Business");
prevstmt1.executeUpdate();
prevstmt1.close();
prevstmt2.close();
prevstmt3.close();
prevstmt4.close();
prevstmt5.close();
prevstmt6.close();
prevstmtonlymain.close();
}
}