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, .NET, Node.js, Ir, PHP, Pythone 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 Mapa 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: Mapa, Lista, Conjuntoe Queue. They are all variants of JSON arrays in the database but presented as native types to your client application.
- Lista – an array that stores values in order
- Mapa – also known as a dictionary – stores values by key
- Conjunto – 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
O Java e .NET APIs have the tightest native Collections interfaces. This short example edits a user profile document as a Mapa 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.
1 2 |
Mapa<Cordas, Cordas> userInfo = novo CouchbaseMap<Cordas>("user:mnunberg", balde); userInfo.colocar("email", "mark.nunberg@couchbase.com"); |
Similarly the Lista is accessible through the CouchbaseArrayList and easily appended to.
1 2 3 |
Lista<Cordas> ll = novo CouchbaseArrayList<Cordas>("user:mnunberg_list", balde); ll.adicionar("msg1"); ll.adicionar("msg2"); |
- See it in action using .NET examples in this blog post.
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 empurrar 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.
1 2 3 4 5 |
>>> cb.queue_push("messages::tyler", {'timestamp': 1485389293, 'from':'user::mark', 'content':'Dear Tyler'}, criar=Verdadeiro) >>> cb.queue_push("messages::tyler", {'timestamp': 1486390293, 'from':'user::jody', 'content':'Dear John...'}) >>> cb.queue_pop("messages::tyler").valor {u'content': u'Dear Tyler', u'timestamp': 1485389293, u'from': u'user::mark'} |
Python Data Structures Examples – List
The following example shows a simplified Python example using the Lista 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.
1 2 3 |
>>> cb.list_append("garage1", ['vehicle::1A', '2017-01-24 08:02:00'], criar=Verdadeiro) >>> cb.list_append("garage1", ['vehicle::2A', '2017-01-24 10:21:00']) >>> cb.list_append("garage1", ['vehicle::1A', '2017-01-25 17:16:00']) |
The resulting document is an array with each entry holding two values in an array.
1 2 3 |
[ [ "vehicle::1A", "2017-01-24 08:02:00" ], [ "vehicle::2A", "2017-01-24 10:21:00" ], [ "vehicle::1A", "2017-01-25 17:16:00" ] ] |
Retrieving the values into a Python list is done easily. Just grab the document and it’s instantly available to iterate over.
1 2 3 4 5 6 |
>>> garage1 = cb.obter('garage1') >>> para rec em garage1.valor: impressão rec [u'vehicle::1A', u'2017-01-24 08:02:00'] [u'vehicle::2A', u'2017-01-24 10:21:00'] [u'vehicle::1A', u'2017-01-25 17:16:00'] |
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.
- New to Couchbase? See our Getting Started Guide or jump directly to our Portal do desenvolvedor.
- Assista ao nosso Couchbase Connect session on Data Structures and more.
- See more .NET Data Structures examples using Couchbase List, Queue and Dictionary.
[…] See code samples and more details in Data Structures: Native Collections New in Couchbase 4.6. […]
[…] 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. […]