Skip to content

Instantly share code, notes, and snippets.

@btiernay
Last active March 29, 2018 08:57
Show Gist options
  • Save btiernay/5271940 to your computer and use it in GitHub Desktop.
Save btiernay/5271940 to your computer and use it in GitHub Desktop.
Example of how one can issue a `splitVector` command in MongoDB 2.4.0 using the Java client when `--auth` is enabled and the requesting user exists in the `admin` db.
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class Main {
public static void main(String... args) throws UnknownHostException {
// URI with db and collection
String uri = args[0];
MongoClientURI mongoClientUri = new MongoClientURI(uri);
Mongo mongo = new MongoClient(mongoClientUri);
// Authenicate: This is required to happen against the *same* mongo object
DB admin = mongo.getDB("admin");
admin.authenticate(mongoClientUri.getUsername(), mongoClientUri.getPassword());
DB db = mongo.getDB(mongoClientUri.getDatabase());
String collectionName = db.getCollection(mongoClientUri.getCollection()).getFullName();
DBObject command = BasicDBObjectBuilder //
.start("splitVector", collectionName) //
.add("keyPattern", new BasicDBObject("_id", 1)) //
.add("maxChunkSize", 8) //
.get();
System.out.println("Sending: " + command);
CommandResult result = db.command(command);
System.out.println("Received: " + result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment