package cn.allen.hadoop.mongo;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.bson.Document;
import org.junit.Test;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
/**
* @ClassName: MongoDBShardsTest
* @company: bsoft
* @Author: Feel君
* @CreateDate: January 23, 2018
*/
public class MongoDBShardsTest {
/**
* get connection
*
* @return
*/
private static MongoDatabase getMongoDBConn(String databaseName) {
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
ServerAddress address1 = new ServerAddress("192.168.204.11", 20000);
ServerAddress address2 = new ServerAddress("192.168.204.12", 20000);
ServerAddress address3 = new ServerAddress("192.168.204.13", 20000);
addresses.add(address1);
addresses.add(address2);
addresses.add(address3);
/*
* //MongoCredential.createScramSha1Credential() The three parameters are username database name password
* MongoCredential credential = MongoCredential.createScramSha1Credential("system", "testdb", "123123".toCharArray()); List<MongoCredential> credentials = new
* ArrayList<MongoCredential>(); credentials.add(credential);
* //Get MongoDB connection through connection authentication
* MongoClient mongoClient = new MongoClient(addresses,credentials);
* //Connect to the database MongoDatabase
* mongoDatabase = mongoClient.getDatabase("databaseName");
*/
@SuppressWarnings("resource")
MongoClient mongoClient = new MongoClient(addresses);
MongoDatabase db = mongoClient.getDatabase(databaseName);
System.out.println("Connect to database successfully");
return db;
}
/**
* Create collection
*/
private static void createColl(String table) {
MongoDatabase mongoDatabase = getMongoDBConn("testdb");
mongoDatabase.createCollection(table);
}
/**
* get collection
* @return
*/
private static MongoCollection<Document> getColl(String dbName,String table) {
MongoDatabase mongoDatabase = getMongoDBConn(dbName);
MongoCollection<Document> collection = mongoDatabase.getCollection(table);
if (collection == null) {
createColl(table);
collection = mongoDatabase.getCollection(table);
}
System.out.println("Collection selection succeeded");
return collection;
}
/**
* insert document
*/
@Test
private static void addData(String dbName,String table) {
MongoCollection<Document> collection = getColl(dbName,table);
List<Document> documents = new ArrayList<Document>();
for (int i = 0; i < 100000; i++) {
String uuid = UUID.randomUUID().toString().replace("-", "");
Document document = new Document("id", uuid).
append("test1", "test1"+uuid).
append("test2", "test2"+uuid).
append("test3", "test3"+uuid);
documents.add(document);
}
// collection.insertOne(document); //insert a single document
collection.insertMany(documents);
System.out.println("Document inserted successfully");
}
/**
* Retrieve all documents
*/
@Test
private static void getCollData(String dbName,String table) {
MongoCollection<Document> collection = getColl(dbName,table);
// retrieve all documents
/**
* 1. Get the iterator FindIterable<Document>
* 2. Get the cursor MongoCursor<Document>
* 3. Traverse the retrieved document collection through the cursor
* */
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
int num = 0;
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
if (num > 100000) {
System.exit(0);
}
num++;
}
}
/**
* Update documentation
*/
@Test
private static void updataData(String dbName,String table) {
MongoCollection<Document> collection = getColl(dbName,table);
//Update the document Change the document with likes=100 in the document to likes=200
collection.updateMany(Filters.eq("id", 16475), new Document("$set",new Document("test1","给你个白眼")));
//Update one and view the content before the update
// Document findOneAndUpdate = collection.findOneAndUpdate(Filters.eq("id", 16476), new Document("$set",new Document("test1","safffgf")));
// System.out.println(findOneAndUpdate.get("id").toString());
// System.out.println(findOneAndUpdate.get("test1").toString());
//Retrieve view result
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
int num = 0;
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
if (num > 16475) {
System.exit(0);
}
num++;
}
}
/**
* Update documentation
*/
@Test
private static void delCollData(String dbName,String table) {
MongoCollection<Document> collection = getColl(dbName,table);
// delete the first document that matches the condition
// collection.deleteOne(Filters.eq("test1", ""));
// delete all eligible documents
// collection.deleteMany (Filters.eq("test1", "testval1"));
// delete all
collection.drop();
}
public static void main(String[] args) {
// getColl("testdb","table2");
addData("testdb2","tablehash");
// getCollData("testdb","table1");
// updataData("testdb","table2");
// delCollData("testdb","table2");
}
}