Support of Couchbase for Spring Data

CAST supports Couchbase via its com.castsoftware.nosqljavaexternal link extension. Details about how this support is provided for Java with Spring Data source code is discussed below.

Supported Client Libraries

Library Versions Supported

Spring Data-couchbase

up to: 3.1.10

(tick)

Supported Operations

Operation

Methods Supported

Insert

org.springframework.data.couchbase.core.CouchbaseOperations.insert

org.springframework.data.couchbase.core.CouchbaseOperations.save

org.springframework.data.couchbase.core.CouchbaseTemplate.insert

org.springframework.data.couchbase.core.CouchbaseTemplate.save

org.springframework.data.repository.CrudRepository.save

org.springframework.data.repository.CrudRepository.saveAll

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.save

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.saveAll

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.save

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.saveAll

org.springframework.data.couchbase.repository.CouchbaseRepository.save

org.springframework.data.couchbase.repository.CouchbaseRepository.saveAll

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.save

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.saveAll

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.save

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.saveAll

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.save

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.saveAll

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.insert

Select

org.springframework.data.couchbase.core.CouchbaseOperations.findById

org.springframework.data.couchbase.core.CouchbaseOperations.findByView

org.springframework.data.couchbase.core.CouchbaseOperations.queryView

org.springframework.data.couchbase.core.CouchbaseOperations.findByN1QL

org.springframework.data.couchbase.core.CouchbaseOperations.findByN1QLProjection

org.springframework.data.couchbase.core.CouchbaseOperations.findBySpatialView

org.springframework.data.couchbase.core.CouchbaseOperations.queryN1QL

org.springframework.data.couchbase.core.CouchbaseOperations.querySpatialView

org.springframework.data.couchbase.core.CouchbaseOperations.queryView

org.springframework.data.couchbase.core.CouchbaseOperations.exists

org.springframework.data.couchbase.core.CouchbaseOperations.execute

org.springframework.data.couchbase.core.CouchbaseTemplate.exists

org.springframework.data.couchbase.core.CouchbaseTemplate.findById

org.springframework.data.couchbase.core.CouchbaseTemplate.findByView

org.springframework.data.couchbase.core.CouchbaseTemplate.queryView

org.springframework.data.couchbase.core.CouchbaseTemplate.findByN1QL

org.springframework.data.couchbase.core.CouchbaseTemplate.findByN1QLProjection

org.springframework.data.couchbase.core.CouchbaseTemplate.findBySpatialView

org.springframework.data.couchbase.core.CouchbaseTemplate.queryN1QL

org.springframework.data.couchbase.core.CouchbaseTemplate.querySpatialView

org.springframework.data.couchbase.core.CouchbaseTemplate.queryView

org.springframework.data.couchbase.core.CouchbaseTemplate.execute

org.springframework.data.repository.CrudRepository.findById

org.springframework.data.repository.CrudRepository.existsById

org.springframework.data.repository.CrudRepository.findAllById

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.findAll

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.findOne

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.existsById

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.count

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.findAllById

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.findAll

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.findOne

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.existsById

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.count

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.findAllById

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.findAll

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.existsById

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.count

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.findAllById

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.findById

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.findAll

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.existsById

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.count

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.findAllById

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.findById

org.springframework.data.repository.CrudRepository.count

org.springframework.data.repository.CrudRepository.findAll

org.springframework.data.couchbase.repository.CouchbaseRepository.findById

org.springframework.data.couchbase.repository.CouchbaseRepository.existsById

org.springframework.data.couchbase.repository.CouchbaseRepository.findAllById

org.springframework.data.couchbase.repository.CouchbaseRepository.count

org.springframework.data.couchbase.repository.CouchbaseRepository.findAll

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.findById

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.existsById

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.findAllById

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.count

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.findAll

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.exists

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.exists

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.queryN1QL

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.findById

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.queryN1QL

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.queryView

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.querySpatialView

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.findByView

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.findByN1QL

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.findBySpatialView

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.findByN1QLProjection

Delete

org.springframework.data.couchbase.core.CouchbaseOperations.remove

org.springframework.data.couchbase.core.CouchbaseTemplate.remove

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.delete

org.springframework.data.repository.CrudRepository.deleteAll

org.springframework.data.repository.CrudRepository.deleteById

org.springframework.data.repository.CrudRepository.delete

org.springframework.data.couchbase.repository.CouchbaseRepository.deleteAll

org.springframework.data.couchbase.repository.CouchbaseRepository.deleteById

org.springframework.data.couchbase.repository.CouchbaseRepository.delete

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.deleteAll

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.deleteById

org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository.delete

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.deleteAll

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.deleteById

org.springframework.data.couchbase.repository.support.N1qlCouchbaseRepository.delete

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.deleteAll

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.deleteById

org.springframework.data.couchbase.repository.support.ReactiveN1qlCouchbaseRepository.delete

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.deleteAll

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.deleteById

org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.delete

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.deleteById

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.deleteAll

org.springframework.data.couchbase.repository.support.SimpleReactiveCouchbaseRepository.delete

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.remove

Update

org.springframework.data.couchbase.core.CouchbaseOperations.update

org.springframework.data.couchbase.core.CouchbaseTemplate.update

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.update

org.springframework.data.couchbase.core.RxJavaCouchbaseOperations.save

Objects

Icon Description

Java Couchbase Cluster

Java Couchbase Bucket

Java Couchbase Collection

Java Couchbase Unknown Cluster

Java Couchbase Unknown Bucket 

Java Couchbase Unknown Collection
Link type Source and destination of link Methods supported
parentLink

Between Couchbase Cluster object, Couchbase Bucket object and Couchbase Collection object

-
useLink Between the caller Spring Data Java Method objects and Couchbase Collection objects

Execute

useSelectLink

Between the caller Spring Data Java Method objects and Couchbase Collection objects

exists

findById

findall

count

findAllById

findByView

findByN1QL

findBySpatialView

queryN1QL

querySpatialView

queryView

useUpdateLink Between the caller Spring Data Java Method objects and Couchbase Collection objects

update

useDeleteLink Between the caller Spring Data Java Method objects and Couchbase Collection objects

delete

deleteall

deleteById

remove

useInsertLink Between the caller Spring Data Java Method objects and Couchbase Collection objects

insert

save







What results can you expect?

Some example scenarios are shown below:

Cluster and Bucket Identification 

Bucket identification from properties file

spring.couchbase.bootstrap-hosts=localhost
spring.couchbase.bucket.name=test

Multiple buckets

@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

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

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

@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 in another bucket

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

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

Couchbase Select Operation 

@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);
    }

Couchbase Delete Operation

@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());
    }

Couchbase N1QL Query and Custom Methods

@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 Annotations

@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);
}

Known Limitations

  • Resolution of Database and Collection is limited, “Unknown” is used when not resolved.