Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.datastax.driver.core.*;
- import com.sun.glass.ui.SystemClipboard;
- import java.util.Random;
- import java.util.Scanner;
- /**
- * Created by rafcik on 10.05.2016.
- */
- public class CassandraTwitter {
- private static String contactPoint = "192.168.56.101";
- private static String keyspace = "cassandra";
- public static void main(String[] args) {
- Cluster cluster = Cluster.builder().addContactPoint(contactPoint).build();
- Session session = cluster.connect(keyspace);
- int action = 0;
- long start = 0;
- long stop = 0;
- long diff = 0;
- Scanner scanner = new Scanner(System.in);
- do {
- System.out.println("Co chcesz zrobić: ");
- System.out.println("0 - koniec");
- System.out.println("1 - wyświetl czas pobrania wszystkich użytkowników");
- System.out.println("2 - wyświetl czas pobrania wszystkich tweetów");
- //System.out.println("3 - wyświetl 10 użytkowników");
- //System.out.println("4 - wyświetl czas pobrania wszystkich tweetów użytkownika");
- System.out.println("5 - generowanie danych");
- action = scanner.nextInt();
- switch (action) {
- case 1:
- start = System.currentTimeMillis();
- session.execute("SELECT * FROM users;");
- stop = System.currentTimeMillis();
- diff = stop - start;
- System.out.println("Czas wykonania: " + diff + " ms");
- break;
- case 2:
- start = System.currentTimeMillis();
- session.execute("SELECT * FROM tweets;");
- stop = System.currentTimeMillis();
- diff = stop - start;
- System.out.println("Czas wykonania: " + diff + " ms");
- break;
- case 3:
- Statement stmt = new SimpleStatement("select * FROM users limit 10;");
- // stmt.setFetchSize(10);
- ResultSet results = session.execute(stmt);
- for (Row row : results) {
- System.out.println(row.getString("username"));
- }
- break;
- case 4:
- System.out.println("Podaj nazwę użytkownika: ");
- String username = scanner.next();
- start = System.currentTimeMillis();
- session.execute("SELECT * FROM tweets where username = '" + username + "';");
- stop = System.currentTimeMillis();
- diff = stop - start;
- System.out.println("Czas wykonania: " + diff + " ms");
- break;
- case 5:
- generateData(session);
- break;
- }
- } while(action != 0);
- scanner.close();
- System.out.println("stop");
- }
- public static String randomString(final int length) {
- Random r = new Random();
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i < length; i++) {
- char c = (char)(r.nextInt(25)+65);
- sb.append(c);
- }
- return sb.toString();
- }
- public static void createTables(Session session) {
- session.execute("CREATE TABLE users (username text PRIMARY KEY, password text);");
- session.execute("CREATE TABLE tweets (tweet_id uuid, username text, body text, PRIMARY KEY(tweet_id, username));");
- session.execute("CREATE TABLE friends (username text, friend text, since timestamp, PRIMARY KEY (username, friend));");
- session.execute("CREATE TABLE followers (username text, follower text, since timestamp, PRIMARY KEY (username, follower));");
- }
- public static void generateData(Session session) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Podaj ilość użytkowników: ");
- int usersNumber = scanner.nextInt();
- System.out.println("Podaj ilość tweetów na użytkownika: ");
- int tweetsNumber = scanner.nextInt();
- for(int i=0; i<usersNumber; i++) {
- String username = randomString(8);
- String password = randomString(15);
- long start = System.currentTimeMillis();
- session.execute("INSERT INTO users (username, password) values ('" + username + "', '" + password + "');");
- for(int j=0; j<tweetsNumber; j++) {
- String tweetBody = randomString(110);
- session.execute("INSERT INTO tweets (tweet_id, username, body) values (uuid(), '" + username + "', '" + tweetBody + "');");
- }
- long stop = System.currentTimeMillis();
- long diff = stop - start;
- System.out.println("user[" + i + "]: " + username + " added in " + diff + "ms");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement