Search in sources :

Example 1 with IMongodConfig

use of de.flapdoodle.embed.mongo.config.IMongodConfig in project beam by apache.

the class MongoDBGridFSIOTest method setup.

@BeforeClass
public static void setup() throws Exception {
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        port = serverSocket.getLocalPort();
    }
    LOG.info("Starting MongoDB embedded instance on {}", port);
    try {
        Files.forceDelete(new File(MONGODB_LOCATION));
    } catch (Exception e) {
    }
    new File(MONGODB_LOCATION).mkdirs();
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).configServer(false).replication(new Storage(MONGODB_LOCATION, null, 0)).net(new Net("localhost", port, Network.localhostIsIPv6())).cmdOptions(new MongoCmdOptionsBuilder().syncDelay(10).useNoPrealloc(true).useSmallFiles(true).useNoJournal(true).build()).build();
    mongodExecutable = mongodStarter.prepare(mongodConfig);
    mongodProcess = mongodExecutable.start();
    LOG.info("Insert test data");
    Mongo client = new Mongo("localhost", port);
    DB database = client.getDB(DATABASE);
    GridFS gridfs = new GridFS(database);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    for (int x = 0; x < 100; x++) {
        out.write(("Einstein\nDarwin\nCopernicus\nPasteur\n" + "Curie\nFaraday\nNewton\nBohr\nGalilei\nMaxwell\n").getBytes());
    }
    for (int x = 0; x < 5; x++) {
        gridfs.createFile(new ByteArrayInputStream(out.toByteArray()), "file" + x).save();
    }
    gridfs = new GridFS(database, "mapBucket");
    long now = System.currentTimeMillis();
    Random random = new Random();
    String[] scientists = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell" };
    for (int x = 0; x < 10; x++) {
        GridFSInputFile file = gridfs.createFile("file_" + x);
        OutputStream outf = file.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(outf);
        for (int y = 0; y < 5000; y++) {
            long time = now - random.nextInt(3600000);
            String name = scientists[y % scientists.length];
            writer.write(Long.toString(time) + "\t");
            writer.write(name + "\t");
            writer.write(Integer.toString(random.nextInt(100)));
            writer.write("\n");
        }
        for (int y = 0; y < scientists.length; y++) {
            String name = scientists[y % scientists.length];
            writer.write(Long.toString(now) + "\t");
            writer.write(name + "\t");
            writer.write("101");
            writer.write("\n");
        }
        writer.flush();
        writer.close();
    }
    client.close();
}
Also used : Mongo(com.mongodb.Mongo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GridFS(com.mongodb.gridfs.GridFS) IOException(java.io.IOException) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) Storage(de.flapdoodle.embed.mongo.config.Storage) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) OutputStreamWriter(java.io.OutputStreamWriter) Net(de.flapdoodle.embed.mongo.config.Net) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) File(java.io.File) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) DB(com.mongodb.DB) BeforeClass(org.junit.BeforeClass)

Example 2 with IMongodConfig

use of de.flapdoodle.embed.mongo.config.IMongodConfig in project beam by apache.

the class MongoDbIOTest method setup.

@Before
public void setup() throws Exception {
    LOG.info("Starting MongoDB embedded instance on {}", port);
    try {
        Files.forceDelete(new File(MONGODB_LOCATION));
    } catch (Exception e) {
    }
    new File(MONGODB_LOCATION).mkdirs();
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).configServer(false).replication(new Storage(MONGODB_LOCATION, null, 0)).net(new Net("localhost", port, Network.localhostIsIPv6())).cmdOptions(new MongoCmdOptionsBuilder().syncDelay(10).useNoPrealloc(true).useSmallFiles(true).useNoJournal(true).build()).build();
    mongodExecutable = mongodStarter.prepare(mongodConfig);
    mongodProcess = mongodExecutable.start();
    LOG.info("Insert test data");
    MongoClient client = new MongoClient("localhost", port);
    MongoDatabase database = client.getDatabase(DATABASE);
    MongoCollection collection = database.getCollection(COLLECTION);
    String[] scientists = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell" };
    for (int i = 1; i <= 1000; i++) {
        int index = i % scientists.length;
        Document document = new Document();
        document.append("_id", i);
        document.append("scientist", scientists[index]);
        collection.insertOne(document);
    }
}
Also used : Document(org.bson.Document) MongoClient(com.mongodb.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) Storage(de.flapdoodle.embed.mongo.config.Storage) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) File(java.io.File) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) MongoDatabase(com.mongodb.client.MongoDatabase) Before(org.junit.Before)

