List of usage examples for com.mongodb.client MongoDatabase getCollection
MongoCollection<Document> getCollection(String collectionName);
From source file:org.apache.rya.mongodb.MongoDBQueryEngine.java
License:Apache License
private MongoCollection<Document> getCollection(final MongoDBRdfConfiguration conf) { final MongoDatabase db = mongoClient.getDatabase(conf.getMongoDBName()); return db.getCollection(conf.getTriplesCollectionName()); }
From source file:org.apache.sling.nosql.mongodb.resourceprovider.impl.MongoDBNoSqlAdapter.java
License:Apache License
/** * @param mongoClient MongoDB client// w w w. j av a 2 s .c o m * @param database MongoDB database * @param collection MongoDB collection */ public MongoDBNoSqlAdapter(MongoClient mongoClient, String database, String collection) { MongoDatabase db = mongoClient.getDatabase(database); this.collection = db.getCollection(collection); }
From source file:org.apache.storm.mongodb.common.MongoDBClient.java
License:Apache License
public MongoDBClient(String url, String collectionName) { //Creates a MongoURI from the given string. MongoClientURI uri = new MongoClientURI(url); //Creates a MongoClient described by a URI. this.client = new MongoClient(uri); //Gets a Database. MongoDatabase db = client.getDatabase(uri.getDatabase()); //Gets a collection. this.collection = db.getCollection(collectionName); }
From source file:org.auraframework.test.perf.PerfResultsUtil.java
License:Apache License
public static void writeToDb(PerfMetrics metrics, String test) { try {/*from www. j av a 2s . co m*/ MongoClient mongo = getMongoClient(); if (mongo != null) { MongoDatabase db = mongo.getDatabase("performance"); MongoCollection<Document> runs = db.getCollection("testRun"); JSONObject json = metrics.toJSONObject(); Document doc = Document.parse(json.toString()); doc.append("testName", test); doc.append("run", RUN_TIME); runs.insertOne(doc); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.auraframework.test.perf.util.PerfResultsUtil.java
License:Apache License
public static void writeToDb(PerfExecutorTestCase test, String testName, String dbURI, PerfMetrics metrics, String traceLog) {/* ww w .j av a 2 s . co m*/ try { MongoClient mongo = getMongoClient(dbURI); if (mongo != null) { LOG.info("Writing perf results into mongo db at: " + mongo.getAddress()); MongoDatabase db = mongo.getDatabase("performance"); MongoCollection<Document> runs = db.getCollection("testRun"); JSONObject json = metrics.toJSONObject(); Document doc = Document.parse(json.toString()); doc.append("timeline", traceLog); doc.append("testName", testName); doc.append("transaction", Document.parse((metrics.getMetricsServiceTransaction()).toString())); doc.append("commonMetrics", Document.parse((metrics.getCommonMetrics()).toString())); doc.append("customMetrics", Document.parse((metrics.getCustomMetrics()).toString())); doc.append("run", RUN_TIME); runs.insertOne(doc); exportToCsv(test, doc); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.bananaforscale.cormac.dao.collection.CollectionDataServiceImpl.java
License:Apache License
/** * Removes a collection with a given name. * * @param databaseName the database/*from w ww . ja va2s. c om*/ * @param collectionName the collection to remove * @return the result of the operation * @throws DatasourceException * @throws NotFoundException */ @Override public boolean removeCollection(String databaseName, String collectionName) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (!collectionExists(databaseName, collectionName)) { throw new NotFoundException("The collection doesn't exist in the datasource"); } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); mongoDatabase.getCollection(collectionName).drop(); return true; } catch (MongoException ex) { logger.error("An error occured while deleting the collection", ex); throw new DatasourceException("An error occured while deleting the collection"); } }
From source file:org.bananaforscale.cormac.dao.database.DatabaseDataServiceImpl.java
License:Apache License
/** * Creates a new database explicitly. Because MongoDB creates a database implicitly when the * database is first referenced in a command, this method is not required for usage of said * database.// w ww . j av a 2 s .c o m * * @param databaseName the database to create * @return the result of the operation * @throws DatasourceException * @throws ExistsException */ @Override public boolean addDatabase(String databaseName) throws DatasourceException, ExistsException { try { if (databaseExists(databaseName)) { throw new ExistsException("The database already exists in the datasource"); } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); String collectionName = "temp" + UUID.randomUUID(); mongoDatabase.createCollection(collectionName); mongoDatabase.getCollection(collectionName).drop(); return true; } catch (MongoException ex) { logger.error("An error occured while adding the database", ex); throw new DatasourceException("An error occured while adding the database"); } }
From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java
License:Apache License
/** * Returns all the documents in a collection. * * @param databaseName the database/*from w w w . j a v a 2s . c o m*/ * @param collectionName the collection * @param query a JSON query param in the style of mongo * @param fields fields to return * @param skip the amount of documents to skip * @param limit the amount of documents to limit the result to * @param orderBy order ascending or descending by property * @param includeId determines whether to include the Mongo "_id" field * @return the documents in a collection * @throws DatasourceException * @throws NotFoundException */ @Override public List<String> getAll(String databaseName, String collectionName, String query, String fields, String skip, String limit, String orderBy, boolean includeId) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (!collectionExists(databaseName, collectionName)) { throw new NotFoundException("The collection doesn't exist in the datasource"); } Integer intSkip, intLimit; try { intSkip = Integer.parseInt(skip); } catch (NumberFormatException ex) { intSkip = 0; } try { intLimit = Integer.parseInt(limit); } catch (NumberFormatException ex) { intLimit = 0; } // 1 or -1 to specify an ascending or descending sort respectively. Document orderByObject = null; if (orderBy != null && !orderBy.isEmpty()) { if (orderBy.contains("ascending")) { String[] parts = orderBy.split(":"); orderByObject = new Document(parts[0], 1); } else if (orderBy.contains("descending")) { String[] parts = orderBy.split(":"); orderByObject = new Document(parts[0], -1); } } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); MongoCollection collection = mongoDatabase.getCollection(collectionName); FindIterable iterable = (query == null || query.isEmpty()) ? collection.find() : collection.find(Document.parse(query)); // TODO: Figure out how to do this in new API // if (fields != null && !fields.isEmpty()) { // // expect the form to be field:value,field:value // Document document = new Document(); // String[] parts = fields.split(","); // for (String part : parts) { // String[] tempParts = part.split(":"); // document.append(tempParts[0], tempParts[1]); // } // iterable.projection(document); // } iterable.skip(intSkip); iterable.limit(intLimit); if (orderByObject != null) { iterable.sort(orderByObject); } Iterator<Document> curIter = iterable.iterator(); List<String> documentList = new ArrayList<>(); while (curIter.hasNext()) { Document current = curIter.next(); if (!includeId) { current.remove("_id"); } documentList.add(JSON.serialize(current)); } return documentList; } catch (MongoException ex) { logger.error("An error occured while retrieving the document list", ex); throw new DatasourceException("An error occured while retrieving the document list"); } }
From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java
License:Apache License
/** * Returns the document of the given document identifier. * * @param databaseName the database//from w w w. jav a 2 s . c o m * @param collectionName the collection * @param documentId the document identifier to query for * @return the document of the given identifier * @throws DatasourceException * @throws NotFoundException */ @Override public String getById(String databaseName, String collectionName, String documentId) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (!collectionExists(databaseName, collectionName)) { throw new NotFoundException("The collection doesn't exist in the datasource"); } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName); Document query = new Document("_id", new ObjectId(documentId)); if (collection.count(query) == 0) { throw new NotFoundException("The document doesn't exist in the datasource"); } Document document = collection.find(query).first(); document.remove("_id"); return JSON.serialize(document); } catch (MongoException ex) { logger.error("An error occured while retrieving the document", ex); throw new DatasourceException("An error occured while retrieving the document"); } }
From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java
License:Apache License
/** * Saves a document to the collection. If the specified database and * collection do not exist they will be created. * * @param databaseName the database/*from w w w . j a va 2 s . c o m*/ * @param collectionName the collection * @param content the JSON payload * @return the document identifier * @throws DatasourceException * @throws DeserializeException * @throws IllegalArgumentException */ @Override public String add(String databaseName, String collectionName, String content) throws DatasourceException, DeserializeException, IllegalArgumentException { try { if (!validInputForAddOrUpdate(databaseName, collectionName, "temp", content)) { throw new IllegalArgumentException(); } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName); Document document = Document.parse(content); collection.insertOne(document); return document.get("_id").toString(); } catch (IllegalArgumentException | ClassCastException | JSONParseException ex) { logger.error("The JSON payload is invalid", ex); throw new DeserializeException("The JSON payload is invalid"); } catch (MongoException ex) { logger.error("An error occured while adding the document", ex); throw new DatasourceException("An error occured while adding the document"); } }