Search in sources :

Example 31 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project graylog2-server by Graylog2.

the class V20170110150100_FixAlertConditionsMigrationTest method setUp.

@Before
public void setUp() throws Exception {
    this.clusterConfigService = spy(new ClusterConfigServiceImpl(objectMapperProvider, fongoRule.getConnection(), nodeId, new ChainingClassLoader(getClass().getClassLoader()), new ClusterEventBus()));
    final MongoConnection mongoConnection = spy(fongoRule.getConnection());
    final MongoDatabase mongoDatabase = spy(fongoRule.getDatabase());
    when(mongoConnection.getMongoDatabase()).thenReturn(mongoDatabase);
    this.collection = spy(mongoDatabase.getCollection("streams"));
    when(mongoDatabase.getCollection("streams")).thenReturn(collection);
    this.migration = new V20170110150100_FixAlertConditionsMigration(mongoConnection, clusterConfigService);
}
Also used : ClusterConfigServiceImpl(org.graylog2.cluster.ClusterConfigServiceImpl) MongoConnection(org.graylog2.database.MongoConnection) ChainingClassLoader(org.graylog2.shared.plugins.ChainingClassLoader) ClusterEventBus(org.graylog2.events.ClusterEventBus) MongoDatabase(com.mongodb.client.MongoDatabase) Before(org.junit.Before)

Example 32 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project yyl_example by Relucent.

the class MongoDbJDbcTest method main.

public static void main(String[] args) {
    try (MongoClient client = new MongoClient("localhost", 27017)) {
        MongoDatabase database = client.getDatabase("my-test-db");
        //获取集合
        MongoCollection<Document> collection = database.getCollection("test-collection");
        //删除集合
        collection.drop();
        //插入文档
        for (int i = 0; i < 3; i++) {
            Document document = new Document();
            document.put("_id", i);
            document.put("name", "测试" + i);
            collection.insertOne(document);
        }
        //更新文档
        {
            Document document = new Document();
            document.put("name", "测试零");
            collection.updateMany(Filters.eq("_id", 0), new Document("$set", document));
        }
        System.out.println(collection.count());
        //删除文档
        collection.deleteOne(Filters.eq("_id", 2));
        //检索文档
        FindIterable<Document> findIterable = collection.find();
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            System.out.println(mongoCursor.next());
        }
        System.out.println(collection.count());
        //删除库
        database.drop();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}
Also used : MongoClient(com.mongodb.MongoClient) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 33 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project drill by apache.

the class MongoPersistentStoreProvider method start.

@Override
public void start() throws IOException {
    MongoClientURI clientURI = new MongoClientURI(mongoURL);
    client = new MongoClient(clientURI);
    MongoDatabase db = client.getDatabase(clientURI.getDatabase());
    collection = db.getCollection(clientURI.getCollection()).withWriteConcern(WriteConcern.JOURNALED);
    Bson index = Indexes.ascending(pKey);
    collection.createIndex(index);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) MongoDatabase(com.mongodb.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 34 with MongoDatabase

use of com.mongodb.client.MongoDatabase in project drill by apache.

the class MongoGroupScan method getScanStats.

@Override
public ScanStats getScanStats() {
    try {
        MongoClient client = storagePlugin.getClient();
        MongoDatabase db = client.getDatabase(scanSpec.getDbName());
        MongoCollection<Document> collection = db.getCollection(scanSpec.getCollectionName());
        long numDocs = collection.count();
        float approxDiskCost = 0;
        if (numDocs != 0) {
            //toJson should use client's codec, otherwise toJson could fail on
            // some types not known to DocumentCodec, e.g. DBRef.
            final DocumentCodec codec = new DocumentCodec(client.getMongoClientOptions().getCodecRegistry(), new BsonTypeClassMap());
            String json = collection.find().first().toJson(codec);
            approxDiskCost = json.getBytes().length * numDocs;
        }
        return new ScanStats(GroupScanProperty.EXACT_ROW_COUNT, numDocs, 1, approxDiskCost);
    } catch (Exception e) {
        throw new DrillRuntimeException(e.getMessage(), e);
    }
}
Also used : MongoClient(com.mongodb.MongoClient) DocumentCodec(org.bson.codecs.DocumentCodec) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Document(org.bson.Document) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) PhysicalOperatorSetupException(org.apache.drill.exec.physical.PhysicalOperatorSetupException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) ExecutionSetupException(org.apache.drill.common.exceptions.ExecutionSetupException) IOException(java.io.IOException) MongoDatabase(com.mongodb.client.MongoDatabase) ScanStats(org.apache.drill.exec.physical.base.ScanStats)

Aggregations

MongoDatabase (com.mongodb.client.MongoDatabase)34 Document (org.bson.Document)25 MongoClient (com.mongodb.MongoClient)14 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 MongoClientURI (com.mongodb.MongoClientURI)5 Bson (org.bson.conversions.Bson)4 ServerAddress (com.mongodb.ServerAddress)3 MongoCollection (com.mongodb.client.MongoCollection)3 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)3 IndexOptions (com.mongodb.client.model.IndexOptions)3 DeleteResult (com.mongodb.client.result.DeleteResult)3 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)2 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)2 TypeSignature (com.facebook.presto.spi.type.TypeSignature)2 ImmutableList (com.google.common.collect.ImmutableList)2 MongoCommandException (com.mongodb.MongoCommandException)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 Date (java.util.Date)2 Configuration (com.alibaba.datax.common.util.Configuration)1