use of com.ibm.cloud.sdk.core.service.exception.ServiceResponseException in project cloudant-java-sdk by IBM.
the class CreateDbAndDoc method main.
public static void main(String[] args) {
// 1. Create a client with `CLOUDANT` default service name ============
Cloudant client = Cloudant.newInstance();
// 2. Create a database ===============================================
// Create a database object with "orders" id
String exampleDbName = "orders";
PutDatabaseOptions putDbOptions = new PutDatabaseOptions.Builder().db(exampleDbName).build();
// Try to create database if it doesn't exist
try {
Ok putDatabaseResult = client.putDatabase(putDbOptions).execute().getResult();
if (putDatabaseResult.isOk()) {
System.out.println("\"" + exampleDbName + "\" database created.");
}
} catch (ServiceResponseException sre) {
if (sre.getStatusCode() == 412)
System.out.println("Cannot create \"" + exampleDbName + "\" database, it already exists.");
}
// 3. Create a document ===============================================
// Create a document object with "example" id
String exampleDocId = "example";
Document exampleDocument = new Document();
// Setting id for the document is optional when "postDocument" method is used for CREATE.
// When id is not provided the server will generate one for your document.
exampleDocument.setId(exampleDocId);
// Add "name" and "joined" fields to the document
exampleDocument.put("name", "Bob Smith");
exampleDocument.put("joined", "2019-01-24T10:42:59.000Z");
// Save the document in the database with "postDocument" method
PostDocumentOptions createDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(exampleDocument).build();
DocumentResult createDocumentResponse = client.postDocument(createDocumentOptions).execute().getResult();
// ====================================================================
// Note: saving the document can also be done with the "putDocument"
// method. In this case `docId` is required for a CREATE operation:
/*
PutDocumentOptions createDocumentOptions =
new PutDocumentOptions.Builder()
.db(exampleDbName)
.docId(exampleDocId)
.document(exampleDocument)
.build();
DocumentResult createDocumentResponse = client
.putDocument(createDocumentOptions)
.execute()
.getResult();
*/
// ====================================================================
// Keeping track of the revision number of the document object
// is necessary for further UPDATE/DELETE operations:
exampleDocument.setRev(createDocumentResponse.getRev());
System.out.println("You have created the document:\n" + exampleDocument);
}
Aggregations