Created
November 9, 2020 20:27
-
-
Save kommradHomer/7970f89c778e68de8877663ea578c77f to your computer and use it in GitHub Desktop.
mongodump on java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package com.ygt.deneme.gister; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
/** | |
* | |
* @author kommradHomer | |
*/ | |
public class App { | |
public static void main(String[] args) throws IOException, InterruptedException { | |
List<String> command = Arrays.asList( | |
"mongodump", | |
"--db", "db_minimumcrm", //NOT NECESSARY IF YOU DUMP ALL DBs | |
"--collection", "my-pretty-collection", // NOT NECESSARY IF YOU DUMP ALL Collections in the db | |
"--port", "28017", //NOT NECESSARY IF DEFAULT 27017 | |
"--username", "minimumcrm", //NOT NECESSARY IF NO AUTH | |
"--authenticationDatabase", "admin", //NOT NECESSARY IF NO AUTH | |
"--password", "mySECRETpassword", //NOT NECESSARY IF NO AUTH | |
"--query", "{\"customer\":\"John Doe\")}", // USED FOR ONLY DUMPING RESULTS OF A QUERY | |
"--out", "OUTPUT_PATH" // DEFAULTS TO working directory | |
); | |
ProcessBuilder pb = new ProcessBuilder(command) | |
.directory(new File("/tmp/")) //YOU CAN CHANGE THE WORKING DIRECTORY AS NECESSARY | |
; | |
System.out.println(pb.command()); | |
Process process = pb.start(); | |
List<String> results = readOutputHelper(process.getInputStream()); | |
List<String> err = readOutputHelper(process.getErrorStream()); | |
System.out.println(results); | |
System.out.println(err); | |
//WAITING FOR A RETURN FROM THE PROCESS WE STARTED | |
int exitCode = process.waitFor(); | |
System.out.println("exitCode:" + exitCode); | |
} | |
private static List<String> readOutputHelper(InputStream inputStream) throws IOException { | |
try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) { | |
return output.lines() | |
.collect(Collectors.toList()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment