Access Couchbase

After I installed the Couchbase server on my Linux machine I wanted to show some date into a web browser using my own application.

I found this page which gave me the intial idea who to start. In teh Eclipse I create a new maven project based on JDK 8. All sources were added and the pom.xml updated. However the code were not able to run and I made several adjustments on it.

public @Bean Bucket bucket() {
    Cluster mycluster;
    // mycluster = CouchbaseCluster.create(hostname);
    mycluster = Cluster.connect(hostname, user, password);


    return mycluster.bucket(bucket);
    // return cluster().openBucket(user, password);
}

The connect to the DB has change with version 3 of the client. Also I add the user name to the application.properties.
The pom.xml has been updated with these entries:


<properties>
    <jackson.version>2.11.2</jackson.version>
</properties>    
<dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>3.3.9.RELEASE</version>
    </dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-logging</artifactId>
	<version>1.3.1.RELEASE</version>
</dependency>

In addtion I deleted some lib from the .m2 couchbase directory before they get installed without error.

Now I created a runtime environment using the Application/Main class and I was able to start the Tomcat.

There are a couple of issues with the client jar of couchbase and dependencies. Also the API has been changed since the web site mention above has been created. Now the cluster is the main entitiy to query the database:

mycluster = Cluster.connect(hostname, user, password);couchbase.Database.setCluster(mycluster);
Bucket bucket = mycluster.bucket(bucket);
String query = "select city,code,country, description from <code>beer-sample</code> where code == '94107' ";
QueryResult result = curCluster.query(query);
return extractResultOrThrow(result);

private static List> extractResultOrThrow(QueryResult result) {    
List<Map<String, Object>> content = new ArrayList<Map<String, Object>>();
    for (JsonObject row : result.rowsAsObject()) {
        content.add(row.toMap());
    }
    return content;
}

Postman request

Post request to product rest api

The couchbase is offering a API which is exposed at 8093 (not 8091 for the DV itself)

Using this statements can be executed using the statement param in the body. Also a basic auth needs to be provided which is the user credentials.