Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.thaonedroid.mongodbexercise; import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Scanner; import org.bson.Document; /** * * @author thatOneDroid */ public class MongoDB { static Query query = new Query(); final static String q1 = "1. How many Twitter users are in our database?"; final static String q2 = "2. Which Twitter users link the most to other Twitter users?"; final static String q3 = "3. Who is are the most mentioned Twitter users?"; final static String q4 = "4. Who are the most active Twitter users? (Top 10)"; final static String q5 = "5. Who are the five most grumpy users?"; final static String q6 = "6. Who are the five most happy users?"; public static void main(String[] args) { try { MongoClientURI connStr = new MongoClientURI("mongodb://localhost:27017"); MongoClient mongoClient = new MongoClient(connStr); MongoDatabase db = mongoClient.getDatabase("twitter"); MongoCollection<Document> collection = db.getCollection("tweets"); Scanner scanner = new Scanner(System.in); int input; OUTER: do { start(); input = scanner.nextInt(); switch (input) { case 1: //System.out.println("Distinct Twitter users : " + query.totalUsers()); System.out.println("Question : " + q1); execute(query.totalUsers()); break; case 2: //execute(query.linkOthers()); System.out.println("Question : " + q2); System.out.println("Answer : Query hasn't been properly figured out yet"); break; case 3: //execute(query.mostMentioned()); System.out.println("Question : " + q3); System.out.println("Answer : Query hasn't been properly figured out yet"); break; case 4: System.out.println("Question : " + q4); execute(query.mostActive()); //System.out.println("Most active users : " + query.mostActive()); break; case 5: System.out.println("Question : " + q5); execute(query.mostGrumpy()); //System.out.println("Most grumpy users : " + query.mostGrumpy()); break; case 6: System.out.println("Question : " + q6); execute(query.mostHappy()); //System.out.println("Most happy users : " + query.mostHappy()); break; case 7: break OUTER; default: break; } } while (true); mongoClient.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ":" + e.getMessage()); } } private static void start() { System.out.println(""); System.out.println("Welcome to the Twitter database."); System.out.println(""); System.out.println("Input a number for the corresponding question to be answered."); System.out.println(""); System.out.println(q1); System.out.println(q2); System.out.println(q3); System.out.println(q4); System.out.println(q5); System.out.println(q6); System.out.println("7. End program."); } public static void execute(String q) { // implement the logic Runtime rt = Runtime.getRuntime(); try { String db = "twitter"; // run a command from terminal. this line is equivalent to // mongo database --eval "db.col.find()" // it calls the mongo binary and execute the javascript you // passed in eval parameter. It should work for both unix and // windows Process pr = rt.exec(new String[] { "mongo", db, "--eval", q }); // read the output of the command InputStream in = pr.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder out = new StringBuilder(); String line; int countLines = 1; while ((line = reader.readLine()) != null) { if (countLines > 2) { out.append("\n" + line); } countLines++; } // print the command and close outputstream reader System.out.println(""); System.out.println("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"); System.out.println("Answer : " + out.toString()); System.out.println("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-"); //1 System.out.println(out.toString()); reader.close(); } catch (IOException ex) { System.out.println(ex); } } }