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.jsIrPHPPythonC.  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: MapaListaConjuntoQueue.  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.

Similarly the Lista 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 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.

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.

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.

 

Autor

A ordem da postagem em relação a outras postagens. Tyler Mitchell - Gerente sênior de marketing de produtos

Trabalha como Gerente Sênior de Marketing de Produto na Couchbase, ajudando a levar o conhecimento sobre os produtos para o centro das atenções do público e, ao mesmo tempo, apoiando nossas equipes de campo com conteúdo valioso. Sua paixão pessoal são todas as coisas geoespaciais, tendo trabalhado em GIS durante metade de sua carreira. Agora, a IA e a pesquisa vetorial estão em sua mente.

2 Comentários

Posição vertical a partir do topo para iniciar o corte como uma porcentagem da altura da imagem.