Data Structures in Couchbase 4.6 is our newest time-saving SDK feature.  These allow your client applications to easily map your array-based JSON data into language specific structures.

Leveraging native collections support in Couchbase will save you time and hassle:

  • Easily map JSON arrays into language specific structures
  • Couchbase Server manages the document efficiently – automatically using sub-document calls
  • You choose the data structure type you need and start coding

Support for Data Structures is available for all our languages: Java.NETNode.jsGoPHPPython, and C.  Including powerful Java and .NET implementations via Collections Frameworks and all other languages have a wide range of functional options.

This post shows how to get started using Data Structures, with specific examples in Java (using the Map type) and Python (using List and Queue types).  Video and reference links follow below.

Couchbase Data Structure Types

Four specific kinds of structures have been added to Couchbase client libraries: MapListSet, and Queue.  They are all variants of JSON arrays in the database but presented as native types to your client application.

  • List – an array that stores values in order
  • Map  – also known as a dictionary – stores values by key
  • Set – a variant of list that only retains unique combination of values
  • Queue – a variant of a list that offers push and pop operations to add/remove items from the queue in a first-in-first-out (FIFO) manner

Java Collections Examples – Map & List

The Java and .NET APIs have the tightest native Collections interfaces.  This short example edits a user profile document as a Map and adds or updates the email contact information.

As the Map gets updated, so does the Document in the background – no manual saving or upserting is required!

See many more beautiful Couchbase .NET Data Structures examples in Matthew Grove’s blog post.

Similarly the List is accessible through the CouchbaseArrayList and easily appended to.

Python Data Structures Examples – Queue

Here is a simple message Queue in Python, including a dictionary of timestamp, sender and some content.  Populate the queue using push to put new messages into it and then use pop to retrieve the first or next entry in the queue, while also removing it from the queue.

All this is done automatically behind the scenes when you use these functions.  No additional calls to the server are required to save the changed Queue.

Python Data Structures Examples – List

The following example shows a simplified Python example using the List type.  In each case a new document is also created at the same time that it is populated with values.  See the Couchbase Python documentation for examples of the other types.

In an IoT use case you may have sensors recording specific timestamped activities and related data values.  Here, a sensor has its own document and a vehicle ID and timestamp are recorded when detected by the sensor.

The resulting document is an array with each entry holding two values in an array.

Retrieving the values into a Python list is done easily.  Just grab the document and it’s instantly available to iterate over.

Next Step

As you can see, the syntax is easy and predictable. By offloading management of these structures to Couchbase Server it simplifies a lot of the communications required to manage dynamic documents. In no time you can be using Couchbase 4.6 as a Data Structure server for your applications.

 

Author

Posted by Tyler Mitchell

Works as Senior Product Marketing Manager at Couchbase, helping bring knowledge about products into the public limelight while also supporting our field teams with valuable content. His personal passion is all things geospatial, having worked in GIS for half his career. Now AI and Vector Search is top of mind.

2 Comments

  1. […] See code samples and more details in Data Structures: Native Collections New in Couchbase 4.6. […]

  2. […] There are still a ton of other options that I’ll touch on in a future blog post – for example, getting the server to do all the work of managing collections frameworks in .NET or Java. […]

Leave a reply