rename module to couchbase (#2646)
* added updated example codes * updated example code StringToCharStream * deleted StringToCharStream.java locally * removed redundant file * added code for apache commons collection SetUtils * refactored example code * added example code for bytebuddy * added example code for PCollections * update pom * refactored tests for PCollections * spring security xml config * spring security xml config * remove redundant comment * example code for apache-shiro * updated example code for Vavr Collections * updated Vavr's Collection example * updated Vavr Collection file * updated example code for Apache Shiro * updated Vavr Collections example * added example code for N1QL * update example code for N1QL * added integration test for N1QL * update N1QL Example code * update the N1QL example Code * rename module to couchbase * rename module to couchbase * change module name in parent module and pom
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
d626f9c2bf
commit
90a102ec47
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.couchbase.async;
|
||||
|
||||
public interface CouchbaseEntity {
|
||||
|
||||
String getId();
|
||||
|
||||
void setId(String id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.couchbase.async.person;
|
||||
|
||||
import com.baeldung.couchbase.async.CouchbaseEntity;
|
||||
|
||||
public class Person implements CouchbaseEntity {
|
||||
|
||||
private String id;
|
||||
private String type;
|
||||
private String name;
|
||||
private String homeTown;
|
||||
|
||||
Person() {
|
||||
}
|
||||
|
||||
public Person(Builder b) {
|
||||
this.id = b.id;
|
||||
this.type = b.type;
|
||||
this.name = b.name;
|
||||
this.homeTown = b.homeTown;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getHomeTown() {
|
||||
return homeTown;
|
||||
}
|
||||
|
||||
public void setHomeTown(String homeTown) {
|
||||
this.homeTown = homeTown;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String id;
|
||||
private String type;
|
||||
private String name;
|
||||
private String homeTown;
|
||||
|
||||
public static Builder newInstance() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public Person build() {
|
||||
return new Person(this);
|
||||
}
|
||||
|
||||
public Builder id(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder type(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder homeTown(String homeTown) {
|
||||
this.homeTown = homeTown;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.couchbase.async.person;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.couchbase.async.service.AbstractCrudService;
|
||||
import com.baeldung.couchbase.async.service.BucketService;
|
||||
|
||||
@Service
|
||||
public class PersonCrudService extends AbstractCrudService<Person> {
|
||||
|
||||
@Autowired
|
||||
public PersonCrudService(@Qualifier("TutorialBucketService") BucketService bucketService, PersonDocumentConverter converter) {
|
||||
super(bucketService, converter);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
loadBucket();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.couchbase.async.person;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.couchbase.async.service.JsonDocumentConverter;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
@Service
|
||||
public class PersonDocumentConverter implements JsonDocumentConverter<Person> {
|
||||
|
||||
@Override
|
||||
public JsonDocument toDocument(Person p) {
|
||||
JsonObject content = JsonObject.empty().put("type", "Person").put("name", p.getName()).put("homeTown", p.getHomeTown());
|
||||
return JsonDocument.create(p.getId(), content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person fromDocument(JsonDocument doc) {
|
||||
JsonObject content = doc.content();
|
||||
Person p = new Person();
|
||||
p.setId(doc.id());
|
||||
p.setType("Person");
|
||||
p.setName(content.getString("name"));
|
||||
p.setHomeTown(content.getString("homeTown"));
|
||||
return p;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.couchbase.async.person;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.couchbase.client.core.CouchbaseException;
|
||||
|
||||
@Service
|
||||
public class RegistrationService {
|
||||
|
||||
@Autowired
|
||||
private PersonCrudService crud;
|
||||
|
||||
public void registerNewPerson(String name, String homeTown) {
|
||||
Person person = new Person();
|
||||
person.setName(name);
|
||||
person.setHomeTown(homeTown);
|
||||
crud.create(person);
|
||||
}
|
||||
|
||||
public Person findRegistrant(String id) {
|
||||
try {
|
||||
return crud.read(id);
|
||||
} catch (CouchbaseException e) {
|
||||
return crud.readFromReplica(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
|
||||
public abstract class AbstractBucketService implements BucketService {
|
||||
|
||||
private ClusterService clusterService;
|
||||
|
||||
private Bucket bucket;
|
||||
|
||||
protected void openBucket() {
|
||||
bucket = clusterService.openBucket(getBucketName(), getBucketPassword());
|
||||
}
|
||||
|
||||
protected abstract String getBucketName();
|
||||
|
||||
protected abstract String getBucketPassword();
|
||||
|
||||
public AbstractBucketService(ClusterService clusterService) {
|
||||
this.clusterService = clusterService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bucket getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.baeldung.couchbase.async.CouchbaseEntity;
|
||||
import com.couchbase.client.core.BackpressureException;
|
||||
import com.couchbase.client.core.time.Delay;
|
||||
import com.couchbase.client.java.AsyncBucket;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.ReplicaMode;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.util.retry.RetryBuilder;
|
||||
|
||||
import rx.Observable;
|
||||
import rx.functions.Action1;
|
||||
import rx.functions.Func1;
|
||||
|
||||
public abstract class AbstractCrudService<T extends CouchbaseEntity> implements CrudService<T> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AbstractCrudService.class);
|
||||
|
||||
private BucketService bucketService;
|
||||
private Bucket bucket;
|
||||
private JsonDocumentConverter<T> converter;
|
||||
|
||||
public AbstractCrudService(BucketService bucketService, JsonDocumentConverter<T> converter) {
|
||||
this.bucketService = bucketService;
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
protected void loadBucket() {
|
||||
bucket = bucketService.getBucket();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(T t) {
|
||||
if (t.getId() == null) {
|
||||
t.setId(UUID.randomUUID().toString());
|
||||
}
|
||||
JsonDocument doc = converter.toDocument(t);
|
||||
bucket.insert(doc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read(String id) {
|
||||
JsonDocument doc = bucket.get(id);
|
||||
return (doc == null ? null : converter.fromDocument(doc));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T readFromReplica(String id) {
|
||||
List<JsonDocument> docs = bucket.getFromReplica(id, ReplicaMode.FIRST);
|
||||
return (docs.isEmpty() ? null : converter.fromDocument(docs.get(0)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(T t) {
|
||||
JsonDocument doc = converter.toDocument(t);
|
||||
bucket.upsert(doc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String id) {
|
||||
bucket.remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> readBulk(Iterable<String> ids) {
|
||||
final AsyncBucket asyncBucket = bucket.async();
|
||||
Observable<JsonDocument> asyncOperation = Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() {
|
||||
public Observable<JsonDocument> call(String key) {
|
||||
return asyncBucket.get(key);
|
||||
}
|
||||
});
|
||||
|
||||
final List<T> items = new ArrayList<T>();
|
||||
try {
|
||||
asyncOperation.toBlocking().forEach(new Action1<JsonDocument>() {
|
||||
public void call(JsonDocument doc) {
|
||||
T item = converter.fromDocument(doc);
|
||||
items.add(item);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.error("Error during bulk get", e);
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createBulk(Iterable<T> items) {
|
||||
final AsyncBucket asyncBucket = bucket.async();
|
||||
Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<JsonDocument> call(final T t) {
|
||||
if (t.getId() == null) {
|
||||
t.setId(UUID.randomUUID().toString());
|
||||
}
|
||||
JsonDocument doc = converter.toDocument(t);
|
||||
return asyncBucket.insert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
|
||||
}
|
||||
}).last().toBlocking().single();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateBulk(Iterable<T> items) {
|
||||
final AsyncBucket asyncBucket = bucket.async();
|
||||
Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<JsonDocument> call(final T t) {
|
||||
JsonDocument doc = converter.toDocument(t);
|
||||
return asyncBucket.upsert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
|
||||
}
|
||||
}).last().toBlocking().single();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBulk(Iterable<String> ids) {
|
||||
final AsyncBucket asyncBucket = bucket.async();
|
||||
Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Observable<JsonDocument> call(String key) {
|
||||
return asyncBucket.remove(key).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
|
||||
}
|
||||
}).last().toBlocking().single();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(String id) {
|
||||
return bucket.exists(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
|
||||
public interface BucketService {
|
||||
|
||||
Bucket getBucket();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
|
||||
public interface ClusterService {
|
||||
|
||||
Bucket openBucket(String name, String password);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.env.CouchbaseEnvironment;
|
||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
||||
|
||||
@Service
|
||||
public class ClusterServiceImpl implements ClusterService {
|
||||
|
||||
private Cluster cluster;
|
||||
private Map<String, Bucket> buckets = new ConcurrentHashMap<>();
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.create();
|
||||
cluster = CouchbaseCluster.create(env, "localhost");
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public Bucket openBucket(String name, String password) {
|
||||
if (!buckets.containsKey(name)) {
|
||||
Bucket bucket = cluster.openBucket(name, password);
|
||||
buckets.put(name, bucket);
|
||||
}
|
||||
return buckets.get(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CrudService<T> {
|
||||
|
||||
void create(T t);
|
||||
|
||||
T read(String id);
|
||||
|
||||
T readFromReplica(String id);
|
||||
|
||||
void update(T t);
|
||||
|
||||
void delete(String id);
|
||||
|
||||
List<T> readBulk(Iterable<String> ids);
|
||||
|
||||
void createBulk(Iterable<T> items);
|
||||
|
||||
void updateBulk(Iterable<T> items);
|
||||
|
||||
void deleteBulk(Iterable<String> ids);
|
||||
|
||||
boolean exists(String id);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
|
||||
public interface JsonDocumentConverter<T> {
|
||||
|
||||
JsonDocument toDocument(T t);
|
||||
|
||||
T fromDocument(JsonDocument doc);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.couchbase.async.service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Qualifier("TutorialBucketService")
|
||||
public class TutorialBucketService extends AbstractBucketService {
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
openBucket();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public TutorialBucketService(ClusterService clusterService) {
|
||||
super(clusterService);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketName() {
|
||||
return "baeldung-tutorial";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBucketPassword() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.couchbase.intro;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.couchbase.client.core.CouchbaseException;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.ReplicaMode;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
import com.couchbase.client.java.env.CouchbaseEnvironment;
|
||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
||||
|
||||
public class CodeSnippets {
|
||||
|
||||
static Cluster loadClusterWithDefaultEnvironment() {
|
||||
return CouchbaseCluster.create("localhost");
|
||||
}
|
||||
|
||||
static Cluster loadClusterWithCustomEnvironment() {
|
||||
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().connectTimeout(10000).kvTimeout(3000).build();
|
||||
return CouchbaseCluster.create(env, "localhost");
|
||||
}
|
||||
|
||||
static Bucket loadDefaultBucketWithBlankPassword(Cluster cluster) {
|
||||
return cluster.openBucket();
|
||||
}
|
||||
|
||||
static Bucket loadBaeldungBucket(Cluster cluster) {
|
||||
return cluster.openBucket("baeldung", "");
|
||||
}
|
||||
|
||||
static JsonDocument insertExample(Bucket bucket) {
|
||||
JsonObject content = JsonObject.empty().put("name", "John Doe").put("type", "Person").put("email", "john.doe@mydomain.com").put("homeTown", "Chicago");
|
||||
String id = UUID.randomUUID().toString();
|
||||
JsonDocument document = JsonDocument.create(id, content);
|
||||
JsonDocument inserted = bucket.insert(document);
|
||||
return inserted;
|
||||
}
|
||||
|
||||
static JsonDocument retrieveAndUpsertExample(Bucket bucket, String id) {
|
||||
JsonDocument document = bucket.get(id);
|
||||
JsonObject content = document.content();
|
||||
content.put("homeTown", "Kansas City");
|
||||
JsonDocument upserted = bucket.upsert(document);
|
||||
return upserted;
|
||||
}
|
||||
|
||||
static JsonDocument replaceExample(Bucket bucket, String id) {
|
||||
JsonDocument document = bucket.get(id);
|
||||
JsonObject content = document.content();
|
||||
content.put("homeTown", "Milwaukee");
|
||||
JsonDocument replaced = bucket.replace(document);
|
||||
return replaced;
|
||||
}
|
||||
|
||||
static JsonDocument removeExample(Bucket bucket, String id) {
|
||||
JsonDocument removed = bucket.remove(id);
|
||||
return removed;
|
||||
}
|
||||
|
||||
static JsonDocument getFirstFromReplicaExample(Bucket bucket, String id) {
|
||||
try {
|
||||
return bucket.get(id);
|
||||
} catch (CouchbaseException e) {
|
||||
List<JsonDocument> list = bucket.getFromReplica(id, ReplicaMode.FIRST);
|
||||
if (!list.isEmpty()) {
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static JsonDocument getLatestReplicaVersion(Bucket bucket, String id) {
|
||||
long maxCasValue = -1;
|
||||
JsonDocument latest = null;
|
||||
for (JsonDocument replica : bucket.getFromReplica(id, ReplicaMode.ALL)) {
|
||||
if (replica.cas() > maxCasValue) {
|
||||
latest = replica;
|
||||
maxCasValue = replica.cas();
|
||||
}
|
||||
}
|
||||
return latest;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
public interface CouchbaseKeyGenerator<T> {
|
||||
|
||||
String generateKey(T t);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class DuplicateKeyException extends Exception {
|
||||
|
||||
public DuplicateKeyException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class RandomUUIDGenerator<T> implements CouchbaseKeyGenerator<T> {
|
||||
|
||||
@Override
|
||||
public String generateKey(T t) {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
public class StudentGrade {
|
||||
|
||||
private String name;
|
||||
private String course;
|
||||
private Integer grade;
|
||||
private Integer hours;
|
||||
|
||||
public StudentGrade() { }
|
||||
|
||||
public StudentGrade(String name, String course, Integer grade, Integer hours) {
|
||||
this.name = name;
|
||||
this.course = course;
|
||||
this.grade = grade;
|
||||
this.hours = hours;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCourse() {
|
||||
return course;
|
||||
}
|
||||
|
||||
public void setCourse(String course) {
|
||||
this.course = course;
|
||||
}
|
||||
|
||||
public Integer getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
public void setGrade(Integer grade) {
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
public Integer getHours() {
|
||||
return hours;
|
||||
}
|
||||
|
||||
public void setHours(Integer hours) {
|
||||
this.hours = hours;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
public class StudentGradeKeyGenerator implements CouchbaseKeyGenerator<StudentGrade> {
|
||||
|
||||
@Override
|
||||
public String generateKey(StudentGrade g) {
|
||||
return g.getName() + ":" + g.getCourse();
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.couchbase.client.java.document.json.JsonArray;
|
||||
import com.couchbase.client.java.view.ViewQuery;
|
||||
|
||||
public class StudentGradeQueryBuilder {
|
||||
|
||||
final ObjectMapper om = new ObjectMapper();
|
||||
|
||||
public ViewQuery findAll() {
|
||||
return ViewQuery.from("studentGrades", "findByCourse");
|
||||
}
|
||||
|
||||
public ViewQuery findByCourse(String course) {
|
||||
return ViewQuery.from("studentGrades", "findByCourse")
|
||||
.key(course);
|
||||
}
|
||||
|
||||
public ViewQuery findByCourses(String... courses) {
|
||||
return ViewQuery.from("studentGrades", "findByCourse")
|
||||
.keys(JsonArray.from(courses));
|
||||
}
|
||||
|
||||
public ViewQuery findByGradeInRange(int lower, int upper, boolean inclusiveEnd) {
|
||||
return ViewQuery.from("studentGrades", "findByGrade")
|
||||
.startKey(lower)
|
||||
.endKey(upper)
|
||||
.inclusiveEnd(inclusiveEnd);
|
||||
}
|
||||
|
||||
public ViewQuery findByGradeLessThan(int upper) {
|
||||
return ViewQuery.from("studentGrades", "findByGrade")
|
||||
.endKey(upper)
|
||||
.inclusiveEnd(false);
|
||||
}
|
||||
|
||||
public ViewQuery findByGradeGreaterThan(int lower) {
|
||||
return ViewQuery.from("studentGrades", "findByGrade")
|
||||
.startKey(lower);
|
||||
}
|
||||
|
||||
public ViewQuery findByCourseAndGradeInRange(String course, int minGrade, int maxGrade, boolean inclusiveEnd) {
|
||||
return ViewQuery.from("studentGrades", "findByCourseAndGrade")
|
||||
.startKey(JsonArray.from(course, minGrade))
|
||||
.endKey(JsonArray.from(course, maxGrade))
|
||||
.inclusiveEnd(inclusiveEnd);
|
||||
}
|
||||
|
||||
public ViewQuery findTopGradesByCourse(String course, int limit) {
|
||||
return ViewQuery.from("studentGrades", "findByCourseAndGrade")
|
||||
.startKey(JsonArray.from(course, 100))
|
||||
.endKey(JsonArray.from(course, 0))
|
||||
.inclusiveEnd(true)
|
||||
.descending()
|
||||
.limit(limit);
|
||||
}
|
||||
|
||||
public ViewQuery countStudentsByCourse() {
|
||||
return ViewQuery.from("studentGrades", "countStudentsByCourse")
|
||||
.reduce()
|
||||
.groupLevel(1);
|
||||
}
|
||||
|
||||
public ViewQuery sumCreditsByStudent() {
|
||||
return ViewQuery.from("studentGrades", "sumCreditsByStudent")
|
||||
.reduce()
|
||||
.groupLevel(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package com.baeldung.couchbase.mapreduce;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonArray;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
import com.couchbase.client.java.view.ViewQuery;
|
||||
import com.couchbase.client.java.view.ViewResult;
|
||||
import com.couchbase.client.java.view.ViewRow;
|
||||
|
||||
public class StudentGradeService {
|
||||
|
||||
final CouchbaseKeyGenerator<StudentGrade> keyGenerator;
|
||||
final CouchbaseCluster cluster;
|
||||
final Bucket bucket;
|
||||
final ObjectMapper om = new ObjectMapper();
|
||||
final StudentGradeQueryBuilder queryBuilder;
|
||||
|
||||
public StudentGradeService(CouchbaseKeyGenerator<StudentGrade> keyGenerator) {
|
||||
this.keyGenerator = keyGenerator;
|
||||
this.queryBuilder = new StudentGradeQueryBuilder();
|
||||
cluster = CouchbaseCluster.create("127.0.0.1");
|
||||
bucket = cluster.openBucket("baeldung-tutorial");
|
||||
}
|
||||
|
||||
public String insert(StudentGrade studentGrade) throws DuplicateKeyException {
|
||||
String id = keyGenerator.generateKey(studentGrade);
|
||||
if(bucket.exists(id)) {
|
||||
throw new DuplicateKeyException("document already exists with key " + id);
|
||||
}
|
||||
JsonObject content = JsonObject.empty()
|
||||
.put("type", "StudentGrade")
|
||||
.put("name", studentGrade.getName())
|
||||
.put("course", studentGrade.getCourse())
|
||||
.put("grade", studentGrade.getGrade())
|
||||
.put("hours", studentGrade.getHours());
|
||||
JsonDocument doc = JsonDocument.create(id, content);
|
||||
bucket.insert(doc);
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<JsonDocument> findAll() {
|
||||
ViewQuery query = queryBuilder.findAll();
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
private List<JsonDocument> extractDocuments(ViewResult result) {
|
||||
List<JsonDocument> docs = new ArrayList<>();
|
||||
for(ViewRow row : result.allRows()) {
|
||||
JsonDocument doc = row.document();
|
||||
docs.add(doc);
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
|
||||
public List<JsonDocument> findByCourse(String course) {
|
||||
ViewQuery query = queryBuilder.findByCourse(course);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public List<JsonDocument> findByCourses(String... courses) {
|
||||
ViewQuery query = queryBuilder.findByCourses(courses);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public List<JsonDocument> findByGradeInRange(int lower, int upper, boolean inclusiveEnd) {
|
||||
ViewQuery query = queryBuilder.findByGradeInRange(lower, upper, inclusiveEnd);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public List<JsonDocument> findByGradeLessThan(int upper) {
|
||||
ViewQuery query = queryBuilder.findByGradeLessThan(upper);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public List<JsonDocument> findByGradeGreaterThan(int lower) {
|
||||
ViewQuery query = queryBuilder.findByGradeGreaterThan(lower);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public List<JsonDocument> findByCourseAndGradeInRange(String course, int minGrade, int maxGrade, boolean inclusiveEnd) {
|
||||
ViewQuery query = queryBuilder.findByCourseAndGradeInRange(course, minGrade, maxGrade, inclusiveEnd);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public List<JsonDocument> findTopGradesByCourse(String course, int limit) {
|
||||
ViewQuery query = queryBuilder.findTopGradesByCourse(course, limit);
|
||||
ViewResult result = bucket.query(query);
|
||||
return extractDocuments(result);
|
||||
}
|
||||
|
||||
public Map<String, Long> countStudentsByCourse() {
|
||||
ViewQuery query = ViewQuery.from("studentGrades", "countStudentsByCourse")
|
||||
.reduce()
|
||||
.groupLevel(1);
|
||||
ViewResult result = bucket.query(query);
|
||||
|
||||
Map<String, Long> numStudentsByCourse = new HashMap<>();
|
||||
for(ViewRow row : result.allRows()) {
|
||||
JsonArray keyArray = (JsonArray) row.key();
|
||||
String course = keyArray.getString(0);
|
||||
long count = Long.valueOf(row.value().toString());
|
||||
numStudentsByCourse.put(course, count);
|
||||
}
|
||||
|
||||
return numStudentsByCourse;
|
||||
}
|
||||
|
||||
public Map<String, Long> sumCreditHoursByStudent() {
|
||||
ViewQuery query = ViewQuery.from("studentGrades", "sumHoursByStudent")
|
||||
.reduce()
|
||||
.groupLevel(1);
|
||||
ViewResult result = bucket.query(query);
|
||||
|
||||
Map<String, Long> creditHoursByStudent = new HashMap<>();
|
||||
for(ViewRow row : result.allRows()) {
|
||||
String course = (String) row.key();
|
||||
long sum = Long.valueOf(row.value().toString());
|
||||
creditHoursByStudent.put(course, sum);
|
||||
}
|
||||
|
||||
return creditHoursByStudent;
|
||||
}
|
||||
|
||||
public Map<String, Long> sumGradePointsByStudent() {
|
||||
ViewQuery query = ViewQuery.from("studentGrades", "sumGradePointsByStudent")
|
||||
.reduce()
|
||||
.groupLevel(1);
|
||||
ViewResult result = bucket.query(query);
|
||||
|
||||
Map<String, Long> gradePointsByStudent = new HashMap<>();
|
||||
for(ViewRow row : result.allRows()) {
|
||||
String course = (String) row.key();
|
||||
long sum = Long.valueOf(row.value().toString());
|
||||
gradePointsByStudent.put(course, sum);
|
||||
}
|
||||
|
||||
return gradePointsByStudent;
|
||||
}
|
||||
|
||||
public Map<String, Float> calculateGpaByStudent() {
|
||||
Map<String, Long> creditHoursByStudent = sumCreditHoursByStudent();
|
||||
Map<String, Long> gradePointsByStudent = sumGradePointsByStudent();
|
||||
|
||||
Map<String, Float> result = new HashMap<>();
|
||||
for(Entry<String, Long> creditHoursEntry : creditHoursByStudent.entrySet()) {
|
||||
String name = creditHoursEntry.getKey();
|
||||
long totalHours = creditHoursEntry.getValue();
|
||||
long totalGradePoints = gradePointsByStudent.get(name);
|
||||
result.put(name, ((float) totalGradePoints / totalHours));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.couchbase.n1ql;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class BucketFactory {
|
||||
|
||||
@Autowired
|
||||
private Cluster cluster;
|
||||
|
||||
private Bucket travelSampleBucket;
|
||||
private Bucket testBucket;
|
||||
|
||||
public Bucket getTravelSampleBucket() {
|
||||
return (travelSampleBucket != null) ?
|
||||
travelSampleBucket : cluster.openBucket("travel-sample");
|
||||
}
|
||||
|
||||
public Bucket getTestBucket() {
|
||||
return (testBucket != null) ?
|
||||
testBucket : cluster.openBucket("test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.couchbase.n1ql;
|
||||
|
||||
import com.couchbase.client.java.query.N1qlQueryResult;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CodeSnippets {
|
||||
|
||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private static final Logger logger = Logger.getLogger(CodeSnippets.class.getName());
|
||||
|
||||
public static List<JsonNode> extractJsonResult(N1qlQueryResult result) {
|
||||
return result.allRows().stream()
|
||||
.map(row -> {
|
||||
try {
|
||||
return objectMapper.readTree(row.value().toString());
|
||||
}catch (IOException e) {
|
||||
logger.log(Level.WARNING, e.getLocalizedMessage());
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.couchbase.spring.person;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String id;
|
||||
private String type;
|
||||
private String name;
|
||||
private String homeTown;
|
||||
|
||||
Person() {
|
||||
}
|
||||
|
||||
public Person(Builder b) {
|
||||
this.id = b.id;
|
||||
this.type = b.type;
|
||||
this.name = b.name;
|
||||
this.homeTown = b.homeTown;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getHomeTown() {
|
||||
return homeTown;
|
||||
}
|
||||
|
||||
public void setHomeTown(String homeTown) {
|
||||
this.homeTown = homeTown;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String id;
|
||||
private String type;
|
||||
private String name;
|
||||
private String homeTown;
|
||||
|
||||
public static Builder newInstance() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public Person build() {
|
||||
return new Person(this);
|
||||
}
|
||||
|
||||
public Builder id(String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder type(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder homeTown(String homeTown) {
|
||||
this.homeTown = homeTown;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.couchbase.spring.person;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.couchbase.spring.service.CrudService;
|
||||
import com.baeldung.couchbase.spring.service.TutorialBucketService;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.ReplicaMode;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
|
||||
@Service
|
||||
public class PersonCrudService implements CrudService<Person> {
|
||||
|
||||
@Autowired
|
||||
private TutorialBucketService bucketService;
|
||||
|
||||
@Autowired
|
||||
private PersonDocumentConverter converter;
|
||||
|
||||
private Bucket bucket;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
bucket = bucketService.getBucket();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(Person person) {
|
||||
if (person.getId() == null) {
|
||||
person.setId(UUID.randomUUID().toString());
|
||||
}
|
||||
JsonDocument document = converter.toDocument(person);
|
||||
bucket.insert(document);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person read(String id) {
|
||||
JsonDocument doc = bucket.get(id);
|
||||
return (doc != null ? converter.fromDocument(doc) : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person readFromReplica(String id) {
|
||||
List<JsonDocument> docs = bucket.getFromReplica(id, ReplicaMode.FIRST);
|
||||
return (docs.isEmpty() ? null : converter.fromDocument(docs.get(0)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Person person) {
|
||||
JsonDocument document = converter.toDocument(person);
|
||||
bucket.upsert(document);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String id) {
|
||||
bucket.remove(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(String id) {
|
||||
return bucket.exists(id);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.couchbase.spring.person;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.couchbase.spring.service.JsonDocumentConverter;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.document.json.JsonObject;
|
||||
|
||||
@Service
|
||||
public class PersonDocumentConverter implements JsonDocumentConverter<Person> {
|
||||
|
||||
@Override
|
||||
public JsonDocument toDocument(Person p) {
|
||||
JsonObject content = JsonObject.empty().put("type", "Person").put("name", p.getName()).put("homeTown", p.getHomeTown());
|
||||
return JsonDocument.create(p.getId(), content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person fromDocument(JsonDocument doc) {
|
||||
JsonObject content = doc.content();
|
||||
Person p = new Person();
|
||||
p.setId(doc.id());
|
||||
p.setType("Person");
|
||||
p.setName(content.getString("name"));
|
||||
p.setHomeTown(content.getString("homeTown"));
|
||||
return p;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.couchbase.spring.person;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.couchbase.client.core.CouchbaseException;
|
||||
|
||||
@Service
|
||||
public class RegistrationService {
|
||||
|
||||
@Autowired
|
||||
private PersonCrudService crud;
|
||||
|
||||
public void registerNewPerson(String name, String homeTown) {
|
||||
Person person = new Person();
|
||||
person.setName(name);
|
||||
person.setHomeTown(homeTown);
|
||||
crud.create(person);
|
||||
}
|
||||
|
||||
public Person findRegistrant(String id) {
|
||||
try {
|
||||
return crud.read(id);
|
||||
} catch (CouchbaseException e) {
|
||||
return crud.readFromReplica(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.couchbase.spring.service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
|
||||
public interface BucketService {
|
||||
|
||||
Bucket getBucket();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.couchbase.spring.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.couchbase.client.java.AsyncBucket;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
|
||||
public interface ClusterService {
|
||||
|
||||
Bucket openBucket(String name, String password);
|
||||
|
||||
List<JsonDocument> getDocuments(Bucket bucket, Iterable<String> keys);
|
||||
|
||||
List<JsonDocument> getDocumentsAsync(AsyncBucket bucket, Iterable<String> keys);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.couchbase.spring.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.couchbase.client.java.AsyncBucket;
|
||||
import com.couchbase.client.java.Bucket;
|
||||
import com.couchbase.client.java.Cluster;
|
||||
import com.couchbase.client.java.CouchbaseCluster;
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
import com.couchbase.client.java.env.CouchbaseEnvironment;
|
||||
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
|
||||
|
||||
import rx.Observable;
|
||||
import rx.functions.Action1;
|
||||
import rx.functions.Func1;
|
||||
|
||||
@Service
|
||||
public class ClusterServiceImpl implements ClusterService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ClusterServiceImpl.class);
|
||||
|
||||
private Cluster cluster;
|
||||
private Map<String, Bucket> buckets = new ConcurrentHashMap<>();
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.create();
|
||||
cluster = CouchbaseCluster.create(env, "localhost");
|
||||
}
|
||||
|
||||
@Override
|
||||
synchronized public Bucket openBucket(String name, String password) {
|
||||
if (!buckets.containsKey(name)) {
|
||||
Bucket bucket = cluster.openBucket(name, password);
|
||||
buckets.put(name, bucket);
|
||||
}
|
||||
return buckets.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JsonDocument> getDocuments(Bucket bucket, Iterable<String> keys) {
|
||||
List<JsonDocument> docs = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
JsonDocument doc = bucket.get(key);
|
||||
if (doc != null) {
|
||||
docs.add(doc);
|
||||
}
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<JsonDocument> getDocumentsAsync(final AsyncBucket asyncBucket, Iterable<String> keys) {
|
||||
Observable<JsonDocument> asyncBulkGet = Observable.from(keys).flatMap(new Func1<String, Observable<JsonDocument>>() {
|
||||
public Observable<JsonDocument> call(String key) {
|
||||
return asyncBucket.get(key);
|
||||
}
|
||||
});
|
||||
|
||||
final List<JsonDocument> docs = new ArrayList<>();
|
||||
try {
|
||||
asyncBulkGet.toBlocking().forEach(new Action1<JsonDocument>() {
|
||||
public void call(JsonDocument doc) {
|
||||
docs.add(doc);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
logger.error("Error during bulk get", e);
|
||||
}
|
||||
|
||||
return docs;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.couchbase.spring.service;
|
||||
|
||||
public interface CrudService<T> {
|
||||
|
||||
void create(T t);
|
||||
|
||||
T read(String id);
|
||||
|
||||
T readFromReplica(String id);
|
||||
|
||||
void update(T t);
|
||||
|
||||
void delete(String id);
|
||||
|
||||
boolean exists(String id);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.couchbase.spring.service;
|
||||
|
||||
import com.couchbase.client.java.document.JsonDocument;
|
||||
|
||||
public interface JsonDocumentConverter<T> {
|
||||
|
||||
JsonDocument toDocument(T t);
|
||||
|
||||
T fromDocument(JsonDocument doc);
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.couchbase.spring.service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.couchbase.client.java.Bucket;
|
||||
|
||||
@Service
|
||||
@Qualifier("TutorialBucketService")
|
||||
public class TutorialBucketService implements BucketService {
|
||||
|
||||
@Autowired
|
||||
private ClusterService couchbase;
|
||||
|
||||
private Bucket bucket;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
bucket = couchbase.openBucket("baeldung-tutorial", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bucket getBucket() {
|
||||
return bucket;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user