List of usage examples for com.mongodb Mongo getDB
@Deprecated public DB getDB(final String dbName)
From source file:org.fao.fenix.wds.web.rest.faostat.FAOSTATDownloadTable.java
License:Open Source License
private void save(Date date, String rest, String payload) throws UnknownHostException { MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(null); DB db = mongo.getDB(SCHEMA); DBCollection collection = db.getCollection("logs"); BasicDBObject document = new BasicDBObject(); document.put("date", date); document.put("rest", rest); DBObject dbObject = (DBObject) JSON.parse(payload); document.put("payload", dbObject); collection.insert(document);//from ww w .j ava 2 s .c o m }
From source file:org.fiware.apps.repository.dao.MongoDAOFactory.java
License:BSD License
public synchronized static DB createConnection() { Mongo m; DB db;/*from w w w . j av a 2s .c o m*/ try { m = new Mongo(RepositorySettings.getProperty("mongodb.host"), Integer.parseInt(RepositorySettings.getProperty("mongodb.port"))); db = m.getDB(RepositorySettings.getProperty("mongodb.db")); } catch (UnknownHostException | MongoException ex) { throw new MongoException(ex.getLocalizedMessage()); } return db; }
From source file:org.fracturedatlas.athena.apa.impl.MongoApaAdapter.java
License:Open Source License
public MongoApaAdapter(String host, Integer port, String dbName, String fieldsCollectionName) throws UnknownHostException { this.fieldsCollectionName = fieldsCollectionName; Mongo m = new Mongo(host, port); db = m.getDB(dbName); fields = db.getCollection(fieldsCollectionName); initializeIndex();/*from w w w . ja v a 2s . co m*/ }
From source file:org.geotools.data.mongodb.MongoDataStore.java
License:LGPL
/** * Get list of valid layers for this mongo DB; those containing at least one valid, non-null * GeoJSON geometry/*from w ww. j a va 2 s. c o m*/ */ @SuppressWarnings({ "rawtypes", "unchecked" }) private void getLayers() { Mongo mongo = null; try { // Get the list of collections from Mongo... mongo = new Mongo(config.getHost(), config.getPort()); DB db = mongo.getDB(config.getDB()); // TODO add authentication Set<String> colls = db.getCollectionNames(); for (String s : colls) { DBCollection dbc = db.getCollection(s); log.info("getLayers; collection=" + dbc); // find distinct non-null geometry to determine if valid layer // TODO branch point for separate geometry-specific layers per collection List geoList = dbc.distinct("geometry.type"); // distinct returns single BSON List, may barf if results large, > max doc. size // trap exception on props distinct and assume it's valid since there's obviously // something there (http://www.mongodb.org/display/DOCS/Aggregation) List propList = null; try { propList = dbc.distinct("properties"); } catch (IllegalArgumentException ex) { propList = new BasicBSONList(); propList.add("ex nihilo"); } catch (MongoException ex) { propList = new BasicBSONList(); propList.add("ex nihilo"); } // check that layer has valid geometry and some properties defined if (geoList != null && propList != null && propList.size() > 0) { boolean hasValidGeo = false; for (GeometryType type : GeometryType.values()) { if (geoList.contains(type.toString())) { hasValidGeo = true; break; } } if (hasValidGeo) { layers.add(new MongoLayer(dbc, config)); } } } } catch (Throwable t) { log.severe("getLayers error; " + t.getLocalizedMessage()); } if (mongo != null) { mongo.close(); } }
From source file:org.geotools.data.mongodb.MongoResultSet.java
License:LGPL
/** * Build features for given layer; convert mongo collection records to equivalent geoTools * SimpleFeatureBuilder// w w w . ja v a2s . c o m * * @param query mongoDB query (empty to find all) */ private void buildFeatures(BasicDBObject query) { if (layer == null) { log.warning("buildFeatures called, but layer is null"); return; } Mongo mongo = null; try { if (layer.getGeometryType() == null) { return; } mongo = new Mongo(layer.getConfig().getHost(), layer.getConfig().getPort()); DB db = mongo.getDB(layer.getConfig().getDB()); DBCollection coll = db.getCollection(layer.getName()); DBCursor cur = coll.find(query); minX = 180; maxX = -180; minY = 90; maxY = -90; SimpleFeatureBuilder fb = new SimpleFeatureBuilder(layer.getSchema()); // use SimpleFeatureBuilder.set(name, value) rather than add(value) since // attributes not in guaranteed order log.finer("cur.count()=" + cur.count()); while (cur.hasNext()) { DBObject dbo = cur.next(); if (dbo == null) { continue; } // get mongo id and ensure valid if (dbo.get("_id") instanceof ObjectId) { ObjectId oid = (ObjectId) dbo.get("_id"); fb.set("_id", oid.toString()); } else if (dbo.get("_id") instanceof String) { fb.set("_id", dbo.get("_id")); } else { throw new MongoPluginException("_id is invalid type: " + dbo.get("_id").getClass()); } // ensure geometry defined DBObject geo = (DBObject) dbo.get("geometry"); if (geo == null || geo.get("type") == null || (geo.get("coordinates") == null && geo.get("geometries") == null)) { continue; } // GeometryType of current record GeometryType recordGeoType = GeometryType.valueOf((String) geo.get("type")); // skip record if its geo type does not match layer geo type if (!layer.getGeometryType().equals(recordGeoType)) { continue; } // create Geometry for given type Geometry recordGeometry = createGeometry(recordGeoType, geo); if (recordGeometry != null) { fb.set("geometry", recordGeometry); // set non-geometry properties for feature (GeoJSON.properties) DBObject props = (DBObject) dbo.get("properties"); setProperties(fb, "properties", props); features.add(fb.buildFeature(null)); bounds = new ReferencedEnvelope(minX, maxX, minY, maxY, layer.getCRS()); } else { fb.reset(); } } } catch (Throwable t) { log.severe("Error building layer " + layer.getName() + "; " + t.getLocalizedMessage()); } if (mongo != null) { mongo.close(); } }
From source file:org.graylog2.hipchatalarmcallback.callback.GraylogMongoClient.java
License:Open Source License
/** * Gets the ID of the Graylog stream. //from ww w. j a va 2 s . c om * The stream is identified by the alarm's topic. * @param alarm Object of the alarm that was triggered. * @return The stream id. */ public String getStreamId(Alarm alarm) { String streamId = null; try { Mongo mongo = new Mongo(); // the streams collection DBCollection col = mongo.getDB("graylog2").getCollection("streams"); // get the name of the current stream String title = getStreamName(alarm); // find the stream by name (title = stream name) BasicDBObject query = new BasicDBObject("title", title); LOG.debug("query: " + query); DBObject message = col.findOne(query); if (message != null) { // get the stream id ObjectId id = (ObjectId) message.get("_id"); streamId = id.toString(); } else { LOG.debug("query did not return any documents."); } } catch (UnknownHostException e) { LOG.error("unable to query mongodb", e); streamId = null; } catch (MongoException e) { LOG.error("unable to query mongodb", e); streamId = null; } return streamId; }
From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java
License:Open Source License
@Override public Set<String> getCollections(Mongo mongo, String dbname) { DB db = mongo.getDB(dbname); return db.getCollectionNames(); }
From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java
License:Open Source License
@Override public Mongo getConnection(String host, int port, String username, String password) throws UnknownHostException { logger.info("connect to " + host + port); Mongo mongo = null; mongo = new Mongo(host, port); DB admin = mongo.getDB("admin"); if (username != null && username.length() > 0) { try {/*from w ww .j a v a 2s .c om*/ admin.authenticateCommand(username, password.toCharArray()); } catch (Exception e) { logger.log(Level.SEVERE, "", e); } } return mongo; }
From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java
License:Open Source License
@Override public MongoCollectionModel getAllDocuments(Mongo mongo, String dbname, String collectionName) { Gson gson = new GsonBuilder().create(); MongoCollectionModel model = new MongoCollectionModel(); StringBuilder bsonData = new StringBuilder(); List<Map> result = new ArrayList<Map>(); model.setQuery("db." + collectionName + ".find();"); Map data = new TreeMap(); DB db = mongo.getDB(dbname); DBCollection coll = db.getCollection(collectionName); DBCursor cursor = coll.find();//from w w w .j a va 2 s .c o m String output = null; try { while (cursor.hasNext()) { output = cursor.next().toString(); bsonData.append(output + "\n"); data = gson.fromJson(output, Map.class); result.add(data); } } finally { cursor.close(); } model.setBsonData(bsonData); model.setResult(result); return model; }
From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java
License:Open Source License
@Override public String insertDocument(String json, String dbname, String collectionName, Mongo mongo) { String result = null;/* w w w .j a v a 2 s . c om*/ DB db = mongo.getDB(dbname); DBCollection coll = db.getCollection(collectionName); DBObject dbObject = (DBObject) JSON.parse(json); coll.insert(dbObject); logger.info("insert object " + dbObject.toString()); result = "insert document : _id= " + dbObject.get("_id").toString(); return result; }