Support of Couchbase for Spring Data
CAST supports Couchbase via its com.castsoftware.nosqljava 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: 4.0.9 | ✅ |
Supported Operations
Operation |
Methods Supported |
---|---|
Insert | springframework.data.couchbase.core
org.springframework.data.couchbase.repository
|
Select | org.springframework.data.couchbase.core.CouchbaseOperations
org.springframework.data.couchbase.core.CouchbaseTemplate
org.springframework.data.couchbase.repository.support
org.springframework.data.couchbase.repository
org.springframework.data.couchbase.core.RxJavaCouchbaseOperations
org.springframework.data.repository.CrudRepository
|
Delete | org.springframework.data.couchbase.repository
org.springframework.data.couchbase.core
|
Update |
|
Objects
Icon | Description |
---|---|
Java Couchbase Cluster | |
Java Couchbase Bucket | |
Java Couchbase Collection | |
Java Couchbase Unknown Cluster | |
Java Couchbase Unknown Bucket | |
|
Java Couchbase Unknown Collection |
Links
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.