Java tutorial
/* twitter-spell-check - Uses Natural Language Processing to spell-check tweets and add new data to dictionary Copyright (C) 2013 Sudeep Duggal This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Contact- Sudeep Duggal - sudeepduggal@gmail.com */ package com.nlp.twitterstream; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.Set; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; import com.mongodb.MongoURI; import com.mongodb.WriteConcern; import com.mongodb.WriteResult; import com.sun.org.apache.xerces.internal.util.URI; public class MongoUtil { private MongoClient mongoClient; DBObject myDoc; DB mongodb = null; MongoURI mongoURI = null; /** * Initialize MongoDB client - Singleton Currently commented for Heroku * functionality * * @throws Exception */ public void initMongo() throws Exception { // if(mongoClient == null) { // mongoClient = new MongoClient( "localhost" , 27017 ); // } if (mongoURI == null) { mongoURI = new MongoURI(System.getenv("MONGOHQ_URL")); } if (mongodb == null) { try { mongodb = mongoURI.connectDB(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } mongodb.authenticate(mongoURI.getUsername(), mongoURI.getPassword()); } /** * Authorize access to database * * @param dbName * database name * @param userName * user name * @param pass * password * @return true if authorization successful, false otherwise */ // public boolean authDB(String dbName, String userName, char[] pass) { // // DB db = mongoClient.getDB(dbName); // boolean auth = db.authenticate(userName, pass); // // return auth; // } /** * Get a database * * @param dbName * @return database object */ public DB getDB(String dbName) { // Commented for Heroku functionality // DB mongodb = mongoClient.getDB(dbName); return mongodb; } /** * Get collections list in a database * * @param mongodb * db object * @return collection list */ public Set<String> getCollectionsList(DB mongodb) { Set<String> colls = mongodb.getCollectionNames(); return colls; } /** * Get a particular collection Creates collection if it does not exist * * @param mongodb * db object * @param collectionName * string * @return DBCollection object */ public DBCollection getCollection(DB mongodb, String collectionName) { DBCollection collection = mongodb.getCollection(collectionName); return collection; } /** * Set the WriteConcern type * * @param writeConcernType */ // public void setWriteConcern(WriteConcern writeConcernType) { // mongoClient.setWriteConcern(writeConcernType); // } /** * Insert document into database * * @param collection * dbcollection object */ public WriteResult insertDoc(DBCollection collection, BasicDBObject basicDBObj) { WriteResult writeRes = collection.insert(basicDBObj); return writeRes; } /** * Get first document that matches selection in database * * @param collection * DBCollection object * @return DBObject */ public DBObject getOneDoc(DBCollection collection) { myDoc = collection.findOne(); return myDoc; } /** * Get first document that matches selection in database Return only * selected fields * * @param collection * DBCollection object * @return DBObject */ public DBObject getOneDocFields(DBCollection collection, DBObject obj1, DBObject obj2) { myDoc = collection.findOne(obj1, obj2); return myDoc; } /** * Get count of documents in DB * * @param collection * DBCollection object * @return count of documents */ public long getDocumentCount(DBCollection collection) { return collection.getCount(); } /** * Get all documents in collection * * @param collection */ public List<DBObject> getAllDocuments(DBCollection collection) { List<DBObject> dbObjList = new ArrayList<DBObject>(); DBCursor cursor = collection.find(); try { while (cursor.hasNext()) { dbObjList.add(cursor.next()); } } finally { cursor.close(); } return dbObjList; } /** * Get document list based on query * * @param collection * @param query * @return List<DBObject> */ public List<DBObject> getDocuments(DBCollection collection, BasicDBObject query) { List<DBObject> dbObjList = new ArrayList<DBObject>(); // Get cursor on documents DBCursor cursor = collection.find(query); try { while (cursor.hasNext()) { dbObjList.add(cursor.next()); } } finally { cursor.close(); } return dbObjList; } /** * Drop Collection Removes all documents and indexes */ public void dropCollection(DBCollection collection) { collection.drop(); } /** * Remove documents matching criteria * * @param collection * @param query * to match */ public WriteResult removeDocuments(DBCollection collection, BasicDBObject query) { WriteResult writeRes = collection.remove(query); return writeRes; } /** * Update documents with all param required * * @param collection * @param updateCriteria * @param updateAction * @param upsertStatus * When true, Inserts if document does not exist * @param multiStatus * When true, Affects multiple documents * @return WriteRes */ public WriteResult updateDocuments(DBCollection collection, BasicDBObject updateCriteria, BasicDBObject updateAction, boolean upsertStatus, boolean multiStatus) { WriteResult writeRes = collection.update(updateCriteria, updateAction, upsertStatus, multiStatus); return writeRes; } /** * Get list of indexes on a collection * * @param collection * @return index List<DBObject> */ public List<DBObject> getIndexList(DBCollection collection) { List<DBObject> list = collection.getIndexInfo(); return list; } /** * Get list of databases * * @return list of database names */ // public List<String> getDatabaseNames() { // // List<String> dbNames = mongoClient.getDatabaseNames(); // // return dbNames; // } /** * Drop database * * @param databaseName * String */ // public void dropDatabase(String databaseName) { // mongoClient.dropDatabase(databaseName); // } }