/ClusterMonitorRepro.java Secret
Created
September 10, 2015 22:32
Java clusterMonitor repro
This file contains 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
import com.mongodb.*; | |
import com.mongodb.util.JSON; | |
import com.mongodb.util.JSONSerializers; | |
import org.bson.BSON; | |
import org.bson.BSONObject; | |
import org.bson.types.ObjectId; | |
import org.json.JSONObject; | |
import org.junit.Test; | |
import java.io.*; | |
import java.net.Authenticator; | |
import java.net.Socket; | |
import java.util.*; | |
public class ClusterMonitorRepro { | |
public static void main(String[] args) throws Exception { | |
final MongoCredential credential = MongoCredential.createScramSha1Credential("usernameWithClusterMonitorRole", "admin", "password".toCharArray()); | |
final MongoClient client = new MongoClient(new ServerAddress("localhost", 6000), Collections.singletonList(credential)); | |
try { | |
final DBCollection collection = client.getDB("admin").getCollection("system.profile"); | |
try (final DBCursor cursor = collection.find() | |
.sort(new BasicDBObject("$natural", -1)) | |
.limit(20) | |
.setReadPreference(ReadPreference.primaryPreferred())) { | |
while(cursor.hasNext()) { | |
cursor.next(); | |
} | |
} | |
} finally { | |
client.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment