Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private Set<String> getTableMetadataNames(String schemaName) throws TableNotFoundException {
    MongoDatabase db = client.getDatabase(schemaName);
    MongoCursor<Document> cursor = db.getCollection(schemaCollection).find()
            .projection(new Document(TABLE_NAME_KEY, true)).iterator();

    HashSet<String> names = new HashSet<>();
    while (cursor.hasNext()) {
        names.add((cursor.next()).getString(TABLE_NAME_KEY));
    }//from  w  ww.j  av a2  s  . c o  m

    return names;
}

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns)
        throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    MongoDatabase db = client.getDatabase(schemaName);
    Document metadata = new Document(TABLE_NAME_KEY, tableName);

    ArrayList<Document> fields = new ArrayList<>();
    if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) {
        fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument());
    }//from w  w  w  .j  a  v a2 s .  c om

    fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));

    metadata.append(FIELDS_KEY, fields);

    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
    schema.insertOne(metadata);
}

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private boolean deleteTableMetadata(SchemaTableName schemaTableName) {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    MongoDatabase db = client.getDatabase(schemaName);
    if (!collectionExists(db, tableName)) {
        return false;
    }//  w w  w .ja v a  2 s  . com

    DeleteResult result = db.getCollection(schemaCollection).deleteOne(new Document(TABLE_NAME_KEY, tableName));

    return result.getDeletedCount() == 1;
}

From source file:com.facebook.presto.mongodb.MongoSession.java

License:Apache License

private List<Document> guessTableFields(SchemaTableName schemaTableName) {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    MongoDatabase db = client.getDatabase(schemaName);
    Document doc = db.getCollection(tableName).find().first();
    if (doc == null) {
        // no records at the collection
        return ImmutableList.of();
    }//from   ww  w . ja va  2s  .  c  om

    ImmutableList.Builder<Document> builder = ImmutableList.builder();

    for (String key : doc.keySet()) {
        Object value = doc.get(key);
        Optional<TypeSignature> fieldType = guessFieldType(value);
        if (fieldType.isPresent()) {
            Document metadata = new Document();
            metadata.append(FIELDS_NAME_KEY, key);
            metadata.append(FIELDS_TYPE_KEY, fieldType.get().toString());
            metadata.append(FIELDS_HIDDEN_KEY,
                    key.equals("_id") && fieldType.get().equals(OBJECT_ID.getTypeSignature()));

            builder.add(metadata);
        } else {
            log.debug("Unable to guess field type from %s : %s",
                    value == null ? "null" : value.getClass().getName(), value);
        }
    }

    return builder.build();
}

From source file:com.focusit.log4jmongo.appender.SimpleMongoDbAppender.java

License:Apache License

protected void initialize() {
    final List<ServerAddress> addresses = getServerAddresses(hostname, port);
    mongo = getMongo(addresses);// w w  w .  ja  va 2s.  c o  m
    mongo.setWriteConcern(WriteConcern.valueOf(getWriteConcern()));

    final MongoDatabase database = getDatabase(mongo, databaseName);

    if (userName != null && userName.trim().length() > 0) {
        // Allow password to be GCed
        password = null;
    }

    setCollection(database.getCollection(collectionName));
    initialized = true;
}

From source file:com.futuremove.cacheServer.service.impl.CarDynPropsServiceImpl.java

@Override
public MongoCollection<Document> getCollection() {
    MongoDatabase database = mongoClient.getDatabase("cacheServer");
    MongoCollection<Document> collection = database.getCollection("carProps");
    return collection;
}

From source file:com.github.cherimojava.data.mongo.io.EntityCodec.java

License:Apache License

/**
 * Creates a MongoCollection which has a EntityCodec attached to it
 *
 * @param db/*from   w  w  w  .j a  v  a 2  s .  c  om*/
 * @param properties
 * @return
 */
public static MongoCollection<? extends Entity> getCollectionFor(MongoDatabase db,
        EntityProperties properties) {
    return db.getCollection(properties.getCollectionName()).withDocumentClass(properties.getEntityClass())
            .withCodecRegistry(EntityCodecProvider.createCodecRegistry(db, properties.getEntityClass()));
}

From source file:com.gs.obevo.mongodb.impl.MongoDbChangeAuditDao.java

License:Apache License

@Override
public void init() {
    for (PhysicalSchema physicalSchema : env.getPhysicalSchemas()) {
        MongoDatabase database = mongoClient.getDatabase(physicalSchema.getPhysicalName());
        try {//from  ww w .j  a  v  a  2  s  . co  m
            database.createCollection(getAuditContainerName());
        } catch (Exception e) {
            // create if it doesn't exist already; TODO clean this up
        }
        MongoCollection<Document> collection = database.getCollection(getAuditContainerName());
        collection.createIndex(Indexes.ascending(changeNameColumn, "OBJECTNAME"));
    }
}

From source file:com.gs.obevo.mongodb.impl.MongoDbChangeAuditDao.java

License:Apache License

@Override
public ImmutableList<Change> getDeployedChanges() {
    return env.getPhysicalSchemas().flatCollect(new Function<PhysicalSchema, Iterable<Change>>() {
        @Override/*from w  w w. ja  v a  2  s .com*/
        public Iterable<Change> valueOf(PhysicalSchema physicalSchema) {
            MongoDatabase database = mongoClient.getDatabase(physicalSchema.getPhysicalName());
            MongoCollection<Document> auditCollection = database.getCollection(getAuditContainerName());

            MutableList<Change> changes = iterableToCollection(auditCollection.find())
                    .collect(new Function<Document, Change>() {
                        @Override
                        public Change valueOf(Document doc) {
                            String artfType = doc.getString("ARTFTYPE");
                            Change artf;
                            if (artfType.equals("I")) {
                                artf = new ChangeIncremental();
                            } else if (artfType.equals("R")) {
                                artf = new ChangeRerunnable();
                            } else {
                                throw new IllegalArgumentException("This type does not exist " + artfType);
                            }

                            artf.setChangeKey(
                                    new ChangeKey(InternMap.instance().intern(doc.getString("DBSCHEMA")),
                                            platform.getChangeType(doc.getString("CHANGETYPE")),
                                            InternMap.instance().intern(doc.getString("OBJECTNAME")),
                                            doc.getString(changeNameColumn)));

                            artf.setActive(doc.getInteger("ACTIVE") == 1);
                            artf.setContentHash(doc.getString("CONTENTHASH"));
                            artf.setTimeInserted(new Timestamp(doc.getDate(timeInsertedColumn).getTime()));
                            artf.setTimeUpdated(new Timestamp(doc.getDate(timeUpdatedColumn).getTime()));

                            artf.setRollbackContent(doc.getString(rollbackContentColumn));

                            return artf;
                        }
                    });

            return changes.toImmutable();
        }
    }).toList().toImmutable();
}

From source file:com.gs.obevo.mongodb.impl.MongoDbChangeAuditDao.java

License:Apache License

private MongoCollection<Document> getAuditCollection(Change change) {
    MongoDatabase database = mongoClient.getDatabase(change.getPhysicalSchema(env).getPhysicalName());
    return database.getCollection(getAuditContainerName());
}