· 9 years ago · Dec 12, 2016, 10:50 AM
1
2import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
3import com.fasterxml.jackson.core.JsonParser;
4import com.fasterxml.jackson.core.type.TypeReference;
5import com.fasterxml.jackson.databind.DeserializationFeature;
6import org.json.simple.JSONObject;
7import org.json.simple.parser.JSONParser;
8import org.json.simple.parser.ParseException;
9import com.fasterxml.jackson.databind.ObjectMapper;
10
11import java.io.*;
12import java.sql.*;
13import java.util.*;
14
15public class Main {
16
17 private static String TABLE_NAME = "POST";
18 private static Connection conn;
19
20 public static void main(String[] args) {
21 conn = null;
22 Statement st = null;
23
24 //JSONParser parser = new JSONParser();
25
26 try {
27 Class.forName("org.sqlite.JDBC");
28 conn = DriverManager.getConnection("jdbc:sqlite:test.db");
29 System.out.println("Success");
30
31 clearTable();
32
33 st = conn.createStatement();
34 String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
35 " (id TEXT," +
36 "parent_id TEXT," +
37 "link_id TEXT, " +
38 "name TEXT," +
39 "autor TEXT," +
40 "body TEXT," +
41 "subreddit_id TEXT," +
42 "subreddit TEXT," +
43 "score INT," +
44 "create_utc INT)";
45 st.executeUpdate(sql);
46 st.close();
47
48 } catch (ClassNotFoundException e) {
49 e.printStackTrace();
50 } catch (SQLException e) {
51 e.printStackTrace();
52 }
53 System.out.println("Table created successfully");
54 File file = new File("src\\main\\resources\\RC_2007-10-test.txt");
55 System.out.println(file.exists());
56 Long startTime = System.nanoTime();
57 System.out.println("Starting to read + insert..");
58
59
60 try {
61 //buffered fuck and simplejson
62 /*
63 FileReader fileReader = new FileReader(file);
64 BufferedReader bufferedReader = new BufferedReader(fileReader);
65 String line;
66 while ((line = bufferedReader.readLine()) != null) {
67 JSONObject o = (JSONObject) parser.parse(line);
68 objects.add(o);
69 //Long time = (System.nanoTime() - startTime) / 1000000;
70 //System.out.println("time elapsed: " + time);
71
72 }
73 */
74
75 ObjectMapper mapper = new ObjectMapper();
76 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
77
78 List<PostEntry> objects = new ArrayList<PostEntry>();
79 Iterator<PostEntry> iterator = mapper.reader(PostEntry.class).readValues(file);
80 while(iterator.hasNext()) {
81 objects.add(iterator.next());
82 }
83
84 // List<PostEntry> objects = Arrays.asList(mapper.readValues(file,PostEntry.class));
85 //mapper.readValue(file, new TypeReference<List<PostEntry>>() {});
86 System.out.println("Finished inserting");
87 Long time = (System.nanoTime() - startTime) / 1000000000;
88 System.out.println("Took " + time + " seconds to read file and add to list.");
89 insertAll(objects);
90 conn.close();
91 } catch (IOException e) {
92 e.printStackTrace();
93 } catch (SQLException e) {
94 e.printStackTrace();
95 }
96 }
97
98 private static void insertAll(List<PostEntry> arr) {
99 int l = arr.size();
100 System.out.println("Stating insert.");
101 long start = System.nanoTime();
102 for (PostEntry o : arr) {
103 insertRow(o);
104 }
105 long time = (System.nanoTime() - start) / 1000000000;
106 System.out.println("Done inserting " + l + " objects. Time: " + time);
107
108 }
109
110 private static void insertRow(PostEntry obj) {
111 try {
112 PreparedStatement stmt = conn.prepareStatement
113 ("insert into POST values(?,?,?,?,?,?,?,?,?,?)");
114
115 stmt.setString(1,obj.id);
116 stmt.setString(2,obj.parent_id);
117 stmt.setString(3,obj.link_id);
118 stmt.setString(4,obj.name);
119 stmt.setString(5,obj.author);
120 stmt.setString(6,obj.body);
121 stmt.setString(7,obj.subreddit_id);
122 stmt.setString(8,obj.subreddit);
123 stmt.setInt(9,obj.score);
124 stmt.setInt(10,obj.created_utc);
125 } catch(SQLException e) {
126 e.printStackTrace();
127 }
128 }
129
130/*
131 private static void insertRow(JSONObject o) {
132 try {
133 PreparedStatement stmt = conn.prepareStatement
134 ("insert into POST values(?,?,?,?,?,?,?,?,?,?)");
135 stmt.setObject(1, o.get("id"));
136 stmt.setObject(2, o.get("parent_id"));
137 stmt.setObject(3, o.get("link_id"));
138 stmt.setObject(4, o.get("name"));
139 stmt.setObject(5, o.get("author"));
140 stmt.setObject(6, o.get("body"));
141 stmt.setObject(7, o.get("subreddit_id"));
142 stmt.setObject(8, o.get("subreddit"));
143 stmt.setObject(9, o.get("score"));
144 stmt.setObject(10, o.get("created_utc"));
145
146 stmt.executeUpdate();
147 stmt.close();
148 } catch (SQLException e) {
149 e.printStackTrace();
150 }
151 }
152*/
153
154 private static void clearTable() {
155 try {
156 Statement statement = conn.createStatement();
157 String SQL = "DELETE FROM " + TABLE_NAME;
158 statement.execute(SQL);
159
160
161 } catch (SQLException e) {
162 e.printStackTrace();
163 }
164
165 }
166
167
168}