/devguide-examples

Examples for topics in the developer guide

Primary LanguageCApache License 2.0Apache-2.0

Developer Guide Examples

See Couchbase Server version specific examples: 4.5 | 4.6 | 5.0 | 5.1

These examples are the basis for examples published in Couchbase documentation. There are examples for each language.

By design each example is fully self-contained and executable. For languages which have a significant setup, the example may be split into multiple files, but should still be executable. Error handling is optional to keep the example easy to read but should be described.

How to Use These Examples

The examples are versioned by the features introduced in a given server version.

The server-4.5 branch should be used for documentation content related to Couchbase Server 4.5, and so on.

Connecting to Couchbase

These examples show how to establish a connection from an SDK to a Couchbase cluster.

Basic Connection

This example should show how to connect to a remote Couchbase cluster and bucket

C | Python | Java| .NET | Go | node.js | PHP

SSL Connection

This example shows how to connect using the server's SSL certificate which has already been downloaded locally to the SDK host. The C-based examples will refer to this path when connecting, while the other SDKs will refer to a specific store and possibly have the example divided into installing and using the certificate.

C | Python | Java | .NET | Go | node.js | PHP

Basic Document/KV Operations

These examples demonstrate the most basic functionality of working with documents.

Updating/Storing

These examples show how to store an item into a cluster

C | Python | Java | .NET | Go | node.js | PHP

Retrieving

This example should show how to get items out of the cluster

C | Python | Java | .NET | Go | node.js | PHP

Advanced Document/KV Operations

These examples show how to perform more detailed operations on documents

Bulk Storing

This example should show how to store items into a cluster using the Bulk API

C | Python | Java | .NET | Go | node.js (N/A) | PHP

Bulk Retrieving

This example should show how to get items out of the cluster using the Bulk API

C | Python | Java | .NET | Go | node.js (N/A) | PHP

Counter

This example should show how to initialize and update a counter

C | Python | Java | .NET | Go | node.js | PHP

Expiry

This example should show how to initialize a document with an Expiry or "ttl" - time to live

C | Python | Java | .NET | Go | node.js | PHP

CAS Handling - Using CAS for Concurrent Mutations

This example will demonstrate concurrent mutations with and without using the CAS value. Without using the CAS value, some modifications may end up getting lost, whereas using the CAS within a proper retry mechanism will ensure that all mutations remain in tact

C | Python | Java | .NET | Go | node.js | PHP

Durability

Shows storing an item with durability requirements, attempting to persist/replicate to the maximum number of nodes available.

Some SDKs provide APIs to determine how many nodes are in the cluster, while some SDKs allow dynamically persisting to the total number of nodes available, while others only allow fixed numbers. More details may be found in the examples.

C | Python | Java | .NET | Go | node.js | PHP

Sub-Document Operations

Sub-Document operations (new in Couchbase 4.5) allows efficient addressing of sections within documents (sub-documents). Sub-Document is often abbreviated as subdoc.

Subdoc - Retrieving

Retrieve a few fields from a document; also demonstrate error handling if some fields are missing

C | Python | Java | .NET | Go | node.js | PHP

Subdoc - Updating/Storing

Modify a few fields within a document. Show error handling and the behavior of the create option

C | Python | Java | .NET | Go | node.js | PHP

N1QL Queries

These examples show how to query using N1QL

Query with Criteria

This example should show how to perform a simple query against the travel-sample bucket. The query is something like:

query = N1QLQuery('SELECT airportname, city, country FROM `travel-sample` '
                  'WHERE type="airport" AND city="Reno"')

C | Python | Java | .NET | Go | node.js | PHP

Query with Placeholders

This example should demonstrate how to use placeholders, and also the advantages they afford, perhaps by abstracting a given query away as a function, and passing a function parameter down as a query parameter. Note: the term "placeholder" was chosen as the term "parameter" is overloaded. Placeholder is a bit more related to what is happening in a query statement.

This example should also demonstrate in a commented section of code how to optimize this query.

C | Python | Java | .NET | Go | node.js | PHP

Query - Ensuring all Documents are the Latest (scan consistency)

This example should show how the scan_consistency parameter may be enabled for a specific query.

C | Python | Java | .NET | Go | node.js | PHP

Query - Ensuring all documents are consistent with a given mutation (at_plus)

This example should show performing a mutation, and then showing a query that is consistent with that given mutation

C | Python | Java | .NET | Go | node.js | PHP

Query - Better Reuse of Queries with adhoc(false) and Prepared Statements

This example should demonstrate best practice when a statement is to be reused heavily. Setting the adhoc N1QL query tuning to false will use Prepared Statements in the background, which is useful in such a case. Note how this works with placeholders (but of course simple statements work too).

C | Python | Java | .NET | Go | node.js

Query - UPDATE and DELETE

Show how these statements can be used to modify existing documents using secondary document attributes. Also show how a single document can be modified via the USE KEYS clause.

C | Python | .NET | Go | node.js

Full-Text (CBFT) Queries

Basic full-text example

This should show basic functionality of the FTS feature. Search for 'hoppy' in beer-sample

C | Python | .NET | Go | node.js | Java | PHP

Cluster Management

Create/Remove buckets

This should show a basic example of creating and removing a bucket. It might also be good to show how to wait until a newly created bucket becomes ready as well.

C | Python | .NET | Go | node.js | Java | PHP

Flushing a Bucket

This should show an example of how to flush a bucket.

C | Python | .NET | Go | node.js | Java | PHP

Creating N1QL Primary Index

This should show how to create the primary N1QL index on a bucket, ignoring whether it exists or not

C | Python | .NET | Go | node.js | Java | PHP

Field Encryption

Symmetric Key Encryption (AES)

An example of Symmetric Key encryption using AES-256 and SHA256 signing to encrypt fields within a document.

C | Python | .NET | Go | node.js | Java | PHP

Asymmetric Key Encryption (RSA)

An example of field field encryption using the Asymmetric Key RSA-2048 algorithm and OAEP-SHA1 padding.

C | Python | .NET | Go | node.js | Java | PHP

Development and Branching

As mentioned above, this repository has a branch per server release. The choice of a branch (versus a tag) is intentional, as samples may continue to be added after a release. This is more documentation management than software management.

In general, if adding samples for the version under development, add them to the 'master' branch. At some point of completion, this will be branched when released.

If adding a sample to an 'older' branch, please also cherry-pick it to master or other relevant branches. For instance, if one were to add a sample to server-4.6, it should also be cherry-picked to master, server-5.0, server-5.1, server-5.5. Better yet might be to add it to master and then cherry-pick it to the relevant releases.