Example usage for com.mongodb.client FindIterable projection

List of usage examples for com.mongodb.client FindIterable projection

Introduction

In this page you can find the example usage for com.mongodb.client FindIterable projection.

Prototype

FindIterable<TResult> projection(@Nullable Bson projection);

Source Link

Document

Sets a document describing the fields to return for all matching documents.

Usage

From source file:at.rocworks.oa4j.logger.dbs.NoSQLMongoDB.java

@Override
public boolean dpGetPeriod(Date t1, Date t2, Dp dp, Set<String> configs, DpGetPeriodResult result) {
    // db.events.find({tag: "System1:Test_1_1.Value", ts: {$gt: ISODate("2016-07-28T09:00:00.000Z"), $lt: ISODate("2016-07-28T10:00:00.000Z")}}, {_id:0, tag:1, ts:1});
    JDebug.out.log(Level.INFO, "dpGetPeriod {0}-{1} dp={2} configs={3}",
            new Object[] { t1, t2, dp, configs.toString() });

    final SimpleDateFormat fmt = new SimpleDateFormat(TimeVar.FMT_DATE_JS_MS);
    fmt.setTimeZone(TimeZone.getTimeZone("UTC"));

    // columns/*ww w . ja v  a  2s . co m*/
    final ArrayList<Dp> dps = createDpConfigAttrList(dp, configs);
    if (dps.isEmpty()) {
        JDebug.out.warning("dpGetPeriod without any valid config.");
        return false;
    }
    Document columns = new Document();
    columns.append("_id", 0);
    columns.append("ts", 1);
    dps.forEach((Dp x) -> {
        String c = attrMap.get(x.getAttribute());
        if (c != null)
            columns.append(c, 1);
    });

    // filter
    Document query = new Document();
    query.append("tag", getTagOfDp(dp));
    query.append("ts", new Document("$gte", t1).append("$lte", t2));

    // query
    FindIterable<Document> find = evcoll.find(query);
    find.projection(columns);
    find.forEach((Block<Document>) document -> {
        // { "ts" : { "$date" : 1469696660635 }, "value" : { "number" : 3.0 }, "status" : { "$numberLong" : "-9007199254738370303" }, "user" : 0 }
        //JDebug.out.info(document.toJson());
        Date ts = document.getDate("ts");
        Object value;
        for (int i = 0; i < dps.size(); i++) {
            try {
                final Dp attr = dps.get(i);
                switch (attr.getAttribute()) {
                case Value:
                    // value_number
                    value = document.get("value");
                    if (value instanceof Document) {
                        Document dval = (Document) value;
                        dval.keySet().forEach(type -> result.addValue(attr, ts, dval.get(type)));
                    }
                    break;
                case Status:
                    value = document.get("status");
                    result.addVariable(attr, ts, new Bit32Var(value));
                    break;

                case Status64:
                    value = document.get("status");
                    result.addVariable(attr, ts, new Bit64Var(value));
                    break;
                case Manager:
                    value = document.get("manager");
                    result.addVariable(attr, ts, Variable.newVariable(value));
                    break;
                case User:
                    value = document.get("user");
                    result.addVariable(attr, ts, Variable.newVariable(value));
                    break;
                case Stime:
                    value = ts;
                    result.addVariable(attr, ts, Variable.newVariable(value));
                    break;
                default:
                    JDebug.out.log(Level.SEVERE, "unhandeled config {0}", attr.getAttribute());
                }
            } catch (Exception ex) {
                JDebug.StackTrace(Level.SEVERE, ex);
            }
        }
    });

    return true;
}

