Page tree
Skip to end of metadata
Go to start of metadata

CAST supports Couchbase via its NoSQL for Java extension. Details about the support provided for Java with Spring Data source code is discussed below.

Supported Client Libraries

Supported Operations

Operation

Methods Supported

Insert
  • insert
  • save
Select
  • exists
  • findById
  • findall
  • count
  • findAllById
  • findByView
  • findByN1QL
  • findBySpatialView
  • queryN1QL
  • querySpatialView
  • queryView
Delete
  • delete
  • deleteall
  • deleteById
  • remove
Update
  • update

Objects

Icon Description

Java Couchbase Cluster

Java Couchbase Bucket

Java Couchbase Unknown Cluster

 

Java Couchbase Unknown Bucket 

Links

Links are created for transaction and function point needs:

Link typeSource and destination of linkMethods supported
parentLink

Between Couchbase Cluster object and Couchbase Bucket object


useLinkBetween the caller Spring Data Java Method objects and Couchbase Buckets objects
  • Execute

useSelectLink

 Select methods supported
  • exists
  • findById
  • findall
  • count
  • findAllById
  • findByView
  • findByN1QL
  • findBySpatialView
  • queryN1QL
  • querySpatialView
  • queryView
useUpdateLink
  • update
useDeleteLink
  • delete
  • deleteall
  • deleteById
  • remove
useInsertLink
  • insert
  • save

What results can you expect?

Once the analysis/snapshot generation is completed, you can view the results in the normal manner (for example via CAST Enlighten). Some examples are shown below.

Cluster and Bucket Identification 

 Cluster and bucket from properties file
Bucket identification from proporties file
spring.couchbase.bootstrap-hosts=localhost
spring.couchbase.bucket.name=test

Multiple bucket
@Bean
	public Bucket campusBucket() throws Exception {
	    return couchbaseCluster().openBucket("test2", "pwd");
	}
	 @Bean
	 public CouchbaseTemplate campusTemplate() throws Exception {
	     CouchbaseTemplate template = new CouchbaseTemplate(
	       couchbaseClusterInfo(), campusBucket(),
	       mappingCouchbaseConverter(), translationService());
	     template.setDefaultConsistency(getDefaultConsistency());
	     return template;
	 }

	 @Override
	 public void configureRepositoryOperationsMapping(
	  RepositoryOperationsMapping baseMapping) {
	  try {
	   baseMapping.mapEntity(Campus.class, campusTemplate());
	  } catch (Exception e) {
	  }
	 }

Cluster and Bucket Identification using Java Configuration

 Cluster and bucket info from java configuration
public class dbConfig extends AbstractCouchbaseConfiguration {
	
	@Value("${couchbase.bucketname}")
	private String bucketName;
	
	@Value("${couchbase.node}")
	private String node;
	
	@Value("${couchbase.password}")
	private String pswd;
	

    @Override
    protected String getBucketName() {
        this.bucketName = bucketName;
        return this.bucketName;
    }

    @Override
    protected String getBucketPassword() {
    	this.pswd = pswd;
        return this.pswd;
    }

	@Override
	protected List<String> getBootstrapHosts(String x,String y) {
		// TODO Auto-generated method stub.
		this.node = node;
		List<String> nodes = new ArrayList<String>("one","two","three");
		nodes.add(this.node);
		return nodes;
	}

}

Marking a Cluster and Bucket Unknown

 Unknown cluster and bucket

When no Cluster or Bucket Object can be identified, but there are Couchbase Operations detected from the Source Code. Then an Unknown Couchbase Cluster and Unknown Couchbase Bucket is Created.

Couchbase Insert Operation

 Insert
@GetMapping("/insert")
	public void insert() {
		CouchbaseOperations cop= context.getBean(campusRepo.class).getCouchbaseOperations();
		List<Campus> campus = new ArrayList<Campus>();
		campus.add(new Campus("SP234","Chennai",new Point(67.456,78.987)));
		campus.add(new Campus("SP456","Hyderabad",new Point(56.274,108.943)));
		cop.insert(campus);
		
	}

