Example usage for com.mongodb BasicDBObjectBuilder get

List of usage examples for com.mongodb BasicDBObjectBuilder get

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder get.

Prototype

public DBObject get() 

Source Link

Document

Gets the top level document.

Usage

From source file:co.cask.hydrator.plugin.batch.sink.MongoDBBatchSink.java

License:Apache License

@Override
public void transform(StructuredRecord input, Emitter<KeyValue<NullWritable, BSONWritable>> emitter)
        throws Exception {
    BasicDBObjectBuilder bsonBuilder = BasicDBObjectBuilder.start();
    for (Schema.Field field : input.getSchema().getFields()) {
        bsonBuilder.add(field.getName(), input.get(field.getName()));
    }/*from  www.  j a v a 2s . c  o  m*/
    emitter.emit(new KeyValue<>(NullWritable.get(), new BSONWritable(bsonBuilder.get())));
}

From source file:com.bbc.remarc.util.ResourceManager.java

License:Apache License

private static void createDocumentsFromFileMap(HashMap<String, List<File>> fileMap,
        HashMap<String, ResourceType> typeMap, File properties, String resourcesDir) {

    DB db = MongoClient.getDB();/*from   ww  w  .  ja  va2s .co  m*/

    Properties documentProps = processPropertiesFile(properties);
    if (documentProps == null) {
        log.error("could not create properties file. Abort directory.");
        return;
    }

    String theme = documentProps.getProperty("theme");
    String decade = documentProps.getProperty("decade");
    if (theme == null && decade == null) {
        log.error("ERROR! Properties file contained neither THEME nor DECADE. Abort directory.");
        return;
    }

    // now we process each key (document) in the hashmap, copying the
    // resources (file array) into the correct folder
    Set<String> keys = fileMap.keySet();
    for (String key : keys) {

        log.debug("processing [" + key + "]");

        // create document with id, theme and decade
        BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start();
        documentBuilder.add("id", key);
        documentBuilder.add("theme", theme);
        documentBuilder.add("decade", decade);

        // based upon the documentType, we can determine all our urls and
        // storage variables
        ResourceType documentType = typeMap.get(key);

        File fileDestDirectory = null;

        // Get the relative base URL from an Environment variable if it has been set
        String relativefileBaseUrl = System.getenv(Configuration.ENV_BASE_URL);
        if (relativefileBaseUrl == null || "".equals(relativefileBaseUrl)) {
            relativefileBaseUrl = Configuration.DEFAULT_RELATIVE_BASE_URL;
        } else {
            relativefileBaseUrl += Configuration.CONTENT_DIR;
        }

        String mongoCollection = "";

        switch (documentType) {
        case IMAGE:
            mongoCollection = "images";
            fileDestDirectory = new File(resourcesDir + Configuration.IMAGE_DIR_NAME);
            relativefileBaseUrl += Configuration.IMAGE_DIR;
            break;
        case AUDIO:
            mongoCollection = "audio";
            fileDestDirectory = new File(resourcesDir + Configuration.AUDIO_DIR_NAME);
            relativefileBaseUrl += Configuration.AUDIO_DIR;
            break;
        case VIDEO:
            mongoCollection = "video";
            fileDestDirectory = new File(resourcesDir + Configuration.VIDEO_DIR_NAME);
            relativefileBaseUrl += Configuration.VIDEO_DIR;
            break;
        default:
            break;
        }

        List<File> files = fileMap.get(key);
        for (File resource : files) {

            log.debug("--- processing [" + resource.getName() + "]");

            String resourceLocation = relativefileBaseUrl + resource.getName();
            String extension = FilenameUtils.getExtension(resource.getName());

            ResourceType fileType = getTypeFromExtension(extension);

            // now determine the value to store the resource under in MongoDB, different if an image or metadata
            String urlKey;
            switch (fileType) {
            case IMAGE:
                urlKey = "imageUrl";
                break;
            case INFORMATION:
                urlKey = "metadata";
                break;
            default:
                urlKey = (extension + "ContentUrl");
                break;
            }

            // If the file is a metadata file, we want to read from it, otherwise we just add the location to the db
            if (fileType == ResourceType.INFORMATION) {
                String metadata = processMetadata(resource.getPath());
                documentBuilder.add(urlKey, metadata);
            } else {
                documentBuilder.add(urlKey, resourceLocation);
            }

        }

        // insert the document into the database
        try {
            DBObject obj = documentBuilder.get();

            log.debug("writing document to collection (" + mongoCollection + "): " + obj);

            db.requestStart();
            DBCollection collection = db.getCollection(mongoCollection);
            collection.insert(documentBuilder.get());

        } finally {
            db.requestDone();
        }

        // write all the resource files to the correct directory
        log.debug("copying resources into " + fileDestDirectory.getPath());

        for (File resource : files) {

            // We don't want to copy the metadata into the directory, so remove it here
            String extension = FilenameUtils.getExtension(resource.getName());
            ResourceType fileType = getTypeFromExtension(extension);

            if (fileType != ResourceType.INFORMATION) {
                try {
                    FileUtils.copyFileToDirectory(resource, fileDestDirectory);
                } catch (IOException e) {
                    log.error("ERROR! Couldn't copy resource to directory: " + e);
                }
            }
        }

    }
}