From source file:com.bluedragon.mongo.MongoCollectionFind.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData query = getNamedParam(argStruct, "query", null);
    if (query == null)
        throwException(_session, "please specify query");

    int size = getNamedIntParam(argStruct, "size", -1);
    int skip = getNamedIntParam(argStruct, "skip", -1);
    cfData sort = getNamedParam(argStruct, "sort", null);
    cfData fields = getNamedParam(argStruct, "fields", null);

    try {/*from ww  w .j a  va 2  s.co  m*/
        MongoCollection<Document> col = db.getCollection(collection);

        // Get the initial cursor
        FindIterable<Document> cursor;
        long start = System.currentTimeMillis();
        Document qry = getDocument(query);

        cursor = col.find(qry);

        if (fields != null)
            cursor = cursor.projection(getDocument(fields));

        // Are we sorting?
        if (sort != null)
            cursor = cursor.sort(getDocument(sort));

        // Are we limiting
        if (skip != -1)
            cursor = cursor.skip(skip);

        // How many we bringing back
        if (size != -1)
            cursor = cursor.limit(size);

        // Now we can run the query
        cfArrayData results = cfArrayData.createArray(1);

        cursor.forEach(new Block<Document>() {

            @SuppressWarnings("rawtypes")
            @Override
            public void apply(final Document st) {
                try {
                    results.addElement(tagUtils.convertToCfData((Map) st));
                } catch (cfmRunTimeException e) {
                }
            }
        });

        _session.getDebugRecorder().execMongo(col, "find", qry, System.currentTimeMillis() - start);

        return results;
    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.bluedragon.mongo.MongoCollectionFindOne.java

License:Open Source License

@SuppressWarnings("rawtypes")
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData query = getNamedParam(argStruct, "query", null);
    if (query == null)
        throwException(_session, "please specify query to find");

    cfData fields = getNamedParam(argStruct, "fields", null);

    try {// w ww . jav  a  2s  .c o m
        MongoCollection<Document> col = db.getCollection(collection);

        long start = System.currentTimeMillis();
        Document qry = getDocument(query);

        FindIterable<Document> cursor = col.find(qry).limit(1);

        if (fields != null)
            cursor = cursor.projection(getDocument(fields));

        _session.getDebugRecorder().execMongo(col, "findone", qry, System.currentTimeMillis() - start);
        return tagUtils.convertToCfData((Map) cursor.first());

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public static FindIterable<Document> buildProjection(FindIterable<Document> documents, String includes,
        String excludes) {/* www  .  j av a  2s  . co m*/
    if (excludes != null) {
        List<String> excludesList = Arrays.asList(excludes.split(","));
        documents = documents.projection(fields(exclude(excludesList), excludeId()));
    } else if (includes != null) {
        List<String> includesList = Arrays.asList(includes.split(","));
        documents = documents.projection(fields(include(includesList), excludeId()));
    } else {
        documents = documents.projection(excludeId());
    }

    return documents;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java

License:Open Source License

private FindIterable<Document> buildProjection(FindIterable<Document> documents, String includes,
        String excludes) {/* w ww  .j a va2s.c o m*/
    if (excludes != null) {
        List<String> excludesList = Arrays.asList(excludes.split(","));
        documents = documents.projection(fields(exclude(excludesList), excludeId()));
    } else if (includes != null) {
        List<String> includesList = Arrays.asList(includes.split(","));
        documents = documents.projection(fields(include(includesList), excludeId()));
    } else {
        documents = documents.projection(excludeId());
    }

    return documents;
}

From source file:com.redhat.thermostat.gateway.service.jvms.mongo.JvmInfoMongoStorageHandler.java

License:Open Source License

public String getJvmsTree(MongoCollection<Document> collection, boolean aliveOnly, String excludes,
        String includes, int limit, int offset) {
    FindIterable<Document> documents;

    if (aliveOnly) {
        documents = collection.find(lt(StorageFields.STOP_TIME, 0));
    } else {//  w ww . j a  va 2 s  . c  o m
        documents = collection.find();
    }

    documents = documents.limit(limit).skip(offset);

    boolean includeSystemId = true;
    if (excludes != null) {
        List<String> excludesList = new ArrayList<>(Arrays.asList(excludes.split(",")));
        if (excludesList.contains(StorageFields.SYSTEM_ID)) {
            excludesList.remove(StorageFields.SYSTEM_ID);
            includeSystemId = false;
        }
        if (excludesList.size() > 0) {
            documents = documents.projection(fields(exclude(excludesList), excludeId()));
        } else {
            documents = documents.projection(excludeId());
        }
    } else if (includes != null) {
        List<String> includesList = new ArrayList<>(Arrays.asList(includes.split(",")));
        if (!includesList.contains(StorageFields.SYSTEM_ID)) {
            includesList.add(StorageFields.SYSTEM_ID);
            includeSystemId = false;
        }
        documents = documents.projection(fields(include(includesList), excludeId()));
    } else {
        documents = documents.projection(excludeId());
    }

    final Map<String, StringBuilder> map = new HashMap<>();

    final boolean finalIncludeSystemId = includeSystemId;
    documents.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {
            String systemId = document.getString(StorageFields.SYSTEM_ID);
            if (!finalIncludeSystemId) {
                document.remove(StorageFields.SYSTEM_ID);
            }

            if (!map.containsKey(systemId)) {
                map.put(systemId, new StringBuilder().append("{\"" + StorageFields.SYSTEM_ID + "\":\""
                        + systemId + "\", \"" + StorageFields.JVMS + "\":["));
            }

            map.get(systemId).append(document.toJson()).append(",");
        }
    });

    StringBuilder responseBuilder = new StringBuilder().append("{ \"" + StorageFields.RESPONSE + "\" : [");
    if (map.size() > 0) {
        for (StringBuilder systemBuilder : map.values()) {
            responseBuilder.append(systemBuilder.deleteCharAt(systemBuilder.length() - 1).toString());
            responseBuilder.append("]},");
        }
        responseBuilder.deleteCharAt(responseBuilder.length() - 1);
    }
    responseBuilder.append("]}");

    return responseBuilder.toString();
}

From source file:com.yahoo.ycsb.db3.MongoDbClient.java

License:Open Source License

/**
 * Read a record from the database. Each field/value pair from the result will
 * be stored in a HashMap.//  w  w  w .  j a v a 2 s . co  m
 * 
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to read.
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A HashMap of field/value pairs for the result
 * @return Zero on success, a non-zero error code on error or "not found".
 */
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    try {
        MongoCollection<Document> collection = database.getCollection(table);
        Document query = new Document("_id", key);

        FindIterable<Document> findIterable = collection.find(query);

        if (fields != null) {
            Document projection = new Document();
            for (String field : fields) {
                projection.put(field, INCLUDE);
            }
            findIterable.projection(projection);
        }

        Document queryResult = findIterable.first();

        if (queryResult != null) {
            fillMap(result, queryResult);
        }
        return queryResult != null ? Status.OK : Status.NOT_FOUND;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    }
}

