summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-07 12:31:49 +0200
committerMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-07 12:31:49 +0200
commit2f203acc7f28afce9e704cf27fd59d11e28ec2d5 (patch)
treed89a422c041932d04d56b2152933dc21540454a7 /src/main
parent4386d1efcd0c423c2fa0e199e65e1c29792bf54c (diff)
downloadDatafiller-2f203acc7f28afce9e704cf27fd59d11e28ec2d5.tar.gz
Added classes to refactor the database code.
* DBConnection creates a persistent conn. * DBQuery resolves and executes queries. * QueryUtils creates sql query text.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/Main.java114
-rw-r--r--src/main/Post.java181
2 files changed, 80 insertions, 215 deletions
diff --git a/src/main/Main.java b/src/main/Main.java
index 4d11951..dec057a 100644
--- a/src/main/Main.java
+++ b/src/main/Main.java
@@ -1,47 +1,93 @@
package main;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.util.Scanner;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import org.json.JSONObject;
+
+import database.DBConnection;
+import io.DataReader;
+import io.InputReader;
+import java.util.Arrays;
+
/**
- *
- * @author maurice
+ * The main class.
*/
public class Main {
-
+
/**
- *
- * @param args
+ * The main method of the application.
+ * @param args the global arguments to pass to the program.
*/
public static void main(String[] args) {
- // TODO code application logic here
-
- Scanner scanner=null;
try {
- scanner = new Scanner(new File("tweets-en.txt"));
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- while(scanner.hasNextLine()){
-
-
- JSONObject tweet = new JSONObject(scanner.nextLine());
- JSONObject user = (JSONObject) tweet.get("user");
- Post p = new Post(tweet,user);
- p.start();
- }
-
-
-
- } catch (Exception e){
- e.printStackTrace();
+ Main main = new Main(args);
+ } catch (IllegalArgumentException ex) {
+ System.err.println(ex.getMessage());
+ System.exit(1);
}
-
-
+ }
+
+ private String m_hostaddress;
+
+ private String m_filename;
+
+ public Main(String[] args) {
+ /* parse the global options. */
+ parseGlobalOptions(args);
+
+ if (m_hostaddress == null) {
+ throw new IllegalArgumentException("Missing --ip to specify the hostaddress.");
+ }
+ DBConnection connection = new DBConnection(m_hostaddress, "5432", "Twitter", "postgres", "2IOC02");
+ /* create the object that fills the database */
+ DataFiller filler = new DataFiller(connection);
+
+ if (m_filename == null) {
+ InputReader reader = new InputReader(filler);
+
+ reader.startLoop();
+ } else {
+ DataReader reader = new DataReader(m_filename, filler);
+ }
+ connection.close();
}
+
+ private void parseGlobalOptions(String[] args) {
+ /* parse global options */
+ for (int i = 0; i < args.length; i++) {
+ if ("--help".equals(args[i])) {
+ printHelp();
+ } else if("--dbhost".equals(args[i])) {
+ m_hostaddress = getParam(args, ++i);
+ } else if (args[i].startsWith("-")) {
+ throw new IllegalArgumentException("Invalid option: " + args[i]);
+ } else {
+ /* This should be the filename */
+ m_filename = getParam(args, i);
+ }
+ }
+ }
+
+ /** Read an extra option for a command. */
+ private String getParam(String[] args, Integer index) {
+ if (index + 1 <= args.length) {
+ index++;
+ return args[index - 1];
+ } else {
+ throw new IllegalArgumentException("An extra option was missing.");
+ }
+ }
+
+ private void printHelp() {
+ for (String line : HELP) {
+ System.out.println(line);
+ }
+ }
+
+ private final static String[] HELP = {
+ "Global options:",
+ " --help Print this help text.",
+ " --ip <ip> Specify the database ip address.",
+ " --file <tweet> <profile> Specify the tweet and profile filenames ",
+ " ",
+ " if no --file was specified read from standard input."
+ };
}
diff --git a/src/main/Post.java b/src/main/Post.java
deleted file mode 100644
index f3597c0..0000000
--- a/src/main/Post.java
+++ /dev/null
@@ -1,181 +0,0 @@
-package main;
-
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
-import java.io.File;
-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.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-/**
- *
- * @author s129778
- */
-public class Post implements Runnable{
- Thread runner;
- JSONObject tweet;
- JSONObject twuser;
-
-
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- PreparedStatement pst = null;
- String url = "jdbc:postgresql://131.155.240.72:5432/Twitter";
- String user = "postgres";
- String password = "2IOC02";
- public Post() {
- }
- public void start(){
- runner.start();
- }
- public Post(JSONObject tweet, JSONObject user) {
- //create new tread
-
- runner = new Thread(this, "post");
- this.twuser = user;
- this.tweet = tweet;
- }
-
-
- public void run() {
- try {
- con = DriverManager.getConnection(url, user, password);
- st = con.createStatement();
- rs = st.executeQuery("SELECT VERSION()");
-
- //st.executeUpdate("CREATE TABLE name (ID INT)");
- //st.executeUpdate("DROP TABLE name");
- //st.executeQuery("SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'name' AND pid <> pg_backend_pid();");
- //st.executeUpdate("DROP DATABASE name");
- //st.executeUpdate("CREATE ROLE joe2 PASSWORD 'ok' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;");
- //select query
- String query = "SELECT * FROM tweet WHERE tweetid<=9000000 ORDER BY tweetid";
- pst = con.prepareStatement(query);
- rs = pst.executeQuery();
- //process select query
- while ( rs.next() ) {
- int id = rs.getInt("tweetid");
- System.out.println( "ID = " + id );
- System.out.println (rs.getTimestamp("createdat"));
- }
-
- //insert query with check for existance
- //for(int i=0;i<900000;i++){
- String update = "INSERT INTO tweet (tweetid,createdat,favcount,retweetcount,text) SELECT ? , ? ,?,?, ? WHERE NOT EXISTS (SELECT * FROM tweet WHERE tweetid= ? )";
- pst = con.prepareStatement(update);
- pst.setLong(1, tweet.getLong("id"));
- pst.setString(2, tweet.getString("created_at"));
- pst.setLong(3, tweet.getLong("favorite_count"));
- pst.setLong(4, tweet.getLong("retweet_count"));
- pst.setString(5, tweet.getString("text"));
- pst.setLong(6, tweet.getLong("id"));
- pst.executeUpdate();
-
-
-
- update = "INSERT INTO ispostedby (tweetid,userid) SELECT ? , ? WHERE NOT EXISTS (SELECT * FROM ispostedby WHERE tweetid= ? )";
- pst = con.prepareStatement(update);
- pst.setLong(1, tweet.getLong("id"));
- pst.setLong(2, twuser.getLong("id"));
- pst.setLong(3, tweet.getLong("id"));
- pst.executeUpdate();
-
- //update = "INSERT INTO twitteruser (userid,displayname,timezone,tweetcount,followercount,followedcount,location,brand) Select 1";
- update = "INSERT INTO twitteruser (userid,displayname,timezone,tweetcount,followercount,followedcount,location) SELECT ? ,? ,? ,? , ?, ? ,? WHERE NOT EXISTS (SELECT * FROM twitteruser WHERE userid= ? )";
- pst = con.prepareStatement(update);
- pst.setLong(1, twuser.getLong("id"));
- pst.setString(2, twuser.getString("name"));
- pst.setString(3, twuser.getString("time_zone"));
- pst.setLong(4, twuser.getLong("statuses_count"));
- pst.setLong(5, twuser.getLong("followers_count"));
- pst.setLong(6, twuser.getLong("friends_count"));
- pst.setString(7, twuser.getString("location"));
- pst.setLong(8, twuser.getLong("id"));
- pst.executeUpdate();
-
- ArrayList<String> brands=getBrands();
- for(String brand : brands){
- update = "INSERT INTO likesbrand (userid,brand) SELECT ? , ? WHERE NOT EXISTS (SELECT * FROM likesbrand WHERE userid= ? AND brand = ?)";
- pst = con.prepareStatement(update);
-
- pst.setLong(1, twuser.getLong("id"));
- pst.setString(2, brand);
- pst.setLong(3, twuser.getLong("id"));
- pst.setString(4, brand);
- pst.executeUpdate();
- }
- //}
- //update
- //pst = con.prepareStatement("UPDATE tweet set text = 'j' where tweetid=1;");
- //pst.executeUpdate();
-
- //delete
- //pst = con.prepareStatement("DELETE from tweet where tweetid=1;");
- //pst.executeUpdate();
-
-
-
-
-
-
-
-
- } catch (JSONException ex) {
- Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex);
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(Main.class.getName());
- lgr.log(Level.SEVERE, ex.getMessage(), ex);
-
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (st != null) {
- st.close();
- }
- if (con != null) {
- con.close();
- }
-
- } catch (SQLException ex) {
- Logger lgr = Logger.getLogger(Main.class.getName());
- lgr.log(Level.WARNING, ex.getMessage(), ex);
- }
- }
-
-
- }
-
- ArrayList<String> getBrands(){
- ArrayList<String> result= new ArrayList<String>();
- String text=null;
- try {
- text = tweet.getString("text");
- text = text.toLowerCase();
- } catch (JSONException ex) {
- Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex);
- }
- if(text.contains("samsung")||text.contains("galaxy")){
- result.add("Samsung");
- System.out.println("samsung");
- } else {
- result.add("geen");
- System.out.println(text);
- }
- return result;
- }
-}