Advertisement
Guest User

Untitled

a guest
Jun 15th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. import com.datastax.driver.core.*;
  2. import com.sun.glass.ui.SystemClipboard;
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  * Created by rafcik on 10.05.2016.
  9.  */
  10. public class CassandraTwitter {
  11.     private static String contactPoint = "192.168.56.101";
  12.     private static String keyspace = "cassandra";
  13.  
  14.  
  15.     public static void main(String[] args) {
  16.         Cluster cluster = Cluster.builder().addContactPoint(contactPoint).build();
  17.         Session session = cluster.connect(keyspace);
  18.  
  19.         int action = 0;
  20.         long start = 0;
  21.         long stop = 0;
  22.         long diff = 0;
  23.  
  24.  
  25.         Scanner scanner = new Scanner(System.in);
  26.         do {
  27.             System.out.println("Co chcesz zrobić: ");
  28.             System.out.println("0 - koniec");
  29.             System.out.println("1 - wyświetl czas pobrania wszystkich użytkowników");
  30.             System.out.println("2 - wyświetl czas pobrania wszystkich tweetów");
  31.             //System.out.println("3 - wyświetl 10 użytkowników");
  32.             //System.out.println("4 - wyświetl czas pobrania wszystkich tweetów użytkownika");
  33.             System.out.println("5 - generowanie danych");
  34.             action = scanner.nextInt();
  35.  
  36.             switch (action) {
  37.                 case 1:
  38.                     start = System.currentTimeMillis();
  39.                     session.execute("SELECT * FROM users;");
  40.                     stop = System.currentTimeMillis();
  41.                     diff = stop - start;
  42.                     System.out.println("Czas wykonania: " + diff + " ms");
  43.                     break;
  44.                 case 2:
  45.                     start = System.currentTimeMillis();
  46.                     session.execute("SELECT * FROM tweets;");
  47.                     stop = System.currentTimeMillis();
  48.                     diff = stop - start;
  49.                     System.out.println("Czas wykonania: " + diff + " ms");
  50.                     break;
  51.                 case 3:
  52.                     Statement stmt = new SimpleStatement("select * FROM users limit 10;");
  53.                    // stmt.setFetchSize(10);
  54.  
  55.                     ResultSet results = session.execute(stmt);
  56.                     for (Row row : results) {
  57.                         System.out.println(row.getString("username"));
  58.                     }
  59.  
  60.                     break;
  61.                 case 4:
  62.                     System.out.println("Podaj nazwę użytkownika: ");
  63.                     String username = scanner.next();
  64.                     start = System.currentTimeMillis();
  65.                     session.execute("SELECT * FROM tweets where username = '" + username + "';");
  66.                     stop = System.currentTimeMillis();
  67.                     diff = stop - start;
  68.                     System.out.println("Czas wykonania: " + diff + " ms");
  69.                     break;
  70.                 case 5:
  71.                     generateData(session);
  72.                     break;
  73.             }
  74.         } while(action != 0);
  75.  
  76.         scanner.close();
  77.         System.out.println("stop");
  78.     }
  79.  
  80.     public static String randomString(final int length) {
  81.         Random r = new Random();
  82.         StringBuilder sb = new StringBuilder();
  83.         for(int i = 0; i < length; i++) {
  84.             char c = (char)(r.nextInt(25)+65);
  85.             sb.append(c);
  86.         }
  87.         return sb.toString();
  88.     }
  89.  
  90.     public static void createTables(Session session) {
  91.         session.execute("CREATE TABLE users (username text PRIMARY KEY, password text);");
  92.         session.execute("CREATE TABLE tweets (tweet_id uuid, username text, body text, PRIMARY KEY(tweet_id, username));");
  93.         session.execute("CREATE TABLE friends (username text, friend text, since timestamp, PRIMARY KEY (username, friend));");
  94.         session.execute("CREATE TABLE followers (username text, follower text, since timestamp, PRIMARY KEY (username, follower));");
  95.     }
  96.  
  97.     public static void generateData(Session session) {
  98.         Scanner scanner = new Scanner(System.in);
  99.         System.out.println("Podaj ilość użytkowników: ");
  100.         int usersNumber = scanner.nextInt();
  101.         System.out.println("Podaj ilość tweetów na użytkownika: ");
  102.         int tweetsNumber = scanner.nextInt();
  103.  
  104.         for(int i=0; i<usersNumber; i++) {
  105.             String username = randomString(8);
  106.             String password = randomString(15);
  107.  
  108.             long start = System.currentTimeMillis();
  109.             session.execute("INSERT INTO users (username, password) values ('" + username + "', '" + password + "');");
  110.  
  111.             for(int j=0; j<tweetsNumber; j++) {
  112.                 String tweetBody = randomString(110);
  113.                 session.execute("INSERT INTO tweets (tweet_id, username, body) values (uuid(), '" + username + "', '" + tweetBody + "');");
  114.             }
  115.             long stop = System.currentTimeMillis();
  116.             long diff = stop - start;
  117.             System.out.println("user[" + i + "]: " + username + " added in " + diff + "ms");
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement