Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
JNoSQL/Diana/Coucbase
Contents
Coucbase
Couchbase Server, originally known as Membase, is an open-source, distributed (shared-nothing architecture) multi-model NoSQL document-oriented database software package that is optimized for interactive applications.
Maven Project
To use Coucbase driver in a Maven Project:
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>couchbase-driver</artifactId>
<version>version</version>
</dependency>
E.g:
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>couchbase-driver</artifactId>
<version>0.0.1</version>
</dependency>
Key Value
The Coucbase key-value implementation uses the CouchbaseKeyValueConfiguration to the configuration. This class has configurations methods such as host, user, and password. Beyond the method also it can read a configuration from diana-couchbase.properties file.
- couchbase-host-: the prefix to add a new host
- couchbase-user: the user
- couchbase-password: the password
couchbase-host-1=localhost couchbase-user=root couchbase-password=123456
Sample Code
public class App {
public static void main(String[] args) {
KeyValueConfiguration configuration = new CouchbaseKeyValueConfiguration();
try (BucketManagerFactory managerFactory = configuration.get()) {
BucketManager bucket = managerFactory.getBucketManager("bucket");
List<String> list = managerFactory.getList("bucketList", String.class);
Set<String> set = managerFactory.getSet("bucketSet", String.class);
Map<String, Integer> map = managerFactory.getMap("bucketList", String.class, Integer.class);
Queue<String> queue = managerFactory.getQueue("queueList", String.class);
bucket.put("key", "value");
Optional<Value> value = bucket.get("key");
}
}
}
Document
The Couchbase document implementation uses the CouchbaseDocumentConfiguration to the configuration. This class has configurations methods such as host, user, and password. Beyond the method also it can read a configuration from diana-couchbase.properties file.
- couchbase-host-: the prefix to add a new host
- couchbase-user: the user
- couchbase-password: the password
Sample Code
public class App {
public static final String DATABASE = "database";
public static final String DOCUMENT_COLLECTION = "person";
public static void main(String[] args) {
DocumentConfiguration configuration = new CouchbaseDocumentConfiguration();
try(DocumentCollectionManagerFactory collectionFactory = configuration.get();) {
DocumentCollectionManager collectionManager = collectionFactory.get(DATABASE);
DocumentEntity entity = DocumentEntity.of(DOCUMENT_COLLECTION);
entity.add(Document.of("name", "Daniel Soro"));
entity.add(Document.of("age", 26));
DocumentEntity entitySaved = collectionManager.save(entity);
Optional<Document> id = entitySaved.find("_id");
DocumentQuery query = select.from(DOCUMENT_COLLECTION).where(DocumentCondition.eq(id.get());
List<DocumentEntity> documentsFound = collectionManager.select(query);
}
}
}
N1qlQuery
N1QL is a declarative query language that extends SQL for JSON. You can query data via native framework and language integration, a fluent API, or the JDBC/ODBC drivers.
Sample Code
public class App {
public static final String DATABASE = "database";
public static final String DOCUMENT_COLLECTION = "person";
public static void main(String[] args) {
CouchbaseDocumentConfiguration configuration = new CouchbaseDocumentConfiguration();
try(CouhbaseDocumentCollectionManagerFactory collectionFactory = configuration.get();) {
CouchbaseDocumentCollectionManager collectionManager = collectionFactory.get(DATABASE);
List<DocumentEntity> documentsFound = collectionManager.n1qlQuery(" select * from data");
List<DocumentEntity> documentsFound2 = collectionManager.n1qlQuery(" select * from data");
Statement statement = select("fname", "lname", "age").from(i("default")).where(x("age").gt(x("$age")));
JsonObject placeholderValues = JsonObject.create().put("age", 22);
List<DocumentEntity> documentsFound2 = collectionManager.n1qlQuery(statement, placeholderValues);
}
}
}