Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture save or update events in Couchbase

I would like to be able to do some data manipulation when documents are updated or created in Couchbase.

Documents can arrive in our database either via Sync Gateway or our own code which streams data in from an http service. It would be great to have one place where I can intercept all updates.

We are running a Spring Boot REST API against this data so this would be the good place to have the interceptor/listener. Either way my preference would be for a Java solution.

The data is written as JSON rather than using Spring entities so I can't use ApplicationListener which only listens to events on Entity classes. Correct me if I'm wrong. I can find precious few examples of setting up ApplicationListeners so I may be wrong here but I can't seem to get it working.

I see that there is an Eventing service where you write Javascript but for a number of reasons I'm not keen to go that way. I'm not keen on fragmenting our API code across platforms and languages, not sure I can run the eventing service on our systems etc. Again, I'm open to debate though.

That leaves DCP only as far as I can tell which seems very low level. https://blog.couchbase.com/couchbases-history-everything-dcp/ but looks like the tool for the job.

The QUESTION: Is there an alternative, less low level, way to catch update events in Couchbase for JSON objects NOT entities other than DCP.

like image 407
Simbosan Avatar asked Oct 16 '18 21:10

Simbosan


People also ask

How is data stored in Couchbase?

The data is stored in a Couchbase cluster using buckets. Buckets are isolated, virtual containers which logically group records within a cluster. A bucket is the equivalent of a database. They provide a secure mechanism for organizing, managing and analyzing data storage.

What is CAS in Couchbase?

Write operations on Couchbase accept a parameter cas (create and set). Also the return result object of any non-data fetching query has cas property in it.

In which format data is stored in Couchbase?

Couchbase Mobile stores data in documents rather than in table rows. A document is a JSON object containing a number of key-value pairs. This means that it can take any form as long as it is valid JSON . The following image represents the same schema as the table above but in JSON format.


1 Answers

Disclaimer: I work for Couchbase and develop the Java DCP client.

If you've already evaluated the Eventing service and decided it doesn't meet your requirements, the Java DCP client might be worth looking into even though it's not officially supported. It's used by the official Couchbase connectors for Kafka, Spark, and Elasticsearch (all of which are open source) and is actively maintained.

If you only care about events that happened since your app started up, usage can be as simple as registering a callback and starting the event stream. Things get a bit more complicated if you need to remember your place in the stream and resume later (to process events that occurred while you were offline, for example), but there's example code for that case too.

The DCP protocol itself is well documented. If you decide to go this route, it might be good to read at least the Architecture section of that documentation. Also be aware that because the Java DCP Client is unsupported, the API can change without notice. (Officially supporting the library and providing a friendlier API are among our long-term goals, but we haven't committed to anything yet.)

like image 178
dnault Avatar answered Sep 22 '22 14:09

dnault