Example 3 with IMongodConfig

use of de.flapdoodle.embed.mongo.config.IMongodConfig in project gora by apache.

the class GoraMongodbTestDriver method setUpClass.

/**
   * Initiate the MongoDB server on the default port
   */
@Override
public void setUpClass() throws IOException {
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, log).processOutput(ProcessOutput.getDefaultInstanceSilent()).build();
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    int port = Network.getFreeServerPort();
    IMongodConfig mongodConfig = new MongodConfigBuilder().version(version).net(new Net(port, Network.localhostIsIPv6())).build();
    // Store Mongo server "host:port" in Hadoop configuration
    // so that MongoStore will be able to get it latter
    conf.set(MongoStoreParameters.PROP_MONGO_SERVERS, "127.0.0.1:" + port);
    log.info("Starting embedded Mongodb server on {} port.", port);
    try {
        _mongodExe = runtime.prepare(mongodConfig);
        _mongod = _mongodExe.start();
        _mongo = new MongoClient("localhost", port);
    } catch (Exception e) {
        log.error("Error starting embedded Mongodb server... tearing down test driver.");
        tearDownClass();
    }
}
Also used : MongoClient(com.mongodb.MongoClient) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) IOException(java.io.IOException) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)

Example 4 with IMongodConfig

use of de.flapdoodle.embed.mongo.config.IMongodConfig in project sling by apache.

the class SlingLaunchpadNosqlMongoIT method startMongo.

protected void startMongo(final int port) throws IOException {
    final MongodStarter starter = MongodStarter.getDefaultInstance();
    final Net net = new Net(port, Network.localhostIsIPv6());
    final IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(net).build();
    executable = starter.prepare(mongodConfig);
    process = executable.start();
}
Also used : MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder)

Example 5 with IMongodConfig

use of de.flapdoodle.embed.mongo.config.IMongodConfig in project sling by apache.

the class SlingLaunchpadOakMongoIT method startMongo.

protected void startMongo(final int port) throws IOException {
    final MongodStarter starter = MongodStarter.getDefaultInstance();
    final Net net = new Net(port, Network.localhostIsIPv6());
    final IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(net).build();
    executable = starter.prepare(mongodConfig);
    process = executable.start();
}
Also used : MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder)

Aggregations

IMongodConfig (de.flapdoodle.embed.mongo.config.IMongodConfig)5 MongodConfigBuilder (de.flapdoodle.embed.mongo.config.MongodConfigBuilder)5 Net (de.flapdoodle.embed.mongo.config.Net)5 MongodStarter (de.flapdoodle.embed.mongo.MongodStarter)3 MongoClient (com.mongodb.MongoClient)2 MongoCmdOptionsBuilder (de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder)2 Storage (de.flapdoodle.embed.mongo.config.Storage)2 File (java.io.File)2 IOException (java.io.IOException)2 DB (com.mongodb.DB)1 Mongo (com.mongodb.Mongo)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 GridFS (com.mongodb.gridfs.GridFS)1 GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)1 GridFSInputFile (com.mongodb.gridfs.GridFSInputFile)1 RuntimeConfigBuilder (de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)1 IRuntimeConfig (de.flapdoodle.embed.process.config.IRuntimeConfig)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1