/** * Deletes a Database with the specified name in mongo database to which user is connected to. * * @param dbName Name of Database to be deleted * @return Success if deleted else throws Exception * @throws DatabaseException throw super type of * UndefinedDatabaseException,DeleteDatabaseException * @throws ValidationException throw super type of EmptyDatabaseNameException */ public String dropDb(String dbName) throws DatabaseException, ValidationException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } try { boolean dbPresent = getDbList().contains(dbName); if (!dbPresent) { throw new DatabaseException( ErrorCodes.DB_DOES_NOT_EXISTS, "DB with name '" + dbName + "' DOES NOT EXIST"); } mongoInstance.dropDatabase(dbName); // newly added line getDbList().remove(dbName); } catch (MongoException e) { throw new DatabaseException(ErrorCodes.DB_DELETION_EXCEPTION, e.getMessage()); } return "Successfully dropped DB '" + dbName + "'. The page will reload now."; }
/** * Gets the result of the command * * @param dbName Name of Database * @param command Name of the Command to be executed * @param queryStr query to be performed. In case of empty query {} return all * @param keys Keys to be present in the resulted docs. * @param limit Number of docs to show. * @param skip Docs to skip from the front. * @return Result of executing the command. * @throws DatabaseException throw super type of UndefinedDatabaseException */ public JSONObject executeQuery( String dbName, String command, String queryStr, String keys, String sortBy, int limit, int skip) throws DatabaseException, JSONException, InvalidMongoCommandException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } // List<String> databaseNames = getDbList(); // if (!databaseNames.contains(dbName)) { // throw new DatabaseException(ErrorCodes.DB_DOES_NOT_EXISTS, // "DB with name [" + dbName + "]DOES_NOT_EXIST"); // } try { MongoDatabase db = mongoInstance.getDatabase(dbName); return DatabaseQueryExecutor.executeQuery(db, command, queryStr, keys, sortBy, limit, skip); } catch (MongoException e) { throw new DatabaseException(ErrorCodes.QUERY_EXECUTION_EXCEPTION, e.getMessage()); } }
protected void doBatch() throws KettleException { WriteConcern concern = null; if (log.getLogLevel().getLevel() >= LogLevel.DETAILED.getLevel()) { concern = new WriteConcern(1); } WriteResult result = null; if (concern != null) { result = m_data.getCollection().insert(m_batch, concern); } else { result = m_data.getCollection().insert(m_batch); } CommandResult cmd = result.getLastError(); if (cmd != null && !cmd.ok()) { String message = cmd.getErrorMessage(); logError(BaseMessages.getString(PKG, "MongoDbOutput.Messages.Error.MongoReported", message)); try { cmd.throwOnError(); } catch (MongoException me) { throw new KettleException(me.getMessage(), me); } } m_batch.clear(); }
/** * Method invoked when a {@link MongoSession} needs to be created. * * @param username the username to use for authentication. NOTE: Please use a dummy user if you * have disabled Mongo authentication * @param password the password to use for authentication. NOTE: Please use a dummy password if * you have disabled Mongo authentication * @param database Name of the database * @return the newly created {@link MongoSession} * @throws org.mule.api.ConnectionException */ @Connect public void connect( @ConnectionKey String username, @Password String password, @Optional @Default("test") String database) throws ConnectionException { DB db = null; try { MongoOptions options = new MongoOptions(); if (connectionsPerHost != null) { options.connectionsPerHost = connectionsPerHost; } if (threadsAllowedToBlockForConnectionMultiplier != null) { options.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier; } if (maxWaitTime != null) { options.maxWaitTime = maxWaitTime; } if (connectTimeout != null) { options.connectTimeout = connectTimeout; } if (socketTimeout != null) { options.socketTimeout = socketTimeout; } if (autoConnectRetry != null) { options.autoConnectRetry = autoConnectRetry; } if (slaveOk != null) { options.slaveOk = slaveOk; } if (safe != null) { options.safe = safe; } if (w != null) { options.w = w; } if (wtimeout != null) { options.wtimeout = wtimeout; } if (fsync != null) { options.fsync = fsync; } if (database != null) { this.database = database; } mongo = getOrCreateMongoInstance(host, port, options); db = getDatabase(mongo, username, password, database); } catch (MongoException me) { throw new ConnectionException(ConnectionExceptionCode.UNKNOWN, null, me.getMessage()); } catch (UnknownHostException e) { throw new ConnectionException(ConnectionExceptionCode.UNKNOWN_HOST, null, e.getMessage()); } this.client = new MongoClientImpl(db); }
public MongoTable(String host, String port, String dbName, String collectionName) { try { this.db = new Mongo(host, Integer.valueOf(port)).getDB(dbName); this.collection = db.getCollection(collectionName); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
public DbLogger(String dbHost, int dbPort, String dbName, String collectionName) { Mongo mongo; try { mongo = new Mongo(dbHost, dbPort); DB db = mongo.getDB(dbName); dbCollection = db.getCollection(collectionName); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
public static MongoClient getMongoClient() { if (mongo == null) { try { mongo = new MongoClient(host, port); LOGGER.debug("New Mongo created with [{}] and [{}]", host, port); } catch (UnknownHostException e) { LOGGER.error(e.getMessage()); } catch (MongoException e2) { LOGGER.error(e2.getMessage()); } } return mongo; }
@Override public void connectDb(String keyword) { try { initMongoDB(); items = db.getCollection(keyword); BasicDBObject index = new BasicDBObject("tweet_ID", 1).append("unique", true); // items.ensureIndex(index, new BasicDBObject("unique", true)); items.createIndex(index); } catch (MongoException ex) { System.out.println("MongoException :" + ex.getMessage()); } }
private static Mongo getMongo() { if (mongo == null) { try { mongo = new Mongo(HOST, PORT); System.out.println("New Mongo created with [" + HOST + "] and [" + PORT + "]"); } catch (MongoException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } } return mongo; }
public MongoDBGroup() { try { m = new Mongo(sHost); DB db = m.getDB(sDB); collection = db.getCollection(sCollection); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MongoException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void connectdb(String keyword) { try { // on constructor load initialize MongoDB and load collection initMongoDB(); items = db.getCollection(keyword); // make the tweet_ID unique in the database BasicDBObject index = new BasicDBObject("tweet_ID", 1); items.ensureIndex(index, new BasicDBObject("unique", true)); } catch (MongoException ex) { System.out.println("MongoException :" + ex.getMessage()); } }
/** @throws NullPointerException */ public static void PrintHighestJerseyNumber() throws NullPointerException { Mongo m = null; try { m = new Mongo("dbh63.mongolab.com", 27637); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } DB trendrr = m.getDB("trendrr"); /* authentication line */ trendrr.authenticate("", "".toCharArray()); DBCollection players = trendrr.getCollection("players"); BasicDBObject query = new BasicDBObject(); query.put("team", "jets"); query.put("position", "wr"); DBCursor cur = players.find(query); int maxJerseyNumber = 0; String playerName = ""; while (cur.hasNext()) { DBObject playerDetails = cur.next(); String nameAndNumber = playerDetails.get("name").toString(); String NameNumberTokens[] = nameAndNumber.split(","); try { if (NameNumberTokens.length != 2) { System.out.println("Invalid name field in the database"); System.out.println( "Name: " + NameNumberTokens[0] + " Jersey Number: " + NameNumberTokens[1]); } } catch (NullPointerException e) { System.out.println("NULL value in name field " + e); continue; } try { if (maxJerseyNumber < Integer.parseInt(NameNumberTokens[1].trim())) { maxJerseyNumber = Integer.parseInt(NameNumberTokens[1].trim()); playerName = NameNumberTokens[0]; } } catch (NumberFormatException e) { System.out.println("Invalid data in name field " + e); continue; } } System.out.println("Player Name: " + playerName); }
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try { // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); // Retrieve database DB db = client.getDB(dbname); // Retrieve collection names Set<String> collectionNames = db.getCollectionNames(); // Storage for results ValueSequence valueSequence = new ValueSequence(); // Iterate over collection names ; only pairs of collections // with names ending .chunks and .files are buckets for (String collName : collectionNames) { if (collName.endsWith(".chunks")) { String bucketName = StringUtils.removeEnd(collName, ".chunks"); if (collectionNames.contains(bucketName + ".files")) { valueSequence.add(new StringValue(bucketName)); } } } return valueSequence; } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, GridfsModule.GRFS0003, ex.getMessage()); } // return Sequence.EMPTY_SEQUENCE; }
@Override protected void annotateInjectorExceptions(Collection<Message> messages) { super.annotateInjectorExceptions(messages); for (Message message : messages) { if (message.getCause() instanceof MongoException) { MongoException e = (MongoException) message.getCause(); LOG.error( UI.wallString( "Unable to connect to MongoDB. Is it running and the configuration correct?\n" + "Details: " + e.getMessage())); System.exit(-1); } } }
public static void put(String id, String msg) { try { DBCollection message = Database.gardensharing().getCollection("message"); BasicDBObject document = new BasicDBObject(); document.put("id", id); document.put("msg", msg); message.insert(document); } catch (MongoException e) { e.printStackTrace(); } // messages.put(id, msg); }
/** * Gets the list of databases present in mongo database to which user is connected to. * * @return List of All Databases present in MongoDb * @throws DatabaseException If any error while getting database list. */ public List<String> getDbList() throws DatabaseException { try { Set<String> authenticatedDbNames = connectionDetails.getAuthenticatedDbNames(); if (!connectionDetails.isAdminLogin()) { return new ArrayList<String>(authenticatedDbNames); } List<String> dbs = new ArrayList<String>(); MongoCursor<String> dbsCursor = mongoInstance.listDatabaseNames().iterator(); while (dbsCursor.hasNext()) { dbs.add(dbsCursor.next()); } return dbs; } catch (MongoException m) { throw new DatabaseException(ErrorCodes.GET_DB_LIST_EXCEPTION, m.getMessage()); } }
public MongoDB() { try { mongo = new MongoClient(HOST, PORT); db = mongo.getDB(DB_NAME); table = db.getCollection(COLLECTION_NAME); dataLoader = new MongoDBDataLoader(table); } catch (final UnknownHostException unknownHostException) { System.err.println(unknownHostException.getMessage()); System.err.println(unknownHostException.getStackTrace()); } catch (final MongoException mongoException) { System.err.println(mongoException.getMessage()); System.err.println(mongoException.getStackTrace()); } }
@Override public List<DependencyStatus> status() { // note failures are tested manually for now, if you make changes test // things still work // TODO TEST add tests exercising failures final String version; try { final CommandResult bi = gfs.getDB().command("buildInfo"); version = bi.getString("version"); } catch (MongoException e) { LoggerFactory.getLogger(getClass()).error("Failed to connect to MongoDB", e); return Arrays.asList( new DependencyStatus( false, "Couldn't connect to MongoDB: " + e.getMessage(), "GridFS", "Unknown")); } return Arrays.asList(new DependencyStatus(true, "OK", "GridFS", version)); }
public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yourdb"); DBCollection collection = db.getCollection("dummyColl"); System.out.println("Testing 1..."); insertDummyDocuments(collection); // find hosting = hostB, and update it with new document BasicDBObject newDocument = new BasicDBObject(); newDocument.put("hosting", "hostB"); newDocument.put("type", "shared host"); newDocument.put("clients", 111); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 2..."); insertDummyDocuments(collection); BasicDBObject newDocument2 = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99)); collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument2); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 3..."); insertDummyDocuments(collection); BasicDBObject newDocument3 = new BasicDBObject() .append("$set", new BasicDBObject().append("type", "dedicated server")); collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Testing 4..."); insertDummyDocuments(collection); BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("clients", "888")); collection.update(new BasicDBObject().append("type", "vps"), updateQuery, false, true); printAllDocuments(collection); removeAllDocuments(collection); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } }
public String returnAllEmployees() throws Exception { ToJson converter = new ToJson(); // JSONArray json = new JSONArray(); String json = ""; try { /** ** Connect to MongoDB *** */ // Since 2.10.0, uses MongoClient // MongoClient mongo = new MongoClient("localhost", 27017); MongoClient mongo = new MongoClient("54.214.0.233", 27017); /** ** Get database *** */ // if database doesn't exists, MongoDB will create it for you DB db = mongo.getDB("telemedicine"); /** ** Get collection / table from 'testdb' *** */ // if collection doesn't exists, MongoDB will create it for you DBCollection table = db.getCollection("person"); /** ** Find and display *** */ BasicDBObject searchQuery = new BasicDBObject(); // searchQuery.put("name", name); DBCursor cursor = table.find(); System.out.println(cursor.count()); while (cursor.hasNext()) { System.out.println(cursor.next()); } json = converter.toJsonArray(cursor); } catch (MongoException e) { e.printStackTrace(); } catch (Exception Error) { Error.printStackTrace(); return json; } finally { // if (conn != null) conn.close(); } return json; }
public static Datastore getInstance() { if (ds == null) { Mongo mongo; try { mongo = new Mongo(); ds = new Morphia().createDatastore(mongo, "jg"); ds.ensureIndexes(); ds.ensureCaps(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MongoException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return ds; }
public MongoDatabaseConnection(DatabaseConfig config) throws DatabaseConnectionException { Mongo m = null; String hostName = config.get("host"); String dbName = config.get("database"); String collectionName = config.get("collection"); try { m = new Mongo(hostName); } catch (UnknownHostException e) { throw new DatabaseConnectionException(e.getMessage()); } catch (MongoException e) { throw new DatabaseConnectionException(e.getMessage()); } this.database = m.getDB(dbName); this.collection = database.getCollection(collectionName); }
/** * Return Stats of a particular Database in mongo to which user is connected to. * * @param dbName Name of Database * @return Array of JSON Objects each containing a key value pair in Db Stats. * @throws JSONException While parsing JSON * @throws DatabaseException Error while performing this operation * @throws ValidationException throw super type of EmptyDatabaseNameException */ public JSONArray getDbStats(String dbName) throws DatabaseException, ValidationException, JSONException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } JSONArray dbStats = new JSONArray(); try { List<String> dbList = getDbList(); boolean dbPresent = dbList.contains(dbName); if (!dbPresent) { throw new DatabaseException( ErrorCodes.DB_DOES_NOT_EXISTS, "DB with name '" + dbName + "' DOES NOT EXIST"); } MongoDatabase db = mongoInstance.getDatabase(dbName); Document stats = db.runCommand(new Document("dbStats", "1")); Set<String> keys = stats.keySet(); Iterator<String> keyIterator = keys.iterator(); while (keyIterator.hasNext()) { JSONObject temp = new JSONObject(); String key = keyIterator.next(); temp.put("Key", key); String value = stats.get(key).toString(); temp.put("Value", value); String type = stats.get(key).getClass().toString(); temp.put("Type", type.substring(type.lastIndexOf('.') + 1)); dbStats.put(temp); } } catch (MongoException m) { throw new DatabaseException(ErrorCodes.GET_DB_STATS_EXCEPTION, m.getMessage()); } return dbStats; }
@Override public void update(TestData testData) { try { DB db = MongoDBProvider.getInstance().getDB(); DBCollection coll = db.getCollection("testdata"); BasicDBObject query = new BasicDBObject(); query.put("source", testData.getClassName()); BasicDBObject object = new BasicDBObject(); object.put("source", testData.getClassName()); object.put("fails", testData.getFails()); coll.update(query, object, true, false); } catch (MongoException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } }
public static String get(String id) { // Since 2.10.0, uses MongoClient try { DBCollection message = Database.gardensharing().getCollection("message"); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("id", id); DBCursor cursor = message.find(searchQuery); if (cursor.hasNext()) { DBObject object = cursor.next(); return object.toString(); } else { return null; } } catch (MongoException e) { e.printStackTrace(); } return null; }
@Test public void insertDuplicateTest() throws Exception { DB db = getDb(); BasicDBObject doc = new BasicDBObject(); doc.put("username", "insertduplicate"); WriteResult result = db.getCollection("users").insert(doc); assertNull(result.getError()); // check we've created the collection Set<String> colls = db.getCollectionNames(); assertTrue(colls.contains("users")); // iterate the collection to ensure we can retrieve the object doc = new BasicDBObject(); doc.put("username", "insertduplicate"); String message = null; try { result = db.getCollection("users").insert(doc); } catch (MongoException me) { message = me.getMessage(); } assertNotNull(message); assertTrue( message.contains( "Entity users requires that property named username be unique, value of insertduplicate exists")); }
/** * Creates a Database with the specified name in mongo database to which user is connected to. * * @param dbName Name of Database to be created * @return Success if Created else throws Exception * @throws DatabaseException throw super type of * DuplicateDatabaseException,InsertDatabaseException * @throws ValidationException throw super type of EmptyDatabaseNameException */ public String createDb(String dbName) throws DatabaseException, ValidationException { if (dbName == null) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database name is null"); } if (dbName.equals("")) { throw new DatabaseException(ErrorCodes.DB_NAME_EMPTY, "Database Name Empty"); } try { boolean dbAlreadyPresent = getDbList().contains(dbName); if (dbAlreadyPresent) { throw new DatabaseException( ErrorCodes.DB_ALREADY_EXISTS, "DB with name '" + dbName + "' ALREADY EXISTS"); } // mongoInstance.getDatabase(dbName).listCollectionNames(); mongoInstance.getDatabase(dbName).createCollection("temp"); connectionDetails.addToAuthenticatedDbNames(dbName); } catch (MongoException e) { throw new DatabaseException(ErrorCodes.DB_CREATION_EXCEPTION, e.getMessage()); } return "Created DB with name '" + dbName + "'"; }
@Test public void testCreateRetryWithError() { Repository<Entity> mockRepo = Mockito.spy(repository); Map<String, Object> studentBody = buildTestStudentEntity(); Map<String, Object> studentMetaData = new HashMap<String, Object>(); int noOfRetries = 5; Mockito.doThrow(new MongoException("Test Exception")) .when(((MongoEntityRepository) mockRepo)) .internalCreate("student", null, studentBody, studentMetaData, "student"); Mockito.doCallRealMethod() .when(mockRepo) .createWithRetries("student", null, studentBody, studentMetaData, "student", noOfRetries); try { mockRepo.createWithRetries( "student", null, studentBody, studentMetaData, "student", noOfRetries); } catch (MongoException ex) { assertEquals(ex.getMessage(), "Test Exception"); } Mockito.verify((MongoEntityRepository) mockRepo, Mockito.times(noOfRetries)) .internalCreate("student", null, studentBody, studentMetaData, "student"); }
@Override public List<TestData> getList() { try { DB db = MongoDBProvider.getInstance().getDB(); DBCollection coll = db.getCollection("testdata"); List<TestData> results = new ArrayList<TestData>(); DBCursor cursor = coll.find(); while (cursor.hasNext()) { DBObject result = cursor.next(); TestData testData = new TestData((String) result.get("source"), (Integer) result.get("fails")); results.add(testData); } return results; } catch (MongoException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; }
@Override public TestData get(String className) { DB db = null; try { db = MongoDBProvider.getInstance().getDB(); DBCollection coll = db.getCollection("testdata"); BasicDBObject query = new BasicDBObject(); query.put("source", className); DBCursor cursor = coll.find(query); if (cursor.hasNext()) { DBObject result = cursor.next(); TestData testData = new TestData((String) result.get("source"), (Integer) result.get("fails")); return testData; } } catch (MongoException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } return new TestData(className, 0); }