Using Java to Operate Amazon DocumentDB
To operate Amazon DocumentDB using Java, you need to complete the following steps:
1. Add Maven dependency: Add Java driver dependencies for DocumentDB in the pom.xml file of the project. For example, you can use the following dependencies:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.999</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>mongodb</artifactId>
<version>2.17.20</version>
</dependency>
2. Configure Amazon DocumentDB connection: You need to configure the Java application using Amazon DocumentDB's terminal node, username, and password. You can configure the connection using the following code:
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
ConnectionString connectionString = new ConnectionString("mongodb://<username>:<password>@<host>:<port>/<database>");
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
MongoClient client = MongoClients.create(settings);
Please ensure to replace 'username>', 'password>', 'host>', 'port>', and 'database>' with the corresponding values for your Amazon DocumentDB instance.
3. Perform data insertion: Use the following code to insert data into the set:
import org.bson.Document;
import com.mongodb.client.MongoCollection;
MongoCollection<Document> collection = client.getDatabase("<database>").getCollection("<collection>");
Document document = new Document("name", "John Doe")
.append("age", 30)
.append("email", "johndoe@example.com");
collection.insertOne(document);
Please replace '<database>' and '<collection>' with your database and collection names.
4. Execute data query: Use the following code to execute the query:
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
FindIterable<Document> documents = collection.find();
MongoCursor<Document> cursor = documents.iterator();
while (cursor.hasNext()) {
Document document = cursor.next();
//Processing Document Data
String name = document.getString("name");
int age = document.getInteger("age");
String email = document.getString("email");
//Output Document Data
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Email: " + email);
}
5. Perform data update: Use the following code to update document data:
import com.mongodb.client.result.UpdateResult;
import static com.mongodb.client.model.Filters.eq;
UpdateResult result = collection.updateOne(eq("name", "John Doe"),
new Document("$set", new Document("age", 40)));
System.out.println("Modified count: " + result.getModifiedCount());
6. Perform data deletion: Use the following code to delete the document:
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Filters.eq;
DeleteResult result = collection.deleteOne(eq("name", "John Doe"));
System.out.println("Deleted count: " + result.getDeletedCount());
These are the basic steps and sample code for operating Amazon DocumentDB using Java. You can modify and expand these examples as needed to meet your specific needs.