Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.text.DateFormat;
  13. import java.text.ParseException;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16.  
  17. //-----------------------------------------------------------------------------------------------------------
  18. //Created by: Philipp Schlechter
  19. //Date: 31/05/2017
  20. //-----------------------------------------------------------------------------------------------------------
  21. public class Einlesen {
  22.  
  23. // Methode zum Pruefen, ob ein Zeichen in einem String vorkommt
  24. private static boolean containsString( String s, String subString ) {
  25. return s.indexOf( subString ) > -1 ? true : false;
  26. }
  27.  
  28.  
  29. // Methode zum Einfuegen der Daten in die DB
  30. public static void importToDB(BufferedReader reader) throws SQLException, ParseException, ClassNotFoundException, NumberFormatException, IOException{
  31.  
  32. reader.readLine(); // ignore the header
  33. String l;
  34.  
  35. // Datumsformat definieren
  36. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  37.  
  38. // Variablen für DB-Interaktion
  39. PreparedStatement stmt = null;
  40. PreparedStatement stmt2 = null;
  41. PreparedStatement stmt3 = null;
  42. PreparedStatement stmt4 = null;
  43. PreparedStatement stmt5 = null;
  44. PreparedStatement stmt6 = null;
  45. PreparedStatement stmt7 = null;
  46. PreparedStatement stmt8 = null;
  47. Connection c = null;
  48.  
  49. // JDBC Treiber laden
  50. Class.forName("org.postgresql.Driver");
  51.  
  52. // Verbindung zur Datenbank herstellen
  53. c = DriverManager
  54. .getConnection("jdbc:postgresql://localhost:8080/Election",
  55. "testuser", "testpass");
  56.  
  57. while ((l = reader.readLine()) != null){
  58. // semicolon als Trennzeichen zum Zugriff auf einzelne Felder
  59. String[] ll = l.split(";");
  60. String candidate = ll[0];
  61. String text = ll[1];
  62. boolean isRetweet = Boolean.parseBoolean(ll[2]);
  63. String originalAuthor = ll[3];
  64. String hashtags = ll[4];
  65. java.sql.Date time = new java.sql.Date(df.parse(ll[5].replace("T"," ")).getTime());
  66. int retweetCount = Integer.parseInt(ll[6]);
  67. int favouriteCount = Integer.parseInt(ll[7]);
  68. String ID = ll[8];
  69.  
  70. // Einfuegen in Tabelle "Tweet"
  71. String insertTweet = "INSERT INTO \"Tweet\" (\"TweetID\", text, \"time\", retweet_count, favourite_count) " +
  72. "VALUES (?,?,?,?,?)";
  73. stmt = c.prepareStatement(insertTweet);
  74. stmt.setString(1, ID);
  75. stmt.setString(2, text);
  76. stmt.setDate(3, time);
  77. stmt.setInt(4, retweetCount);
  78. stmt.setInt(5, favouriteCount);
  79. stmt.executeUpdate();
  80.  
  81. // Einfuegen in Tabelle "contains"
  82. if(hashtags.length() != 0){
  83. if(containsString(hashtags,"+")){
  84. String[] tags = hashtags.split("\\+");
  85. for (int i = 0; i < tags.length; i++) {
  86. Statement checkContain = c.createStatement();
  87. String sql = "SELECT * FROM public.contains WHERE \"TweetID\"='" + ID + "' AND htext='" + tags[i] + "'";
  88. ResultSet rs = checkContain.executeQuery(sql);
  89. if(!rs.next()){ // Falls es den Eintrag noch nicht gibt
  90. String insertContain = "INSERT INTO public.contains (\"TweetID\", htext) VALUES (?, ?);";
  91. System.out.println(insertContain);
  92. stmt2 = c.prepareStatement(insertContain);
  93. stmt2.setString(1, ID);
  94. stmt2.setString(2, tags[i]);
  95. stmt2.executeUpdate();
  96. }
  97. }
  98. }else{
  99. String insertContain = "INSERT INTO public.contains (\"TweetID\", htext) VALUES (?, ?);";
  100. stmt2 = c.prepareStatement(insertContain);
  101. stmt2.setString(1, ID);
  102. stmt2.setString(2, hashtags);
  103. stmt2.executeUpdate();
  104. }
  105.  
  106. }
  107.  
  108. // Einfuegen in Tabelle User und Candidate
  109. Statement checkUser = c.createStatement();
  110. String sqlUser = "SELECT * FROM public.\"User\" WHERE \"UserName\"='" + candidate + "' OR \"UserName\"='" + originalAuthor + "'";
  111. ResultSet rs = checkUser.executeQuery(sqlUser);
  112. if(!rs.next()){
  113. String insertUser = "INSERT INTO public.\"User\" (\"UserName\") VALUES (?);";
  114. stmt4 = c.prepareStatement(insertUser);
  115. stmt4.setString(1, candidate);
  116. stmt4.executeUpdate();
  117. String insertCandidate = "INSERT INTO public.\"Candidate\" (\"UserName\") VALUES (?);";
  118. stmt5 = c.prepareStatement(insertCandidate);
  119. stmt5.setString(1, candidate);
  120. stmt5.executeUpdate();
  121. }
  122.  
  123. // Einfuegen in Tabelle User von Usern die keine Kandidaten sind
  124. Statement checkUser2 = c.createStatement();
  125. String sqlUser2 = "SELECT * FROM public.\"User\" WHERE \"UserName\"='" + originalAuthor + "'";
  126. ResultSet rs2 = checkUser2.executeQuery(sqlUser2);
  127. if(!rs2.next() && originalAuthor != ""){
  128. String insertUser2 = "INSERT INTO public.\"User\" (\"UserName\") VALUES (?);";
  129. stmt7 = c.prepareStatement(insertUser2);
  130. stmt7.setString(1, originalAuthor);
  131. stmt7.executeUpdate();
  132. }
  133.  
  134.  
  135. // Einfuegen in Tabelle writes
  136. String insertWrites = "INSERT INTO public.writes (\"UserName\", \"TweetID\") VALUES (?,?);";
  137. stmt6 = c.prepareStatement(insertWrites);
  138. stmt6.setString(1, candidate);
  139. stmt6.setString(2, ID);
  140. stmt6.executeUpdate();
  141.  
  142. // Einfuegen in Tabelle retweet_from
  143. if(isRetweet){
  144. String insertRetweet = "INSERT INTO public.retweet_from (\"UserName\", \"TweetID\") VALUES (?,?);";
  145. stmt8 = c.prepareStatement(insertRetweet);
  146. stmt8.setString(1, originalAuthor);
  147. stmt8.setString(2, ID);
  148. stmt8.executeUpdate();
  149. }
  150.  
  151.  
  152. }
  153.  
  154. // Einfuegen in Tabelle Hashtags
  155. Statement distinctTags = c.createStatement();
  156. String sqlTags = "SELECT DISTINCT htext FROM public.contains";
  157. ResultSet rsTags = distinctTags.executeQuery(sqlTags);
  158. while(rsTags.next()){
  159. String insertHashtags = "INSERT INTO public.\"Hashtag\" (htext) VALUES (?);";
  160. stmt3 = c.prepareStatement(insertHashtags);
  161. stmt3.setString(1, rsTags.getString("htext"));;
  162. stmt3.executeUpdate();
  163. }
  164.  
  165.  
  166.  
  167. }
  168.  
  169.  
  170. // Main Method
  171. // Input: american-election-tweets-clean.csv
  172. // Output: Data inserted to Database
  173. public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException, ParseException {
  174.  
  175. System.out.println("********************************************************************************");
  176. System.out.println("2. Iteration: Datenimport");
  177. System.out.println("********************************************************************************");
  178.  
  179. // Pruefen, ob Datei im Input vorhanden
  180. if (args.length != 1) {
  181. System.out.println("Only one object in input needed");
  182. } else {
  183. // Daten aus Excel-Datei auslesen
  184. BufferedReader reader = new BufferedReader(new FileReader(args[0]));
  185.  
  186. // Importfunktion aufrufen
  187. importToDB(reader);
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement