Github source code is CouchDb
This article containst couchDb connection, insert document, delete document, select/find a document, update document, bulk insert/update java code examples.
Index
1. CouchDb connection datase and get connection client
2. Insert Database Using With Map
3. Find/Select a Document from Database
4. Update Database
5. Delete from Database
6. Insert Database Using Entity/Discrete Class
7. Bulk insert/update
System properties are:
java = 1.7
Project type = maven
Necessary maven dependencies:
<dependency> <groupid>com.cloudant</groupid> <artifactid>cloudant-client</artifactid> <version>2.9.0</version> </dependency> <dependency> <groupid>org.ektorp</groupid> <artifactid>org.ektorp</artifactid> <version>1.4.4</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-core</artifactid> <version>2.6.7</version> </dependency>
1. CouchDb connection datase and get connection client
import org.ektorp.CouchDbConnector; import org.ektorp.CouchDbInstance; import org.ektorp.http.HttpClient; import org.ektorp.http.StdHttpClient; import org.ektorp.impl.StdCouchDbInstance; import com.example.couchdb.constant.ICouchDbConstants; public class ConnectCouchDb { public static void main(String[] args) throws MalformedURLException { HttpClient httpClient = new StdHttpClient.Builder() .url("http://localhost:5984").username("admin") .password("12345") .build(); CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient); // if the second parameter is true, the database will be created if it // doesn't exists CouchDbConnector db = dbInstance.createConnector("my_first_database", true); } }
You can connect database with server url + port and database username and password.
2. Insert Database Using With Map
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;
public class InsertDbExample {
public static void main(String[] args) throws MalformedURLException {
HttpClient httpClient = new StdHttpClient.Builder()
.url("http://localhost:5984").username("admin")
.password("12345")
.build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
// if the second parameter is true, the database will be created if it
// doesn't exists
CouchDbConnector db = dbInstance.createConnector("my_first_database", true);
// Delete already exist
Map resultMap = db.find(Map.class, "1");
if (resultMap != null) {
db.delete(resultMap);
}
// delete end
// fill the map
Map<String, Object> map = new HashMap<String, Object>();
map.put("_id", "1");
map.put("name", "ektorp");
map.put("version", 2.41);
// Insert command
db.create(map);
//Find document
Map<String, Object> map2 = db.find(Map.class, "1");
showDocument(map2);
}
public static void showDocument(Map map){
System.out.println("------------ Displaying map ---------------");
for (Object id : map.keySet()) {
System.out.println(id + ": " + map.get(id));
}
System.out.println("---------------------------------");
}
}
3. Find/Select a Document from Database
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;
public class FindDbExample {
public static void main(String[] args) throws MalformedURLException {
HttpClient httpClient = new StdHttpClient.Builder()
.url("http://localhost:5984").username("admin")
.password("12345")
.build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = dbInstance.createConnector("my_first_database", true);
Map<String, Object> map2 = db.find(Map.class, "1");
showDocument(map2);
}
public static void showDocument(Map map){
System.out.println("------------ Displaying map ---------------");
for (Object id : map.keySet()) {
System.out.println(id + ": " + map.get(id));
}
System.out.println("---------------------------------");
}
}
4. Update Database
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;
public class UpdateDbExample {
public static void main(String[] args) throws MalformedURLException {
HttpClient httpClient = new StdHttpClient.Builder()
.url("http://localhost:5984").username("admin")
.password("12345")
.build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = dbInstance.createConnector("my_first_database", true);
//If already exist delete
Map resultMap = db.find(Map.class, "2");
if (resultMap != null) {
db.delete(resultMap);
}
//fill the map
Map<String, Object> map = new HashMap<String, Object>();
map.put("_id", "2");
map.put("name", "Ektorp");
map.put("version", 2.41);
//insert db
db.create(map);
showDocument(map);
//select from db
Map<String, Object> map2 = db.find(Map.class, "2");
map2.put("name", "Ektorp Updated");
System.out.println("name = Ektorp Updated");
//update name field
db.update(map2);
//select from db
map2 = db.find(Map.class, "2");
showDocument(map2);
}
public static void showDocument(Map map){
System.out.println("------------ Displaying map ---------------");
for (Object id : map.keySet()) {
System.out.println(id + ": " + map.get(id));
}
System.out.println("---------------------------------");
}
}
5. Delete from Database
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;
public class DeleteDbExample {
public static void main(String[] args) throws MalformedURLException {
HttpClient httpClient = new StdHttpClient.Builder()
.url("http://localhost:5984").username("admin")
.password("12345")
.build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = dbInstance.createConnector("my_first_database", true);
// Delete already exist
Map resultMap = db.find(Map.class, "1");
if (resultMap != null) {
db.delete(resultMap);
}
// delete end
// fill the map
Map<String, Object> map = new HashMap<String, Object>();
map.put("_id", "1");
map.put("name", "ektorp");
map.put("version", 2.41);
// Insert command
db.create(map);
Map<String, Object> map2 = db.find(Map.class,
"1");
showDocument(map2);
//Delete
resultMap = db.find(Map.class, "1");
if (resultMap != null) {
System.out.println("Database document deleted with id : " + "1");
db.delete(resultMap);
}
// delete end
resultMap = db.find(Map.class, "1");
if(resultMap == null){
System.out.println("Empty result");
}
}
public static void showDocument(Map map){
System.out.println("------------ Displaying map ---------------");
for (Object id : map.keySet()) {
System.out.println(id + ": " + map.get(id));
}
System.out.println("---------------------------------");
}
}
6. Insert Database Using Entity/Discrete Class
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;
import com.example.couchdb.entity.Sofa;
public class InsertDbWithDiscreteClass {
public static void main(String[] args) throws MalformedURLException {
HttpClient httpClient = new StdHttpClient.Builder()
.url("http://localhost:5984").username("admin")
.password("12345")
.build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector db = dbInstance.createConnector("my_first_database", true);
Map<String, Object> map;
//delete if already exist
map = db.find(Map.class, "ektorp");
if(map != null){
db.delete(map);
}
//delete end
//insert a document just has a id
map = new HashMap<String, Object>();
map.put("_id", "ektorp");
db.create(map );
map = db.find(Map.class, "ektorp");
showDocument(map);
//change entity's attributes
Sofa sofa = db.get(Sofa.class, "ektorp");
sofa.setColor("blue");
sofa.setAmount(150.0);
//update with entity
db.update(sofa);
System.out.println("--- updated color, amount -----\n");
map = db.find(Map.class, "ektorp");
showDocument(map);
}
public static void showDocument(Map map){
System.out.println("------------ Displaying map ---------------");
for (Object id : map.keySet()) {
System.out.println(id + ": " + map.get(id));
}
System.out.println("---------------------------------");
}
}
7. Entity class:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties({"id", "revision"})
public class Sofa {
@JsonProperty("_id")
private String id;
@JsonProperty("_rev")
private String revision;
private String color;
private Double amount;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public String getRevision() {
return revision;
}
public void setColor(String s) {
color = s;
}
public String getColor() {
return color;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
}
8. Bulk insert/update
import java.net.MalformedURLException; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.ektorp.CouchDbConnector; import org.ektorp.CouchDbInstance; import org.ektorp.http.HttpClient; import org.ektorp.http.StdHttpClient; import org.ektorp.impl.StdCouchDbInstance; public class BulkInsertDbExample { public static void main(String[] args) throws MalformedURLException { HttpClient httpClient = new StdHttpClient.Builder() .url("http://localhost:5984").username("admin") .password("12345") .build(); CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient); CouchDbConnector db = dbInstance.createConnector("my_first_database", true); //Delete already exist Map resultMap; resultMap = db.find(Map.class, "1"); if(resultMap != null){ db.delete(resultMap); } resultMap = db.find(Map.class, "2"); if(resultMap != null){ db.delete(resultMap); } //delete end //fill the map Map<String, Object> map1 = new HashMap<String, Object>(); Map<String, Object> map2 = new HashMap<String, Object>(); map1.put("_id", "1"); map1.put("name", "ektorp"); map1.put("version", 2.41); map2.put("_id", "2"); map2.put("name", "ektorp2"); map2.put("version", 2.41); List<Map<String, Object>> list = Arrays.asList(map1, map2); //Insert bulk command //Bulk update is same. db.executeBulk(list); System.out.println("Bulk insert done\n"); resultMap = db.find(Map.class, "1"); showDocument(map2); resultMap = db.find(Map.class, "2"); showDocument(map2); } public static void showDocument(Map map){ System.out.println("------------ Displaying map ---------------"); for (Object id : map.keySet()) { System.out.println(id + ": " + map.get(id)); } System.out.println("---------------------------------"); } }
Thank you very much.