public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String deleteProductValue = "false"; try { // Get the form data int productID = Integer.parseInt(request.getParameter("productID")); PrintWriter out = response.getWriter(); DB db = mongo.getDB("FashionFactoryProd"); DBCollection myProducts = db.getCollection("productInfo"); DBCollection myTrending = db.getCollection("myTrending"); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("productID", productID); DBCursor cursor = myProducts.find(searchQuery); if (cursor.count() == 0) { deleteProductValue = "false"; request.setAttribute("deleteProductValue", deleteProductValue); RequestDispatcher rd = request.getRequestDispatcher("deleteProduct.jsp"); rd.forward(request, response); } else { int product = 0; while (cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); product = obj.getInt("productID"); if (product == productID) { myProducts.remove(obj); myTrending.remove(searchQuery); deleteProductValue = "true"; request.setAttribute("deleteProductValue", deleteProductValue); RequestDispatcher rd = request.getRequestDispatcher("deleteProduct.jsp"); rd.forward(request, response); } } } } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { try { meet myobj = new meet(); // load jsiconfig.txt ArrayList<String> myconfiglist = new ArrayList<String>(); myconfiglist = myobj.loadArray("jsiconfig.txt"); // The text uri // "mongodb://*****:*****@ds023288.mongolab.com:23288/sample"; String textUri = myconfiglist.get(0); // Create MongoClientURI object from which you get MongoClient obj MongoClientURI uri = new MongoClientURI(textUri); // Connect to that uri MongoClient m = new MongoClient(uri); // get the database named sample String DBname = myconfiglist.get(1); DB d = m.getDB(DBname); // get the collection mycollection in sample String collectionName = myconfiglist.get(2); DBCollection collection = d.getCollection(collectionName); // System.out.println("Config: "+textUri+":"+DBname+":"+collectionName); // twitter4j // Twitter twitter = new TwitterFactory().getInstance(); Twitter twitter = new TwitterFactory().getSingleton(); User user = twitter.verifyCredentials(); // Twitter collection of latest tweets into the home account - defaulted to latest 20 tweets// ////////////////////////////////////////////////////////////// ArrayList<String> mylatesttweetslist = new ArrayList<String>(); // get list of tweets from a user or the next tweet listed try { long actid = 0; // twitter.createFriendship(actid); // The factory instance is re-useable and thread safe. // Twitter twitter = TwitterFactory.getSingleton(); List<Status> statuses = twitter.getHomeTimeline(); System.out.println("Showing home timeline."); for (Status status : statuses) { // System.out.println(status.getUser().getName() + ":" +status.getText()); // Addes timeline to an array String mytweets = status.getUser().getName() + ":" + status.getText(); mylatesttweetslist.add(mytweets); } ; } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } // MongoDB Insert Below // ////////////////////////// // System Date Date sd = new Date(); String sysdate = sd.toString(); // Toggle the below to display and insert the transactions as required boolean showtrans = true; boolean inserttrans = true; // checkArray - loads args to a text string to allow the contains function String checkString = ""; for (int ck = 0; ck < args.length; ck++) { checkString = checkString + args[ck]; } ; // display transactions flag on runnning jsimongo eg: java jsimongo -d will NOT display // transactions // insert transactions flag on runnning jsimongo eg: java jsimongo -i will NOT insert // transactions if (args.length > 0) { if (checkString.contains("-d")) showtrans = false; if (checkString.contains("-i")) inserttrans = false; } ; int x = 0; for (String atweet : mylatesttweetslist) { x++; // Display tweets to console if (showtrans == true) { System.out.println("tweet : " + atweet); System.out.println("Created_DateTime : " + sysdate); // was sysdate } ; // Insert JSON into MongoDB if (inserttrans == true) { BasicDBObject b = new BasicDBObject(); System.out.println("tweet : " + atweet); System.out.println("Created_DateTime : " + sysdate); // was sysdate // Insert the JSON object into the chosen collection collection.insert(b); } ; Thread.sleep(1); } ; System.out.println("End, the number of tweets inserted at this time was: " + x); } catch (Exception e) { e.printStackTrace(); } }
public DB getConnection() { return client.getDB(dbName); }
public static void main(String[] args) { // TODO Auto-generated method stub try { MongoClient mc = new MongoClient("localhost"); DB db = mc.getDB("mydb"); DBCollection dbc = db.getCollection("books"); /*BasicDBObject book= new BasicDBObject(); book.put("name", "자바의 정석"); book.put("pages", 600); dbc.insert(book); book= new BasicDBObject(); book.put("name", "스프링4"); book.put("pages", 250); dbc.insert(book); book= new BasicDBObject(); book.put("name", "JQuery"); book.put("pages", 150); dbc.insert(book); book= new BasicDBObject(); book.put("name", "오라클 프로그램"); book.put("pages", 800); dbc.insert(book); book= new BasicDBObject(); book.put("name", "AngularJS"); book.put("pages", 130); dbc.insert(book); book= new BasicDBObject(); book.put("name", "MongoDB"); book.put("pages", 650); dbc.insert(book); book= new BasicDBObject(); book.put("name", "Hadoop"); book.put("pages", 300); dbc.insert(book); book= new BasicDBObject(); book.put("name", "HIVE"); book.put("pages", 350); dbc.insert(book); book= new BasicDBObject(); book.put("name", "PIG"); book.put("pages", 650); dbc.insert(book); book= new BasicDBObject(); book.put("name", "HTML5"); book.put("pages", 360); dbc.insert(book);*/ String map = "function(){" + "var category;" + "if(this.pages>=300){" + "category='Big Books';" + "}else{" + "category='Small Books';}" + "emit(category,{name:this.name});}"; String reduce = "function(key,values){" + "var sum=0;" + "values.forEach(function(doc){" + "sum+=1;" + "});" + "return {books:sum};}"; MapReduceCommand cmd = new MapReduceCommand(dbc, map, reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = dbc.mapReduce(cmd); for (DBObject o : out.results()) { System.out.println(o.toString()); } } catch (Exception ex) { System.out.println(ex.getMessage()); } }