From source file:com.continuent.tungsten.replicator.applier.MongoApplier.java

License:Open Source License

private void ensureIndexes(DBCollection coll, OneRowChange orc) {
    // If we have not seen this table before, check whether it
    // needs an index.
    if (autoIndex) {
        String schema = orc.getSchemaName();
        String table = orc.getTableName();
        Table t = tableMetadataCache.retrieve(schema, table);
        if (t == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ensuring index exists on collection: db=" + schema + " collection=" + table);
            }//from  ww  w.  j  av a 2s.co m

            // Compute required index keys and ensure they
            // exist in MongoDB.
            List<ColumnSpec> keySpecs = orc.getKeySpec();
            if (keySpecs.size() > 0) {
                BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
                for (ColumnSpec keySpec : keySpecs) {
                    builder.add(keySpec.getName(), 1);
                }
                coll.ensureIndex(builder.get());
            }

            // Note that we have processed the table.
            t = new Table(schema, table);
            tableMetadataCache.store(t);
        }
    }
}

From source file:com.conventus.mongodb.converter.ContentConverter.java

public DBObject toDBObject(Content content) {

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().append("name", content.getName())
            .append("source", content.getSource()).append("file", content.getFile());

    if (content.getId() != null)
        builder = builder.append("_id", new ObjectId(content.getId()));

    return builder.get();
}

From source file:com.conventus.mongodb.converter.FilmConverter.java

public DBObject toDBObject(Film film) {

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().append("name", film.getName())
            .append("source", film.getSource()).append("file", film.getFile()).append("title", film.getTitle())
            .append("genre", film.getGenre()).append("size", film.getSize())
            .append("quality", film.getQuality()).append("resolution", film.getResolution())
            .append("frameRate", film.getFrameRate()).append("language", film.getLanguage())
            .append("duration", film.getDuration()).append("imdbRating", film.getImdbRating())
            .append("mpr", film.getMpr()).append("peersSeeds", film.getPeersSeeds());

    if (film.getId() != null)
        builder = builder.append("_id", new ObjectId(film.getId()));

    return builder.get();
}

From source file:com.databank.mongodb.converter.UserConverter.java

public static DBObject toDBObject(User u) {

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().append("firstname", u.getFirstname())
            .append("lastname", u.getLastname()).append("email", u.getEmail())
            .append("username", u.getUsername()).append("location", u.getLocation())
            .append("password1", u.getPassword1());
    if (u.getID() != null) {
        builder = builder.append("_id", new ObjectId(u.getID()));
    }/*  w ww .  ja va 2 s .c  o  m*/
    return builder.get();
}

From source file:com.ejbmongoembeddedtomcat.converter.CustomerConverter.java

public static DBObject toDBObject(Customer cus) {

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().append("name", cus.getName())
            .append("address", cus.getAddress()).append("kota", cus.getKota());

    if (cus.getId() != null) {
        builder = builder.append("_id", new ObjectId(cus.getId()));
    }//from ww  w . ja v a 2 s  .com

    return builder.get();
}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

public void saveFile(Message<JsonObject> message, JsonObject jsonObject) {

    ObjectId id = getObjectId(message, jsonObject, "id");
    if (id == null) {
        return;//from  ww  w .j av  a  2 s.  c o m
    }

    Integer length = getRequiredInt("length", message, jsonObject, 1);
    if (length == null) {
        return;
    }

    Integer chunkSize = getRequiredInt("chunkSize", message, jsonObject, 1);
    if (chunkSize == null) {
        return;
    }

    long uploadDate = jsonObject.getLong("uploadDate", 0);
    if (uploadDate <= 0) {
        uploadDate = System.currentTimeMillis();
    }

    String filename = jsonObject.getString("filename");
    String contentType = jsonObject.getString("contentType");
    JsonObject metadata = jsonObject.getObject("metadata");

    try {
        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("_id", id).add("length", length)
                .add("chunkSize", chunkSize).add("uploadDate", new Date(uploadDate));

        if (filename != null)
            builder.add("filename", filename);
        if (contentType != null)
            builder.add("contentType", contentType);
        if (metadata != null)
            builder.add("metadata", JSON.parse(metadata.encode()));

        DBObject dbObject = builder.get();

        String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET);
        DBCollection collection = db.getCollection(bucket + ".files");

        // Ensure standard indexes as long as collection is small
        if (collection.count() < 1000) {
            collection.ensureIndex(BasicDBObjectBuilder.start().add("filename", 1).add("uploadDate", 1).get());
        }

        collection.save(dbObject);
        sendOK(message);

    } catch (Exception e) {
        sendError(message, "Error saving file", e);
    }
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> simpleAggregation() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.add("_id", "$manufacturer");
    builder.push("num_products");
    builder.add("$sum", 1);
    builder.pop();/*from   w  w w.  j a v a 2s  .  co  m*/
    builder.pop();

    return col.aggregate(builder.get()).results().iterator();
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> compoundAggregation() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.push("_id");
    builder.add("manufacturer", "$manufacturer");
    builder.add("category", "$category");
    builder.pop();//from w w  w. ja  v a2  s  . co  m
    builder.push("num_products");
    builder.add("$sum", 1);
    builder.pop();
    builder.pop();

    return col.aggregate(builder.get()).results().iterator();
}