List of usage examples for com.mongodb BasicDBObject append
@Override public BasicDBObject append(final String key, final Object val)
From source file:Entity.Rubrique.java
public BasicDBList mapBddCritere() { BasicDBList result = new BasicDBList(); for (Critere critere : lstCritere) { BasicDBObject dbRubriques = new BasicDBObject(); //dbRubriques.append("_id", critere.getId()); dbRubriques.append("libelle", critere.getLibelle()); dbRubriques.append("poid", critere.getPoid()); result.add(dbRubriques);//from w w w.j a v a2 s .co m } return result; }
From source file:epoxide.lpa.impl.mongodb.ClassConverter.java
License:Apache License
public BasicDBObject encode(Object record) { BasicDBObject dest = new BasicDBObject(); for (FieldInfo info : infos) { Object val = fa.get(record, info.num); if (info.isBytes) { if (val != null) val = new Binary((byte[]) val); } else if (!info.isNative) { val = new Binary(Serializer.INSTANCE.serialize(val)); }//from ww w . ja v a 2s.c om dest.append(info.key, val); } return dest; }
From source file:epoxide.lpa.impl.mongodb.MongoRecordSet.java
License:Apache License
@Override public IterableIterator<T> getEntities(Query query) { BasicDBObject nq = new BasicDBObject(); for (Entry<String, Object> e : query.getHaving().entrySet()) nq.append(conv.translateFieldName(e.getKey()), Natives.encode(e.getValue())); DBCursor cursor = coll.find(nq);/*from w ww. j av a 2 s . c o m*/ return new MongoIterator<T>(cls, conv, cursor); }
From source file:es.eucm.gleaner.realtime.functions.AddTrialNum.java
License:Apache License
@Override public void execute(TridentTuple objects, TridentCollector tridentCollector) { // Extract values from the tuple Object playerId = objects.getValueByField("gameplayId"); Object taskId = objects.getValueByField("target"); // Generate mongoDB query using the playerId and taskId BasicDBObject mongoDoc = new BasicDBObject(); mongoDoc.append("playerId", playerId); mongoDoc.append("taskId", taskId); // Query the database in order to find the trial long count = traces.count(mongoDoc); if (count == 0) { // The doc did not previously exist so it will be created mongoDoc.append("trial", count + 1); traces.insert(mongoDoc);/*from ww w .jav a2 s .c om*/ } else { // The doc already existed so update the trial number by 1 BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("trial", 1)); traces.update(new BasicDBObject().append("playerId", playerId).append("taskId", taskId), newDocument); } // Query and emit the the trial DBCursor curDoc = traces.find(new BasicDBObject().append("playerId", playerId).append("taskId", taskId)); long trial = (long) curDoc.next().get("trial"); tridentCollector.emit(new Values(trial)); }
From source file:es.eucm.gleaner.realtime.functions.DescriptivesGenerator.java
License:Apache License
@Override public void execute(TridentTuple objects, TridentCollector tridentCollector) { // Extract values for searching the DB from the tuple Object taskId = objects.getValueByField("target"); Object trial = objects.getValueByField("trial"); // Extract value for updating the statistics from the tuple Double score = Double.parseDouble((String) objects.getValueByField("value")); /*** Step 1: get the initial performance statistics (from mongoDB or use starting values) ***/ // Generate mongoDB query using target and trial BasicDBObject mongoDoc = new BasicDBObject(); mongoDoc.append("taskId", taskId); mongoDoc.append("trial", trial); // Prepare statistics variables Double max; // Optional (not required for the calculation of the other statistics) Double min; // Optional (not required for the calculation of the other statistics) Double sum; // Optional (not required for the calculation of the other statistics) Double variance; // Variable needed for the calculation of stdDev Double mean; // Mean (target outcome) Double stdDev; // Standard deviation (target outcome) Double skewness; // The deviation of a gaussian distribution's mean from the median (target outcome) Double kurtosis; // The flatness of a gaussian distribution (target outcome) long n; // The number of samples that were used in this distribution (number of playthroughs) Boolean normal; // Is the normality assumption respected? (target outcome) Double help1; // Variable needed for skew and kurt calculation Double help2; // Variable needed for skew and kurt calculation Double help3; // Variable needed for skew and kurt calculation // Query the database in order to find the current statistics status n = performanceStatistics.count(mongoDoc); // Prepare the variables based on the statistics status if (n == 0) { // There were no previous completions of this task-trial combination so we start with a blank slate max = 0D;//from w ww . j av a 2s . co m min = 0D; sum = 0D; variance = 0D; mean = 0D; stdDev = 0D; skewness = 0D; kurtosis = 0D; n = 1; normal = false; help1 = 0D; help2 = 0D; help3 = 0D; } else { // There were previous completions of this task-trial combination so we update the previous results DBCursor currentStatistics = performanceStatistics.find(mongoDoc); max = (double) currentStatistics.next().get("max"); // Uses next to get to the first field min = (double) currentStatistics.curr().get("min"); // Uses curr to moves through all fields after the first sum = (double) currentStatistics.curr().get("sum"); variance = (double) currentStatistics.curr().get("variance"); mean = (double) currentStatistics.curr().get("mean"); stdDev = (double) currentStatistics.curr().get("stdDev"); skewness = (double) currentStatistics.curr().get("skewness"); kurtosis = (double) currentStatistics.curr().get("kurtosis"); n = 1 + (long) currentStatistics.curr().get("n"); normal = (boolean) currentStatistics.curr().get("normal"); help1 = (double) currentStatistics.curr().get("help1"); help2 = (double) currentStatistics.curr().get("help2"); help3 = (double) currentStatistics.curr().get("help3"); } // For testing: output results // System.out.print("taskId: "); System.out.print(taskId);System.out.print(", "); // System.out.print("trial: "); System.out.print(trial);System.out.print(", "); // System.out.print("max: "); System.out.print(max);System.out.print(", "); // System.out.print("min: "); System.out.print(min);System.out.print(", "); // System.out.print("sum: "); System.out.print(sum);System.out.print(", "); // System.out.print("var: "); System.out.print(variance);System.out.print(", "); // System.out.print("mea: "); System.out.print(mean);System.out.print(", "); // System.out.print("std: "); System.out.print(stdDev);System.out.print(", "); // System.out.print("ske: "); System.out.print(skewness);System.out.print(", "); // System.out.print("kur: "); System.out.print(kurtosis);System.out.print(", "); // System.out.print("n : "); System.out.print(n);System.out.print(", "); // System.out.print("nor: "); System.out.print(normal);System.out.print(", "); // System.out.print("hp1: "); System.out.print(help1);System.out.print(", "); // System.out.print("hp2: "); System.out.print(help2);System.out.print(", "); // System.out.print("hp3: "); System.out.print(help3);System.out.println("."); /*** Step 2: calculate the up-to-date statistics ***/ if (n == 1) { max = score; min = score; sum = score; } else { // New max if (score > max) max = score; //New min if (score < min) min = score; // New sum sum += score; } // New mean, variance, & stdDev >> based on: http://www.johndcook.com/blog/standard_deviation/ Double oldMean = mean; Double newMean; Double oldS = variance; Double newS; if (n == 1) { newMean = score; newS = 0D; } else { //New means formula suitable for 1-pass statistics (= big data ready) newMean = oldMean + (score - oldMean) / n; newS = oldS + (score - oldMean) * (score - newMean); } mean = newMean; variance = (n > 1) ? (newS / (n - 1)) : 0D; stdDev = Math.sqrt(variance); // New skewness & kurtosis >> based on: http://www.johndcook.com/blog/skewness_kurtosis/ double delta, delta_n, delta_n2, term1; delta = score - newMean; delta_n = delta / n; delta_n2 = delta_n * delta_n; term1 = delta * delta_n * n; help3 = (term1 * delta_n2 * (n * n - 3 * n + 3)) + (6 * delta_n2 * help1) - (4 * delta_n * help2); help2 = (term1 * delta_n * (n - 2)) - (3 * n * delta_n * help1); help1 = term1; skewness = (Math.sqrt((double) n) * help2 / Math.pow(help1, 1.5)); kurtosis = (((double) n) * help3 / (help1 * help1) - 3.0); // For testing: output results // System.out.print("taskId: "); System.out.print(taskId);System.out.print(", "); // System.out.print("trial: "); System.out.print(trial);System.out.print(", "); // System.out.print("max: "); System.out.print(max);System.out.print(", "); // System.out.print("min: "); System.out.print(min);System.out.print(", "); // System.out.print("sum: "); System.out.print(sum);System.out.print(", "); // System.out.print("var: "); System.out.print(variance);System.out.print(", "); // System.out.print("mea: "); System.out.print(mean);System.out.print(", "); // System.out.print("std: "); System.out.print(stdDev);System.out.print(", "); // System.out.print("ske: "); System.out.print(skewness);System.out.print(", "); // System.out.print("kur: "); System.out.print(kurtosis);System.out.print(", "); // System.out.print("n : "); System.out.print(n);System.out.print(", "); // System.out.print("nor: "); System.out.print(normal);System.out.print(", "); // System.out.print("hp1: "); System.out.print(help1);System.out.print(", "); // System.out.print("hp2: "); System.out.print(help2);System.out.print(", "); // System.out.print("hp3: "); System.out.print(help3);System.out.println("."); // Step 3: update the mongoDB performanceStatistics collection // Prepare the new document BasicDBObject newMongoDoc = new BasicDBObject(); newMongoDoc.append("taskId", taskId).append("trial", trial).append("max", max).append("min", min) .append("sum", sum).append("variance", variance).append("mean", mean).append("stdDev", stdDev) .append("skewness", skewness).append("kurtosis", kurtosis).append("n", n).append("normal", normal) .append("help1", help1).append("help2", help2).append("help3", help3); // For EVEN MORE testing // System.out.println(newDescriptives); // Insert the new document (create or update depending on whether it is a new task-trial combination if (n == 1) { // The doc did not previously exist so it will be created performanceStatistics.insert(newMongoDoc); } else { // The doc already existed so update the previous document performanceStatistics.update(new BasicDBObject().append("taskId", taskId).append("trial", trial), newMongoDoc); } // Step 4: emit the new tuple // Prepare the tuple ArrayList<Object> object = new ArrayList(); object.add(max); object.add(max); object.add(min); object.add(sum); object.add(variance); object.add(mean); object.add(stdDev); object.add(skewness); object.add(kurtosis); object.add(n); object.add(normal); object.add(help1); object.add(help2); object.add(help3); // For testing // System.out.print("The new tuple is: "); // System.out.println(object); // Emit the tuple tridentCollector.emit(object); }
From source file:eu.cassandra.server.mongo.util.MongoDBQueries.java
License:Apache License
/** * curl -i --header "dbname:run_id" 'http://localhost:8080/cassandra/api/results?inst_id=instID_&aggr_unit=3&metric=1&from=3&to=100' * //w w w . j a v a2s. c o m * @param installationId * @param metric * @param aggregationUnit * @param fromTick * @param toTick * @return */ public DBObject mongoResultQuery(HttpHeaders httpHeaders, String installationId, String metricS, String aggregationUnitS, String fromTickS, String toTickS) { try { String runId = getDbNameFromHTTPHeader(httpHeaders); if (runId == null && installationId == null) throw new RestQueryParamMissingException( "QueryParamMissing: Both run_id and installation_id are null"); String aggrUnit = " (Minute)"; String defaultAggrUnit = " (Minute)"; Integer aggregationUnit = null; Integer defaultAggregationUnit = null; if (aggregationUnitS != null) { aggregationUnit = Integer.parseInt(aggregationUnitS); aggrUnit = " " + aggregationUnit + " Minute" + (aggregationUnit == 1 ? "" : "s") + ")"; } int numberOfDays = Integer.parseInt( DBConn.getConn(runId).getCollection("sim_param").findOne().get("numberOfDays").toString()); if (numberOfDays == 1) { defaultAggregationUnit = 5; defaultAggrUnit = " (5 Minutes)"; } else if (numberOfDays <= 5) { defaultAggregationUnit = 15; defaultAggrUnit = " (15 Minutes)"; } else if (numberOfDays <= 20) { defaultAggregationUnit = 60; defaultAggrUnit = " (1 Hour)"; } else if (numberOfDays <= 60) { defaultAggregationUnit = 180; defaultAggrUnit = " (3 Hours)"; } else if (numberOfDays <= 360) { defaultAggregationUnit = 720; defaultAggrUnit = " (12 Hours)"; } if (aggregationUnit == null) { aggregationUnit = defaultAggregationUnit; aggrUnit = defaultAggrUnit; } Integer fromTick = null; if (fromTickS != null) fromTick = Integer.parseInt(fromTickS); Integer toTick = null; if (toTickS != null) toTick = Integer.parseInt(toTickS); String coll = MongoResults.COL_AGGRRESULTS; if (aggregationUnit == null || aggregationUnit <= 0) aggregationUnit = 1; if (installationId != null) coll = MongoResults.COL_INSTRESULTS; String yMetric = ACTIVE_POWER_P; if (metricS != null && metricS.equalsIgnoreCase(REACTIVE_POWER_Q)) yMetric = REACTIVE_POWER_Q; //db.inst_results.find({inst_id:"dszfs123",tick:{$gt:1}}).sort({tick:1}).pretty() //db.inst_results.group( // { // keyf:function(doc) // {var key=new NumberInt(doc.tick/4); return {x:key} // } , // cond:{inst_id:"instID_"}, // reduce:function(obj,prev) // {prev.csum+=obj.p}, // initial:{csum:0} // } //) BasicDBObject condition = null; if (installationId != null || fromTick != null || toTick != null) condition = new BasicDBObject(); if (installationId != null) condition.append("inst_id", installationId); if (fromTick != null && toTick != null) condition.append("tick", BasicDBObjectBuilder.start("$gte", fromTick).add("$lte", toTick).get()); else if (fromTick != null) condition.append("tick", new BasicDBObject("$gte", fromTick)); else if (toTick != null) condition.append("tick", new BasicDBObject("$lte", toTick)); BasicDBObject groupCmd = new BasicDBObject("ns", coll); groupCmd.append("$keyf", "function(doc){var key=new NumberInt(doc.tick/" + aggregationUnit + "); return {x:key}}"); if (condition != null) groupCmd.append("cond", condition); groupCmd.append("$reduce", "function(obj,prev){prev.y+=obj." + yMetric + "}"); groupCmd.append("initial", new BasicDBObject("y", 0)); @SuppressWarnings("deprecation") BasicDBList dbList = (BasicDBList) DBConn.getConn(getDbNameFromHTTPHeader(httpHeaders)) .getCollection(coll).group(groupCmd); if (aggregationUnit > 1) { for (int i = 0; i < dbList.size(); i++) { BasicDBObject obj = (BasicDBObject) dbList.get(i); obj.put("y", Double.parseDouble(obj.get("y").toString()) / aggregationUnit); } } return jSON2Rrn.createJSONPlot(dbList, "Data for plot retrieved successfully", "Consumption " + (yMetric.equalsIgnoreCase(REACTIVE_POWER_Q) ? "Reactive Power" : "Active Power"), "Time" + aggrUnit, yMetric.equalsIgnoreCase(REACTIVE_POWER_Q) ? "VAr" : "W", defaultAggregationUnit, numberOfDays); } catch (Exception e) { e.printStackTrace(); return jSON2Rrn.createJSONError("Error in retrieving results", e.getMessage()); } }
From source file:eu.eubrazilcc.lvl.storage.dao.LeishmaniaDAO.java
License:EUPL
private BasicDBObject parseFilter(final String parameter, final String expression, final BasicDBObject query) throws InvalidFilterParseException { BasicDBObject query2 = query; if (isNotBlank(parameter) && isNotBlank(expression)) { String field = null;/* ww w .ja v a 2 s . c o m*/ // keyword matching search if ("source".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "dataSource"; } else if ("definition".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "definition"; } else if ("accession".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "accession"; } else if ("length".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "length"; } else if ("gene".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "gene"; } else if ("organism".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "organism"; } else if ("country".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "countryFeature"; } else if ("locale".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "locale"; } if (isNotBlank(field)) { if ("accession".equalsIgnoreCase(parameter)) { // convert the expression to upper case and compare for exact matching query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, expression.toUpperCase()); } else if ("locale".equalsIgnoreCase(parameter)) { // regular expression to match the language part of the locale final Pattern regex = compile("(" + expression.toLowerCase() + ")([_]{1}[A-Z]{2}){0,1}"); query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, regex); } else if ("length".equalsIgnoreCase(parameter)) { // comparison operator query2 = mongoNumeriComparison(field, expression); } else { // regular expression to match all entries that contains the keyword final Pattern regex = compile(".*" + expression + ".*", CASE_INSENSITIVE); query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, regex); } } else { // full-text search if ("text".equalsIgnoreCase(parameter)) { field = "$text"; } if (isNotBlank(field)) { if (query2 != null) { final BasicDBObject textSearch = (BasicDBObject) query2.get("$text"); final BasicDBObject search = new BasicDBObject("$search", textSearch != null ? textSearch.getString("$search") + " " + expression : expression); query2 = query2.append("$text", search.append("$language", "english")); } else { final BasicDBObject search = new BasicDBObject("$search", expression); query2 = new BasicDBObject().append("$text", search.append("$language", "english")); } } else { throw new InvalidFilterParseException(parameter); } } } return query2; }
From source file:eu.eubrazilcc.lvl.storage.dao.SandflyDAO.java
License:EUPL
private BasicDBObject parseFilter(final String parameter, final String expression, final BasicDBObject query) throws InvalidFilterParseException { BasicDBObject query2 = query; if (isNotBlank(parameter) && isNotBlank(expression)) { String field = null;// w ww .j av a 2 s. c om // keyword matching search if ("source".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "dataSource"; } else if ("definition".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "definition"; } else if ("accession".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "accession"; } else if ("length".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "length"; } else if ("gene".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "gene"; } else if ("organism".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "organism"; } else if ("country".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "countryFeature"; } else if ("locale".equalsIgnoreCase(parameter)) { field = DB_PREFIX + "locale"; } if (isNotBlank(field)) { if ("accession".equalsIgnoreCase(parameter)) { // convert the expression to upper case and compare for exact matching query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, expression.toUpperCase()); // TODO } else if ("locale".equalsIgnoreCase(parameter)) { // regular expression to match the language part of the locale final Pattern regex = compile("(" + expression.toLowerCase() + ")([_]{1}[A-Z]{2}){0,1}"); query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, regex); // TODO } else if ("length".equalsIgnoreCase(parameter)) { // comparison operator query2 = mongoNumeriComparison(field, expression); // TODO } else { // regular expression to match all entries that contains the keyword final Pattern regex = compile(".*" + expression + ".*", CASE_INSENSITIVE); query2 = (query2 != null ? query2 : new BasicDBObject()).append(field, regex); // TODO } } else { // full-text search if ("text".equalsIgnoreCase(parameter)) { field = "$text"; } if (isNotBlank(field)) { if (query2 != null) { final BasicDBObject textSearch = (BasicDBObject) query2.get("$text"); final BasicDBObject search = new BasicDBObject("$search", textSearch != null ? textSearch.getString("$search") + " " + expression : expression); query2 = query2.append("$text", search.append("$language", "english")); } else { final BasicDBObject search = new BasicDBObject("$search", expression); query2 = new BasicDBObject().append("$text", search.append("$language", "english")); } } else { throw new InvalidFilterParseException(parameter); } } } return query2; }
From source file:eu.eubrazilcc.lvl.storage.mongodb.MongoDBHelper.java
License:EUPL
public static BasicDBObject toProjection(final @Nullable ImmutableMap<String, Boolean> projection) { BasicDBObject obj = null; if (projection != null && !projection.isEmpty()) { obj = new BasicDBObject(); final ImmutableSet<Entry<String, Boolean>> entrySet = projection.entrySet(); final boolean action = entrySet.iterator().next().getValue(); for (final Entry<String, Boolean> entry : entrySet) { String field = null;// w ww. ja v a 2 s .co m checkState(action == entry.getValue(), "A projection cannot contain both include and exclude specifications"); checkState(isNotBlank(field = trimToNull(entry.getKey())), "Uninitialized field"); obj.append(field, action ? 1 : 0); } } return obj; }
From source file:eu.vital.vitalcep.restApp.alert.Alerts.java
/** * Gets the filters.//from w w w . j a v a 2 s . c om * * @return the filters */ @GET @Path("getalerts") @Produces(MediaType.APPLICATION_JSON) public String getAlerts() { MongoClient mongo = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase db = mongo.getDatabase(mongoDB); BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject().append("_id", false); fields.append("dolceSpecification", false); FindIterable<Document> coll = db.getCollection("alerts").find(query).projection(fields); final JSONArray AllJson = new JSONArray(); coll.forEach(new Block<Document>() { @Override public void apply(final Document document) { AllJson.put(document); } }); if (db != null) db = null; if (mongo != null) { mongo.close(); mongo = null; } return AllJson.toString(); }