From source file:com.yahoo.ycsb.db3.MongoDbClient.java

License:Open Source License

/**
 * Perform a range scan for a set of records in the database. Each field/value
 * pair from the result will be stored in a HashMap.
 * /*from w  ww  .j  a  va2 s.c  om*/
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value
 *          pairs for one record
 * @return Zero on success, a non-zero error code on error. See the {@link DB}
 *         class's description for a discussion of error codes.
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result) {
    MongoCursor<Document> cursor = null;
    try {
        MongoCollection<Document> collection = database.getCollection(table);

        Document scanRange = new Document("$gte", startkey);
        Document query = new Document("_id", scanRange);
        Document sort = new Document("_id", INCLUDE);

        FindIterable<Document> findIterable = collection.find(query).sort(sort).limit(recordcount);

        if (fields != null) {
            Document projection = new Document();
            for (String fieldName : fields) {
                projection.put(fieldName, INCLUDE);
            }
            findIterable.projection(projection);
        }

        cursor = findIterable.iterator();

        if (!cursor.hasNext()) {
            System.err.println("Nothing found in scan for key " + startkey);
            return Status.ERROR;
        }

        result.ensureCapacity(recordcount);

        while (cursor.hasNext()) {
            HashMap<String, ByteIterator> resultMap = new HashMap<String, ByteIterator>();

            Document obj = cursor.next();
            fillMap(resultMap, obj);

            result.add(resultMap);
        }

        return Status.OK;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:io.lumeer.storage.mongodb.MongoDbStorage.java

License:Open Source License

@Override
public List<DataDocument> search(String collectionName, DataFilter filter, final DataSort sort,
        List<String> attributes, final int skip, int limit) {
    MongoCollection<Document> collection = database.getCollection(collectionName);
    FindIterable<Document> documents = filter != null ? collection.find(filter.<Bson>get()) : collection.find();
    if (sort != null) {
        documents = documents.sort(sort.<Bson>get());
    }//from  ww w.  j a v  a2 s .c o  m
    if (attributes != null && !attributes.isEmpty()) {
        documents.projection(Projections.fields(Projections.include(attributes)));
    }
    if (skip > 0) {
        documents = documents.skip(skip);
    }
    if (limit > 0) {
        documents = documents.limit(limit);
    }

    return MongoUtils.convertIterableToList(documents);
}

From source file:org.apache.nifi.processors.mongodb.GetMongo.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();

    final Document query = context.getProperty(QUERY).isSet()
            ? Document.parse(context.getProperty(QUERY).evaluateAttributeExpressions().getValue())
            : null;// w  ww .  j ava2  s. c o m
    final Document projection = context.getProperty(PROJECTION).isSet()
            ? Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions().getValue())
            : null;
    final Document sort = context.getProperty(SORT).isSet()
            ? Document.parse(context.getProperty(SORT).evaluateAttributeExpressions().getValue())
            : null;
    final String jsonTypeSetting = context.getProperty(JSON_TYPE).getValue();
    configureMapper(jsonTypeSetting);

    final MongoCollection<Document> collection = getCollection(context);

    try {
        final FindIterable<Document> it = query != null ? collection.find(query) : collection.find();
        if (projection != null) {
            it.projection(projection);
        }
        if (sort != null) {
            it.sort(sort);
        }
        if (context.getProperty(LIMIT).isSet()) {
            it.limit(context.getProperty(LIMIT).evaluateAttributeExpressions().asInteger());
        }
        if (context.getProperty(BATCH_SIZE).isSet()) {
            it.batchSize(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger());
        }

        final MongoCursor<Document> cursor = it.iterator();
        ComponentLog log = getLogger();
        try {
            FlowFile flowFile = null;
            if (context.getProperty(RESULTS_PER_FLOWFILE).isSet()) {
                int ceiling = context.getProperty(RESULTS_PER_FLOWFILE).evaluateAttributeExpressions()
                        .asInteger();
                List<Document> batch = new ArrayList<>();

                while (cursor.hasNext()) {
                    batch.add(cursor.next());
                    if (batch.size() == ceiling) {
                        try {
                            if (log.isDebugEnabled()) {
                                log.debug("Writing batch...");
                            }
                            String payload = buildBatch(batch, jsonTypeSetting);
                            writeBatch(payload, context, session);
                            batch = new ArrayList<>();
                        } catch (IOException ex) {
                            getLogger().error("Error building batch", ex);
                        }
                    }
                }
                if (batch.size() > 0) {
                    try {
                        writeBatch(buildBatch(batch, jsonTypeSetting), context, session);
                    } catch (IOException ex) {
                        getLogger().error("Error sending remainder of batch", ex);
                    }
                }
            } else {
                while (cursor.hasNext()) {
                    flowFile = session.create();
                    flowFile = session.write(flowFile, new OutputStreamCallback() {
                        @Override
                        public void process(OutputStream out) throws IOException {
                            String json;
                            if (jsonTypeSetting.equals(JSON_TYPE_STANDARD)) {
                                json = mapper.writerWithDefaultPrettyPrinter()
                                        .writeValueAsString(cursor.next());
                            } else {
                                json = cursor.next().toJson();
                            }
                            IOUtils.write(json, out);
                        }
                    });
                    flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(),
                            "application/json");

                    session.getProvenanceReporter().receive(flowFile, getURI(context));
                    session.transfer(flowFile, REL_SUCCESS);
                }
            }

            session.commit();

        } finally {
            cursor.close();
        }

    } catch (final RuntimeException e) {
        context.yield();
        session.rollback();
        logger.error("Failed to execute query {} due to {}", new Object[] { query, e }, e);
    }
}