Using Java to Operate IBM Cloudant
IBM Cloudant is a hosted NoSQL database solution based on Apache CouchDB and provides hosted services. Using Java to operate Cloudant can be achieved through the Java SDK. This tutorial will demonstrate how to use Java for data insertion, modification, querying, and deletion.
1. Add Maven dependency
Firstly, the following Maven dependencies need to be added to the pom.xml file of the project:
<dependency>
<groupId>com.cloudant</groupId>
<artifactId>cloudant-client</artifactId>
<version>2.17.0</version>
</dependency>
2. Create Cloudant instance
To use Cloudant, you need to create a Cloudant instance. You can use the service credentials (username, password, and URL) in IBM Cloud to create an instance:
String username = "<your-username>";
String password = "<your-password>";
String url = "<your-url>";
CloudantClient client = ClientBuilder.account(username)
.username(username)
.password(password)
.url(url)
.build();
3. Data insertion
Next, we will demonstrate how to insert a document (JSON object) into the Cloudant database.
//Get Database Objects
Database db = client.database("mydatabase", false);
//Create a JSON object
JsonObject json = new JsonObject();
json.addProperty("name", "John Doe");
json.addProperty("age", 30);
//Insert Document
Response response = db.save(json);
if (response.getError() == null) {
System. out. println ("Insert successful");
}
4. Data modification
Cloudant uses documentation for_ ID and_ The rev attribute identifies each document. To modify a document, you need to first retrieve it and update its content before saving it.
//Obtain the document to be modified
Document doc = db.find(Document.class, "<document-id>");
//Update document content
doc.set("name", "Jane Smith");
//Save the modified document
Response response = db.update(doc);
if (response.getError() == null) {
System. out. println ("modified successfully");
}
5. Data Query
Using Cloudant's view function allows for easy data querying. Firstly, we need to create a view in the database.
//Create a Software design description and define views
DesignDocument designDoc = new DesignDocument();
designDoc.setId("_design/mydesigndoc");
View view = new View();
view.setMap("function (doc) { emit(doc.name, doc.age); }");
designDoc.addView("myview", view);
db.save(designDoc);
Then, the view can be used to query the specified data.
//Query View
ViewRequest<String, String> request = db.getViewRequestBuilder("mydesigndoc", "myview")
.newRequest(Key.Type.STRING, KeyValue.Type.STRING)
.build();
ViewResponse<String, String> response = request.getResponse();
List<Row<String, String>> rows = response.getRows();
for (Row<String, String> row : rows) {
System.out.println(row.getKey() + ": " + row.getValue());
}
6. Data deletion
To delete a document, use the db. remove (Document doc) method and pass the document object.
//Obtain the document to be deleted
Document doc = db.find(Document.class, "<document-id>");
//Delete Document
Response response = db.remove(doc);
if (response.getError() == null) {
System. out. println ("deleted successfully");
}
This is the complete example code of how to use Java to operate IBM Cloudant for data insertion, modification, query, and deletion. According to the actual situation, the above code can be modified and adapted.