@GetMapping("/execute")
	public book executeAct() {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.execute(new BucketCallback<book>() {
			public book doInBucket() throws java.util.concurrent.TimeoutException,ExecutionException, InterruptedException {
				book bk1 = new book("789UV","Spring-data","framework","Deepak Vohra");
				cop.save(bk1);
				return bk1;
			}});

insert op in another bucket
@GetMapping("/insertObj")
	public void insertObj() {
		CouchbaseOperations cop= context.getBean(campusRepo.class).getCouchbaseOperations();
		Campus campus = new Campus("SP678","Trivandrum",new Point(67.456,78.987));
		cop.insert(campus);
		
	}

Couchbase Update Operation

 Update
@PostMapping("/update")
	public void updateCustomer(@RequestBody Customer customer) {
		CouchbaseOperations cOperations = repository.getCouchbaseOperations();
		cOperations.update(customer);
	}

Couchbase Select Operation 

 Select
@GetMapping("/map/{id}")
	public book mapbyId(@PathVariable(required = true) String id) {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.findById(id,book.class);
	}
	
	

@GetMapping("/pquery/{genre}")
	public List<Genre> NPquery(@PathVariable(required = true) String genre) {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.findByN1QLProjection(N1qlQuery.simple("SELECT test.id, test.genre FROM test WHERE genre = '" + genre + "'"),Genre.class);
	}

@GetMapping("/query/{bname}")
	public List<book> Nquery(@PathVariable(required = true) String bname) {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.findByN1QL(N1qlQuery.simple("SELECT META(test).id AS _ID, META(test).cas AS _CAS, test.* FROM test WHERE bname = '" + bname + "'"),book.class);			
	}

@GetMapping("/squery")
	public List<book> SVquery() {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.findBySpatialView(SpatialViewQuery.from("book","all"),book.class );
	}

@GetMapping("/Vquery")
	public List<book> Vquery() {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.findByView(ViewQuery.from("book","all"),book.class);
	}

@GetMapping("/exist/{id}")
	public Boolean existId(@PathVariable(required = true) String id) {
		CouchbaseOperations cop = context.getBean(bookRepo.class).getCouchbaseOperations();
		return cop.exists(id);
	}

@GetMapping("/fetchall")
	public Iterable<Customer> getAll() {
		return repository.findAll();
	}

	@GetMapping("/getCount")
	public long getCount() {
		return repository.count();
	}

@GetMapping("/fetchbyId/{id}")
	public Optional<Customer> getById(@PathVariable(required = true) int id) {
		return repository.findById(id);
	}

Selecte operation from another bucket
@GetMapping("/queryV")
	public  ViewResult querV() {
		CouchbaseOperations cop= context.getBean(campusRepo.class).getCouchbaseOperations();
		return cop.queryView(ViewQuery.from("campus","byName"));
	}

Couchbase Delete Operation

 Delete
@GetMapping("/removeC")
	public void remC() {
		CouchbaseOperations cop= context.getBean(campusRepo.class).getCouchbaseOperations();
		List<Campus> campus = new ArrayList<Campus>();
		campus.add(new Campus("SP89A","Mumbai",new Point(32.456,160.987)));
		campus.add(new Campus("SPABC","Kolkata",new Point(90.274,186.943)));
		cop.remove(campus);
	}
	
	

@GetMapping("/deletebyname/{name}")
	public void deletebyname(@PathVariable(required = true) String name) {
		List<Customer> customers = repository.findByName(name);
		repository.deleteAll(customers);
	}

@GetMapping("/delete/{id}")
	public void delete(@PathVariable(required = true) int id) {
		Optional<Customer> customer = repository.findById(id);
		repository.delete(customer.get());
	}

Delete operation in another bucket
@GetMapping("/removeC")
	public void remC() {
		CouchbaseOperations cop= context.getBean(campusRepo.class).getCouchbaseOperations();
		List<Campus> campus = new ArrayList<Campus>();
		campus.add(new Campus("SP89A","Mumbai",new Point(32.456,160.987)));
		campus.add(new Campus("SPABC","Kolkata",new Point(90.274,186.943)));
		cop.remove(campus);
	}

Couchbase N1QL Query and Custom Methods

 N1QL Query and Custom Methods
@N1qlPrimaryIndexed
@ViewIndexed(designDoc="book", viewName="all")
public interface bookRepo extends CouchbaseRepository<book, String>{
	
	@Query("SELECT * FROM BOOK WHERE GENRE = $1")
	List<book> Bygenre(String genre);
}

@GetMapping("/bygenre/{genre}")
	public List<Book> bygenre(@PathVariable(required = true) String genre) {
		return context.getBean(bookRepo.class).Bygenre(genre);
}

Couchbase N1QL Query and Custom Methods

 Query Annotations
code
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "building")
public interface BuildingRepository extends CouchbaseRepository<Building, String> {

    List<Building> findByCompanyId(String companyId);

    Page<Building> findByCompanyIdAndNameLikeOrderByName(String companyId, String name, Pageable pageable);

    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId = $1 and $2 within #{#n1ql.bucket}")
    Building findByCompanyAndAreaId(String companyId, String areaId);

    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} AND ANY phone IN phoneNumbers SATISFIES phone = $1 END")
    List<Building> findByPhoneNumber(String telephoneNumber);

    @Query("SELECT COUNT(*) AS count FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} and companyId = $1")
    Long countBuildings(String companyId);
}


Evolutions

  • Query methods are supported
  • Better resolution for the collection names used in the crud operation 
  • Support for crud operations and query methods performed using class member such as "this."
  • Support for crud operations in multiple buckets
  • Couchbase operation fully supported
  • Support for Query Methods using @Query annotation

Limitations


Future

  • To Support Reactive Couchbase 
  • No labels