CRUD Operations on CouchBase Database
Lets start inserting some documents.
If you look at it,Couch operates on JSON,serializing and deserializing java Objects into JSON leads to a lot of boilerplate code,
So we are going to use something called EntityDocument provided by couch client itself.
EntityDocument<@Valid Employee> employeeEntity = EntityDocument.create(employee);
CRUD operations is posted below.
package com.couch.demo.CouchDemo;
import javax.xml.bind.annotation.XmlRootElement;
import org.hibernate.validator.constraints.NotBlank;
@XmlRootElement
public class Employee {
  
  
 @NotBlank
 private String empId;
 @NotBlank
 private String empName;
 @NotBlank
 private String empDesignation;
 
 public Employee() {
  // TODO Auto-generated constructor stub
 }
 
 public Employee(String empId, String empName,
   String empDesignation) {
  super();
  this.empId = empId;
  this.empName = empName;
  this.empDesignation = empDesignation;
 }
 public String getEmpId() {
  return empId;
 }
 public void setEmpId(String empId) {
  this.empId = empId;
 }
 public String getEmpName() {
  return empName;
 }
 public void setEmpName(String empName) {
  this.empName = empName;
 }
 public String getEmpDesignation() {
  return empDesignation;
 }
 public void setEmpDesignation(String empDesignation) {
  this.empDesignation = empDesignation;
 }
 
}
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.EntityDocument;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.repository.Repository;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.stream.events.EntityDeclaration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("web")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class Controller {
 static CouchbaseCluster couchbasecluster = null;
 static Bucket bucket = null;
 
 
 static{
   couchbasecluster = CouchbaseCluster.create("localhost");
   bucket = couchbasecluster.openBucket("Test");
 }
 
 static  Repository repository = bucket.repository();
@RequestMapping("/saveemployee")
@POST
public @Valid Employee saveEmployee(@Valid @RequestBody Employee employee){
 
 
 EntityDocument<@Valid Employee> insert = repository.insert(EntityDocument.create(employee.getEmpId(),employee));
 return insert.content();
}
@GET
@RequestMapping("/getEmployee/{id}")
public Employee getEmployee(@PathVariable("id") String id){
 System.out.println("ID "+id);
 return repository.get(id,Employee.class).content();
 
}
@POST
@RequestMapping("/updateemployee")
public Employee updateEmployee(@Valid @RequestBody Employee employee){
 return repository.upsert(EntityDocument.create(employee.getEmpId(),employee)).content();
}
@POST
@RequestMapping("/deleteemployee/{id}")
public String deleteEmployee(@PathVariable("id") String id,@Valid @RequestBody Employee employee){
 return bucket.remove(id).id();
}
}
If your requirement is to get certain fields of a document by filtering, then you should go for N1QL Query.
Getting documents based on criteria using N1QL is shown in next chapter.

0 comments:
Post a Comment