Connecting Mule ESB to Couchbase NoSQL database


Mule ESB has connectors that helps to connect Mule ESB with many systems including nosql databases like MongoDB, Cassandra etc. I have created a mule connector to connect with Couchbase NoSQL database. Let us see how to use it.

What is couchbase?

Couchbase is a key-value or JSON document based NoSQL database.

You can download the couchbase and read about how to get started with nosql here.

Get Mule Connector

Mule couchbase connector is available in maven central. You can add below dependency to maven pom -

<dependency>
  <groupId>com.javastreets</groupId>
  <artifactId>mule-couchbase-connector</artifactId>
  <version>1.0.0</version>
</dependency>

Get Studio plugin

There is a studio plugin available. Download the UpdateSite.zip from here and install it in Anypoint Studio.

mule couchbase connector plugin

After installation, you should see a Couchbase DB in Mule Palette -

mule couchbase connector mule palette

Using connector

Connector exposes many CRUD methods, below are sample flows that shows usage of all those methods -

  • Get Document

<flow name="couchbase-connector-demo-get-document">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/get" doc:name="HTTP"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    <couchbasedb:get-document config-ref="Couchbase_DB__Configuration" id="#['test1']" doc:name="Couchbase DB"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
  • UpSert Document: Updates or Insert document with provided document id.

<flow name="couchbase-connector-demo-upsert-document">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/upsert" doc:name="HTTP"/>
    <logger message="#[message.inboundProperties.'http.query.params'.docid]" level="INFO" doc:name="Logger"/>
    <couchbasedb:upsert-document config-ref="Couchbase_DB__Configuration" doc:name="Couchbase DB">
        <couchbasedb:java-map-document id="#[message.inboundProperties.'http.query.params'.docid]">
            <couchbasedb:content ref="#[{&quot;key1&quot;: &quot;Val1&quot;, &quot;Key2&quot;:&quot;Value2&quot;, &quot;time&quot; : server.dateTime.getMilliSeconds()}]"/>
        </couchbasedb:java-map-document>
    </couchbasedb:upsert-document>
    <set-payload value="#['Document in server: '+ payload]" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
  • Insert Document: Inserts document with given id in database. If document id exists, it will throw exception.

<flow name="couchbase-connector-demo-insert-document">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/insert" doc:name="HTTP"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    <couchbasedb:insert-document config-ref="Couchbase_DB__Configuration" doc:name="Couchbase DB">
        <couchbasedb:java-map-document id="#[message.inboundProperties.'http.query.params'.docid]">
            <couchbasedb:content ref="#[{&quot;key1&quot;: &quot;Val1&quot;, &quot;Key2&quot;:&quot;Value2&quot;}]"/>
        </couchbasedb:java-map-document>
    </couchbasedb:insert-document>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
  • Update Document: Insert Document: Update document with given id in database. If document id does not exist, it will throw exception.

<flow name="couchbase-connector-demo-update-document">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/update" doc:name="HTTP"/>
    <logger message="#[message.inboundProperties.'http.query.params'.docid]" level="INFO" doc:name="Logger"/>
    <couchbasedb:update-document config-ref="Couchbase_DB__Configuration" doc:name="Couchbase DB">
        <couchbasedb:java-map-document id="#[message.inboundProperties.'http.query.params'.docid]">
            <couchbasedb:content ref="#[{&quot;key1&quot;: &quot;Val1&quot;, &quot;Key2&quot;:&quot;Value2&quot;, &quot;time&quot; : server.dateTime.getMilliSeconds()}]"/>
        </couchbasedb:java-map-document>
    </couchbasedb:update-document>
    <set-payload value="#['Document in server: '+ payload]" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
  • Delete Document

<flow name="couchbase-connector-demo-delete-document">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/delete" doc:name="HTTP"/>
    <logger message="#[message.inboundProperties.'http.query.params'.docid]" level="INFO" doc:name="Logger"/>
    <couchbasedb:delete-document config-ref="Couchbase_DB__Configuration" doc:name="Couchbase DB">
        <couchbasedb:java-map-document id="#[message.inboundProperties.'http.query.params'.docid]"/>
    </couchbasedb:delete-document>
    <set-payload value="#['Document Removed?: '+ payload]" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
  • Execute Query : Runs the N1QL Query on couchbase database

<flow name="couchbase-connector-demo-execute-query">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/executeQuery" doc:name="HTTP"/>
    <logger message="#[message.inboundProperties.'http.query.params'.docid]" level="INFO" doc:name="Logger"/>
    <couchbasedb:execute-query config-ref="Couchbase_DB__Configuration" query="select * from default where type='user'" doc:name="Couchbase DB" params-ref="#[]">
    </couchbasedb:execute-query>
    <set-payload value="#['Document in server: '+ payload]" doc:name="Set Payload"/>
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>

Conclusion

Mule integrations are fun, go give it a try.

on twitter to get updates on new posts.

Stay updated!

On this blog, I post articles about different technologies like Java, MuleSoft, and much more.

You can get updates for new Posts in your email by subscribing to JavaStreets feed here -


Lives on Java Planet, Walks on Java Streets, Read/Writes in Java, JCP member, Jakarta EE enthusiast, MuleSoft Integration Architect, MuleSoft Community Ambassador, Open Source Contributor and Supporter, also writes at Unit Testers, A Family man!