Skip to content
Snippets Groups Projects
Commit f209ff0e authored by Pablo's avatar Pablo
Browse files

MongoImport support

parent dbf89319
No related branches found
No related tags found
No related merge requests found
...@@ -17,3 +17,7 @@ target ...@@ -17,3 +17,7 @@ target
# Misc # Misc
*.log *.log
# IntelliJ
.idea/
*.iml
...@@ -63,6 +63,46 @@ Usage ...@@ -63,6 +63,46 @@ Usage
<!-- optional, skips this plugin entirely, use on the command line like -Dembedmongo.skip --> <!-- optional, skips this plugin entirely, use on the command line like -Dembedmongo.skip -->
</configuration> </configuration>
</execution> </execution>
<execution>
<id>import</id>
<goals>
<goal>import</goal>
</goals>
<configuration>
<defaultImportDatabase>test</defaultImportDatabase>
<!-- optional, name of the default database to import data -->
<parallel>false</parallel>
<!-- optional, default false, if true it launches in parallel all imports -->
<wait>false</wait>
<!-- optional, default false, if true it will wait forever after it imports the data -->
<imports>
<import>
<database>my_db</database>
<!-- optional, name of the database, if null it will fallback to defaultImportDatabase -->
<collection>col</collection>
<!-- required, name of the collection to import data -->
<file>import_file.json</file>
<!-- required, name of the json file to import -->
<upsertOnImport>true</upsertOnImport>
<!-- optional, default true, if true it will do an upsert on each document imported -->
<dropOnImport>false</dropOnImport>
<!-- optional, default true, if true it will do a drop the collection before starts to import -->
<timeout>20000</timeout>
<!-- optional, default 20000, it will fail if it takes more than this time importing a file (time in millis) -->
</import>
<!-- More imports are accepted and it will be executed in strictly order (if parallel is not set) -->
</imports>
</configuration>
</execution>
<execution> <execution>
<id>stop</id> <id>stop</id>
<goals> <goals>
...@@ -82,6 +122,6 @@ Notes ...@@ -82,6 +122,6 @@ Notes
If you're using Jenkins, you can also try the [Port Allocator Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Port+Allocator+Plugin). If you're using Jenkins, you can also try the [Port Allocator Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Port+Allocator+Plugin).
* If you need to use a proxy to download MongoDB then you can either use `-Dhttp.proxyHost` and `-Dhttp.proxyPort` as additional Maven arguments (this will affect the entire build) or instruct the plugin to use a proxy when downloading Mongo by adding the `proxyHost` and `proxyPort` configuration properties. * If you need to use a proxy to download MongoDB then you can either use `-Dhttp.proxyHost` and `-Dhttp.proxyPort` as additional Maven arguments (this will affect the entire build) or instruct the plugin to use a proxy when downloading Mongo by adding the `proxyHost` and `proxyPort` configuration properties.
* If you're having trouble with Windows firewall rules, try setting the _bindIp_ config property to `127.0.0.1`. * If you're having trouble with Windows firewall rules, try setting the _bindIp_ config property to `127.0.0.1`.
* If you'd like the start goal to start mongodb and wait, you can add `-Dembedmongo.wait` to your Maven command line arguments * If you'd like the start goal to start mongodb and wait, you can add `-Dembedmongo.wait` to your Maven command line arguments or -Dembedmongo.import.wait if you want the imports
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/92ea4148abefddaeadb849b65212bd0d "githalytics.com")](http://githalytics.com/joelittlejohn/embedmongo-maven-plugin) [![githalytics.com alpha](https://cruel-carlota.pagodabox.com/92ea4148abefddaeadb849b65212bd0d "githalytics.com")](http://githalytics.com/joelittlejohn/embedmongo-maven-plugin)
/**
* Copyright © 2012 Pablo Diaz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.joelittlejohn.embedmongo;
public class ImportDataConfig {
private String database;
private String collection;
private String file;
private Boolean dropOnImport = true;
private Boolean upsertOnImport = true;
private long timeout = 200000;
public ImportDataConfig() {
}
public ImportDataConfig(String database, String collection, String file, Boolean dropOnImport, Boolean upsertOnImport, long timeout) {
this.database = database;
this.collection = collection;
this.file = file;
this.dropOnImport = dropOnImport;
this.upsertOnImport = upsertOnImport;
this.timeout = timeout;
}
public String getDatabase() {
return database;
}
public String getCollection() {
return collection;
}
public String getFile() {
return file;
}
public Boolean getDropOnImport() {
return dropOnImport;
}
public Boolean getUpsertOnImport() {
return upsertOnImport;
}
public long getTimeout() {
return timeout;
}
@Override
public String toString() {
return "ImportDataConfig{" +
"database='" + database + '\'' +
", collection='" + collection + '\'' +
", file='" + file + '\'' +
", dropOnImport=" + dropOnImport +
", upsertOnImport=" + upsertOnImport +
", timeout=" + timeout +
'}';
}
}
/**
* Copyright © 2012 Pablo Diaz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.joelittlejohn.embedmongo;
import de.flapdoodle.embed.mongo.MongoImportExecutable;
import de.flapdoodle.embed.mongo.MongoImportProcess;
import de.flapdoodle.embed.mongo.MongoImportStarter;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.config.*;
import de.flapdoodle.embed.process.runtime.Network;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @goal import
* @phase pre-integration-test
*/
public class ImportEmbeddedMongoMojo extends AbstractMojo {
/**
* @parameter
*/
private ImportDataConfig[] imports;
/**
* @parameter expression="${embedmongo.defaultImportDatabase}"
*/
private String defaultImportDatabase;
/**
* @parameter expression="${embedmongo.import.wait}" default-value="false"
*/
private Boolean wait;
/**
* @parameter expression="${embedmongo.parallel}" default-value="false"
*/
private Boolean parallel;
/**
* @parameter expression="${embedmongo.skip}" default-value="false"
*/
private boolean skip;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
MongodProcess mongod = (MongodProcess)getPluginContext().get(StartEmbeddedMongoMojo.MONGOD_CONTEXT_PROPERTY_NAME);
if(mongod == null) {
throw new MojoExecutionException("Can't import without an EmbeddedMongoDB running");
}
try {
sendImportScript(mongod);
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
private void sendImportScript(MongodProcess mongod) throws IOException, InterruptedException, MojoExecutionException {
List<MongoImportProcess> pendingMongoProcess = new ArrayList<MongoImportProcess>();
if(imports == null || imports.length == 0) {
getLog().error("No imports found, check your configuration");
return;
}
IMongodConfig config = mongod.getConfig();
getLog().info("Default import database: " + defaultImportDatabase);
for(ImportDataConfig importData: imports) {
getLog().info("Import " + importData);
verify(importData);
String database = importData.getDatabase();
if(StringUtils.isBlank(database)){
database = defaultImportDatabase;
}
IMongoImportConfig mongoImportConfig = new MongoImportConfigBuilder()
.version(config.version())
.net(new Net(config.net().getPort(), Network.localhostIsIPv6()))
.db(database)
.collection(importData.getCollection())
.upsert(importData.getUpsertOnImport())
.dropCollection(importData.getDropOnImport())
.importFile(importData.getFile())
.jsonArray(true)
.timeout(new Timeout(importData.getTimeout()))
.build();
MongoImportExecutable mongoImport = MongoImportStarter.getDefaultInstance().prepare(mongoImportConfig);
MongoImportProcess importProcess = mongoImport.start();
if(parallel){
pendingMongoProcess.add(importProcess);
}else{
waitFor(importProcess);
}
}
for(MongoImportProcess importProcess: pendingMongoProcess){
waitFor(importProcess);
}
if(wait) {
getLog().info("STARTED - MongoDB up and all imports done.");
mongod.waitFor();
}
}
private void waitFor(MongoImportProcess importProcess) throws InterruptedException, MojoExecutionException {
int code = importProcess.waitFor();
if(code != 0){
throw new MojoExecutionException("Cannot import '" + importProcess.getConfig().getImportFile() + "'");
}
getLog().info("Import return code: " + code);
}
private void verify(ImportDataConfig config) {
Validate.notBlank(config.getFile(), "Import file is required\n\n" +
"<imports>\n" +
"\t<import>\n" +
"\t\t<file>[my file]</file>\n" +
"...");
Validate.isTrue(StringUtils.isNotBlank(defaultImportDatabase) || StringUtils.isNotBlank(config.getDatabase()), "Database is required you can either define a defaultImportDatabase or a <database> on import tags");
Validate.notBlank(config.getCollection(), "Collection is required\n\n" +
"<imports>\n" +
"\t<import>\n" +
"\t\t<collection>[my file]</collection>\n" +
"...");
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin-test-parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin-test4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<url>https://github.com/joelittlejohn/embedmongo-maven-plugin</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin</artifactId>
<executions>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<version>v2.2.0</version>
</configuration>
</execution>
<execution>
<id>import</id>
<goals>
<goal>import</goal>
</goals>
<configuration>
<defaultImportDatabase>test</defaultImportDatabase>
<parallel>false</parallel>
<imports>
<import>
<database>app</database>
<collection>some_zips</collection>
<file>src/main/test/mongo_data/tiny_zips.json</file>
<upsertOnImport>true</upsertOnImport>
<dropOnImport>false</dropOnImport>
</import>
<import>
<database>intensive_test</database>
<collection>zips</collection>
<file>src/main/test/mongo_data/import_zips.json</file>
<dropOnImport>true</dropOnImport>
<timeout>120000</timeout>
</import>
<import>
<collection>test_zips</collection>
<file>src/main/test/mongo_data/tiny_zips.json</file>
</import>
</imports>
</configuration>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
{}
\ No newline at end of file
This diff is collapsed.
{ "_id" : "01001", "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA" }
{ "_id" : "01002", "city" : "CUSHMAN", "loc" : [ -72.51564999999999, 42.377017 ], "pop" : 36963, "state" : "MA" }
{ "_id" : "01005", "city" : "BARRE", "loc" : [ -72.10835400000001, 42.409698 ], "pop" : 4546, "state" : "MA" }
{ "_id" : "01007", "city" : "BELCHERTOWN", "loc" : [ -72.41095300000001, 42.275103 ], "pop" : 10579, "state" : "MA" }
{ "_id" : "01008", "city" : "BLANDFORD", "loc" : [ -72.936114, 42.182949 ], "pop" : 1240, "state" : "MA" }
{ "_id" : "01010", "city" : "BRIMFIELD", "loc" : [ -72.188455, 42.116543 ], "pop" : 3706, "state" : "MA" }
{ "_id" : "01011", "city" : "CHESTER", "loc" : [ -72.988761, 42.279421 ], "pop" : 1688, "state" : "MA" }
{ "_id" : "01012", "city" : "CHESTERFIELD", "loc" : [ -72.833309, 42.38167 ], "pop" : 177, "state" : "MA" }
{ "_id" : "01013", "city" : "CHICOPEE", "loc" : [ -72.607962, 42.162046 ], "pop" : 23396, "state" : "MA" }
{ "_id" : "01020", "city" : "CHICOPEE", "loc" : [ -72.576142, 42.176443 ], "pop" : 31495, "state" : "MA" }
{ "_id" : "01022", "city" : "WESTOVER AFB", "loc" : [ -72.558657, 42.196672 ], "pop" : 1764, "state" : "MA" }
{ "_id" : "01026", "city" : "CUMMINGTON", "loc" : [ -72.905767, 42.435296 ], "pop" : 1484, "state" : "MA" }
{ "_id" : "01027", "city" : "MOUNT TOM", "loc" : [ -72.67992099999999, 42.264319 ], "pop" : 16864, "state" : "MA" }
{ "_id" : "01028", "city" : "EAST LONGMEADOW", "loc" : [ -72.505565, 42.067203 ], "pop" : 13367, "state" : "MA" }
{ "_id" : "01030", "city" : "FEEDING HILLS", "loc" : [ -72.675077, 42.07182 ], "pop" : 11985, "state" : "MA" }
{ "_id" : "01031", "city" : "GILBERTVILLE", "loc" : [ -72.19858499999999, 42.332194 ], "pop" : 2385, "state" : "MA" }
{ "_id" : "01032", "city" : "GOSHEN", "loc" : [ -72.844092, 42.466234 ], "pop" : 122, "state" : "MA" }
{ "_id" : "01033", "city" : "GRANBY", "loc" : [ -72.52000099999999, 42.255704 ], "pop" : 5526, "state" : "MA" }
{ "_id" : "01034", "city" : "TOLLAND", "loc" : [ -72.908793, 42.070234 ], "pop" : 1652, "state" : "MA" }
{ "_id" : "01035", "city" : "HADLEY", "loc" : [ -72.571499, 42.36062 ], "pop" : 4231, "state" : "MA" }
{ "_id" : "01036", "city" : "HAMPDEN", "loc" : [ -72.43182299999999, 42.064756 ], "pop" : 4709, "state" : "MA" }
{ "_id" : "01038", "city" : "HATFIELD", "loc" : [ -72.61673500000001, 42.38439 ], "pop" : 3184, "state" : "MA" }
{ "_id" : "01039", "city" : "HAYDENVILLE", "loc" : [ -72.70317799999999, 42.381799 ], "pop" : 1387, "state" : "MA" }
{ "_id" : "01040", "city" : "HOLYOKE", "loc" : [ -72.626193, 42.202007 ], "pop" : 43704, "state" : "MA" }
{ "_id" : "01050", "city" : "HUNTINGTON", "loc" : [ -72.873341, 42.265301 ], "pop" : 2084, "state" : "MA" }
{ "_id" : "01053", "city" : "LEEDS", "loc" : [ -72.70340299999999, 42.354292 ], "pop" : 1350, "state" : "MA" }
{ "_id" : "01054", "city" : "LEVERETT", "loc" : [ -72.499334, 42.46823 ], "pop" : 1748, "state" : "MA" }
{ "_id" : "01056", "city" : "LUDLOW", "loc" : [ -72.471012, 42.172823 ], "pop" : 18820, "state" : "MA" }
{ "_id" : "01057", "city" : "MONSON", "loc" : [ -72.31963399999999, 42.101017 ], "pop" : 8194, "state" : "MA" }
{ "_id" : "01060", "city" : "FLORENCE", "loc" : [ -72.654245, 42.324662 ], "pop" : 27939, "state" : "MA" }
{ "_id" : "01068", "city" : "OAKHAM", "loc" : [ -72.051265, 42.348033 ], "pop" : 1503, "state" : "MA" }
{ "_id" : "01069", "city" : "PALMER", "loc" : [ -72.328785, 42.176233 ], "pop" : 9778, "state" : "MA" }
{ "_id" : "01070", "city" : "PLAINFIELD", "loc" : [ -72.918289, 42.514393 ], "pop" : 571, "state" : "MA" }
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
<module>example1</module> <module>example1</module>
<module>example2</module> <module>example2</module>
<module>example3</module> <module>example3</module>
<module>example4</module>
<module>randomport</module> <module>randomport</module>
</modules> </modules>
...@@ -23,7 +24,7 @@ ...@@ -23,7 +24,7 @@
<plugin> <plugin>
<groupId>com.github.joelittlejohn.embedmongo</groupId> <groupId>com.github.joelittlejohn.embedmongo</groupId>
<artifactId>embedmongo-maven-plugin</artifactId> <artifactId>embedmongo-maven-plugin</artifactId>
<version>0.1.11-SNAPSHOT</version> <version>0.1.13-SNAPSHOT</version>
</plugin> </plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment