### Example Python Version Output Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/platform-help.adoc This is an example of the output you should expect when verifying your Python installation. ```console Python 3.12.3 ``` -------------------------------- ### SDK 2 Upsert and Get Example Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/migrating-sdk-code-to-3.n.adoc Illustrates the SDK 2 approach to upserting and getting documents using specific Document types. ```python from couchbase.bucket import Bucket from couchbase.document import JsonDocument bucket = Bucket('couchbase://127.0.0.1/default') # SDK 2: Upsert a JSON document doc = JsonDocument(id='my_key', content={'name': 'Alice'}) result = bucket.upsert(doc) # SDK 2: Get a JSON document retrieved_doc = bucket.get('my_key') print(retrieved_doc.content) ``` -------------------------------- ### SDK 3 Upsert and Get Example Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/migrating-sdk-code-to-3.n.adoc Demonstrates the SDK 3 approach to upserting and getting documents using Python dictionaries, leveraging default JSON transcoding. ```python from couchbase.cluster import Cluster from couchbase.auth import PasswordAuthenticator # SDK 3: Connect to the cluster cluster = Cluster('couchbase://127.0.0.1') cluster.authenticate(PasswordAuthenticator('user', 'password')) bucket = cluster.bucket('bucket-name') # SDK 3: Upsert a JSON document (Python dictionary) collection = bucket.default_collection() result = collection.upsert('my_key', {'name': 'Alice'}) # SDK 3: Get a JSON document retrieved_doc = collection.get('my_key') print(retrieved_doc.content) ``` -------------------------------- ### Install Couchbase Python SDK Version 4.5.0 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Example of installing a specific version (4.5.0) of the Couchbase Python SDK. ```bash python3 -m pip install couchbase==4.5.0 ``` -------------------------------- ### Perform a Simple Get Operation Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/migrating-sdk-code-to-3.n.adoc Example of performing a simple 'get' operation using SDK 3.x, demonstrating the updated API structure with Cluster, Bucket, and Collection. ```python from couchbase.cluster import Cluster from couchbase.options import ClusterOptions cluster = Cluster("couchbases://localhost", ClusterOptions(authenticator=PasswordAuthenticator("user", "password"))) bucket = cluster.bucket("travel-sample") # Use default_collection() if Collections are not supported by the server # collection = bucket.default_collection() # If Collections are supported, use them like this: collection = bucket.collection("some_collection") get_result = collection.get("airline_10") ``` -------------------------------- ### Install Couchbase SDK Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/start-using-sdk.adoc Use pip to install the Couchbase SDK. Ensure you have Python 3 installed. ```bash $ sudo -H python3 -m pip install couchbase ``` -------------------------------- ### Install OpenTelemetry Libraries Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/observability-tracing.adoc Install the necessary OpenTelemetry API and SDK packages to enable tracing export. This command installs them alongside the Couchbase SDK. ```console python3 -m pip install opentelemetry-api~=1.22, opentelemetry-sdk~=1.22 ``` -------------------------------- ### Install Python SDK 3.0.0 Beta 2 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use pip to install a specific beta version of the Couchbase Python SDK. This command installs version 3.0.0b2. ```bash pip install couchbase==3.0.0b2 ``` -------------------------------- ### Install Python SDK 3.0.0 Beta 3 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use pip to install a specific beta version of the Couchbase Python SDK. This command installs version 3.0.0b3. ```bash pip install couchbase==3.0.0b3 ``` -------------------------------- ### Install Couchbase Python SDK on Windows Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Installs the Couchbase Python SDK on Windows using pip. Assumes Python is installed and the user is operating within a virtual environment. ```bash python -m pip install couchbase ``` -------------------------------- ### SDK Metadata Output Example Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/managing-connections.adoc An example of the detailed metadata output from the `get_metadata` function, showing paths to OpenSSL certificates and information about the bundled Mozilla CA certificates. ```json { ... 'mozilla_ca_bundle_date': 'Tue Jan 10 04:12:06 2023 GMT', 'mozilla_ca_bundle_embedded': True, 'mozilla_ca_bundle_sha256': 'fb1ecd641d0a02c01bc9036d513cb658bbda62a75e246bedbc01764560a639f0', 'mozilla_ca_bundle_size': 137, ... 'openssl_default_cert_dir': '/opt/homebrew/etc/openssl@1.1/certs', 'openssl_default_cert_dir_env': 'SSL_CERT_DIR', 'openssl_default_cert_file': '/opt/homebrew/etc/openssl@1.1/cert.pem', 'openssl_default_cert_file_env': 'SSL_CERT_FILE', ... } ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Beta 1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use this command to install the beta 1 version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0b1 ``` -------------------------------- ### Get Document with FlatMap in Python SDK Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/start-using-sdk.adoc This example shows how to use flatMap for more robust error handling when retrieving a document. It chains operations and stops on the first failure. ```python get_result = collection.get(doc_id).flatMap(lambda r: r.content_as[str]) ``` -------------------------------- ### Start Docker Containers Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/observability-metrics.adoc Command to start the Docker containers defined in the docker-compose.yml file in detached mode. ```console $ docker-compose up -d ``` -------------------------------- ### Install Development Tools on Debian/Ubuntu Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Installs essential development tools required for building and installing Python packages on Debian-based systems. ```bash $ sudo apt-get install git-all python3-dev python3-pip python3-setuptools build-essential ``` -------------------------------- ### Simpler Connection Example Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/start-using-sdk.adoc A simplified version of `Cluster.connect()` for scenarios where customizing the cluster environment is not required. ```python Cluster.connect() ``` -------------------------------- ### Install OpenTelemetry Libraries Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/observability-metrics.adoc Install the necessary Python libraries for OpenTelemetry integration with the Couchbase SDK. This includes the API, SDK, and the OTLP GRPC exporter. ```bash $ python3 -m pip install opentelemetry-api~=1.22, opentelemetry-sdk~=1.22 $ python3 -m pip install opentelemetry-exporter-otlp-proto-grpc~=1.22 ``` -------------------------------- ### Install Couchbase Python SDK 4.0.5 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.0.5 ``` -------------------------------- ### Install Couchbase Python SDK 4.0.1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.0.1 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.0.1 ``` -------------------------------- ### Install Couchbase Python SDK 4.1.1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.1 ``` -------------------------------- ### Upgrade PIP, Setuptools, and Wheel Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Upgrade your PIP, setuptools, and wheel installations to ensure compatibility with PEP517, which is required for installing the Couchbase Python SDK. ```console $ python3 -m pip install --upgrade pip setuptools wheel ``` -------------------------------- ### Create a Twisted Cluster Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/concurrent-async-apis.adoc Use the `txcouchbase` module for Twisted integration. This example demonstrates creating a cluster connection. ```python from txcouchbase.cluster import Cluster from couchbase.auth import PasswordAuthenticator def create_cluster(): auth = PasswordAuthenticator('user', 'password') cluster = Cluster('couchbase://localhost', authenticator=auth) return cluster ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Alpha 5 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use this command to install the alpha 5 version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0a5 ``` -------------------------------- ### Install Couchbase Python SDK 4.1.0 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.0 ``` -------------------------------- ### Install Couchbase Python SDK 4.1.2 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.2 ``` -------------------------------- ### Install Development Tools on RHEL/CentOS Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Installs essential development tools, including GCC and Python development headers, required for building and installing Python packages on RHEL-based systems. ```bash $ sudo yum install gcc gcc-c++ python3-devel python3-pip ``` -------------------------------- ### Install Couchbase Python SDK v4.1.5 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install version 4.1.5 of the Couchbase Python SDK using pip. ```bash python3 -m pip install couchbase==4.1.5 ``` -------------------------------- ### Install Couchbase Encryption Library Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/encrypting-using-sdk.adoc Install the optional encryption library using pip. Ensure you meet the version requirements for both the SDK and the encryption library. ```bash python3 -m pip install cbencryption ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Alpha 6 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use this command to install the alpha 6 version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0a6 ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Alpha 2 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use pip to install a specific alpha version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0a2 ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Alpha 1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use pip to install a specific alpha version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0a1 ``` -------------------------------- ### Install Couchbase Python SDK 4.1.3 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.1.3 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.3 ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Alpha 3 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use pip to install a specific alpha version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0a3 ``` -------------------------------- ### Install Couchbase Python SDK 4.3.1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```console $ python3 -m pip install couchbase==4.3.1 ``` -------------------------------- ### Install Couchbase Python SDK 4.1.7 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install version 4.1.7 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.7 ``` -------------------------------- ### Create an asyncio Cluster Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/concurrent-async-apis.adoc Use the `acouchbase` module for asyncio integration. This example shows how to create a cluster connection. ```python from acouchbase.cluster import Cluster from couchbase.auth import PasswordAuthenticator async def create_cluster(): auth = PasswordAuthenticator('user', 'password') cluster = await Cluster('couchbase://localhost', authenticator=auth) return cluster ``` -------------------------------- ### Install Couchbase Python SDK 3.0.0 Alpha 4 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/3.0-pre-release-notes.adoc Use pip to install a specific alpha version of the Couchbase Python SDK. ```bash pip install couchbase==3.0.0a4 ``` -------------------------------- ### Install Couchbase Python SDK 4.2.1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install version 4.2.1 of the Couchbase Python SDK using pip. ```console $ python3 -m pip install couchbase==4.2.1 ``` -------------------------------- ### Install Couchbase Python SDK 4.3.2 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```console $ python3 -m pip install couchbase==4.3.2 ``` -------------------------------- ### Install Couchbase Python SDK 4.3.3 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install a specific version of the Couchbase Python SDK using pip. ```console $ python3 -m pip install couchbase==4.3.3 ``` -------------------------------- ### Install Couchbase Python SDK v4.4.0 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Use pip to install a specific version of the Couchbase Python SDK. ```bash $ python3 -m pip install couchbase==4.4.0 ``` -------------------------------- ### Install Couchbase Python SDK v4.3.5 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.3.5 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.3.5 ``` -------------------------------- ### Verify Python Installation Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/platform-help.adoc Use this command to check your installed Python version. The '3' may not be necessary on all systems, but 'python3' works universally. ```console $ python3 --version ``` -------------------------------- ### Install Couchbase Python SDK Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/overview.adoc Command to install the Couchbase Python SDK using pip. This is the standard method for adding the SDK to your Python project. ```bash $ python3 -m pip install couchbase ``` -------------------------------- ### Start Jaeger Docker Container Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/observability-tracing.adoc Start the Jaeger service using Docker Compose. This command launches Jaeger with OTLP support, making it ready to receive and display tracing data. ```console docker-compose up -d ``` -------------------------------- ### Creating a cluster with custom settings Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/ref/pages/client-settings.adoc Demonstrates how to create a cluster using custom client settings with `ClusterOptions` and `PasswordAuthenticator`. ```APIDOC ## Creating a cluster with custom settings ### Description This example shows how to instantiate a `Cluster` object with custom client settings by utilizing the `ClusterOptions` class and providing a `PasswordAuthenticator`. ### Method `Cluster.connect` ### Endpoint N/A (SDK Method) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None (Configuration is passed via arguments) ### Request Example ```python # Create a cluster using the custom client settings. cluster = Cluster.connect(connectionString, ClusterOptions( PasswordAuthenticator(username, password), option1=xxx, option2=yyy...)) ``` ### Response #### Success Response (200) Returns a `Cluster` object configured with the specified options. #### Response Example ```json { "example": "Cluster object" } ``` ``` -------------------------------- ### Install Couchbase Python SDK 4.6.1 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Use this command to install a specific patch version of the Python SDK. Always recommended to use the latest version for security patches and new features. ```bash $ python3 -m pip install couchbase==4.6.1 ``` -------------------------------- ### Get Document with For-Comprehension in Python SDK Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/start-using-sdk.adoc This example demonstrates using a for-comprehension for error handling when retrieving a document. It provides an alternative to flatMap for chaining operations. ```python get_result = collection.get(doc_id) for_comp_result = ( for r in get_result.content_as[str] yield r ) ``` -------------------------------- ### Install Couchbase Python SDK 4.6.2 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Use this command to install a specific patch version of the Python SDK. Always recommended to use the latest version for security patches and new features. ```bash $ python3 -m pip install couchbase==4.6.2 ``` -------------------------------- ### Create a Bucket Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/provisioning-cluster-resources.adoc Use the BucketManager to create a new bucket with specified settings. Ensure all required parameters like name and ram_quota_mb are provided. ```python bucket_settings = BucketSettings(bucket_name, ram_quota_mb=100, bucket_type=BucketType.COUCHBASE, num_replicas=1) bucket_manager.create_bucket(bucket_settings) ``` -------------------------------- ### Perform Global vs. Scoped Search Queries Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/full-text-searching-with-sdk.adoc Demonstrates how to initiate search queries. Use `Cluster.searchQuery()` for global indexes and `Scope.search()` for scoped indexes. ```python search_query = SearchQuery(MatchAllQuery()) cluster.search_query("my_index", search_query) scope.search("my_scoped_index", search_query) ``` -------------------------------- ### User Signup Code (Python) Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/sample-application.adoc Handles user signup by creating a document with a lowercase username as the document key. ```python def signup(username, password): key = username.lower() doc = { "type": "user", "password": password } try: bucket.default_collection().upsert(key, doc) return True except Exception as e: print(e) return False ``` -------------------------------- ### Install Couchbase Python SDK 4.0.0 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.0.0 of the Couchbase Python SDK using pip. ```console $ python3 -m pip install couchbase==4.0.0 ``` -------------------------------- ### Define Tenant User Signup Endpoint Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/hello-world/pages/sample-application.adoc Defines the signup endpoint for users within a specific tenant. It handles K/V insert into the users collection. ```python @ns.route('/user/signup') class UserSignup(Resource): """User signup endpoint.""" @ns.expect(user_model) def post(self): """Sign up a new user.""" username = request.json.get('username') password = request.json.get('password') if not username or not password: return {"message": "Username and password are required"}, 400 try: tenant_agent = app.config["tenant_agent_00"] users_collection = tenant_agent.bucket("users").scope("user").collection("users") users_collection.upsert(username, {"username": username, "password": password}) return {"message": "User created successfully"}, 201 except Exception as e: return {"message": str(e)}, 500 ``` -------------------------------- ### Create a Bucket using Bucket Manager Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/concept-docs/pages/buckets-and-clusters.adoc Use the `buckets()` call on the cluster object to get a BucketManager instance, then use `createBucket()` to provision a new bucket. The `BucketSettings` object is required for this operation. ```java Cluster cluster = Cluster.connect("127.0.0.1", "user", "123456"); BucketManager manager = cluster.buckets(); manager.createBucket(bucketSettings); ``` -------------------------------- ### Execute Simple N1QL Query (Asyncio) Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/.n1ql-queries-with-sdk.adoc Demonstrates a basic N1QL query using the asyncio API. Ensure the `ACluster` object is initialized before use. ```python cluster = ACluster("couchbase://localhost") await cluster.query("SELECT count(*) FROM `travel-sample`") ``` -------------------------------- ### Install Couchbase Python SDK 4.1.4 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.1.4 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.4 ``` -------------------------------- ### Connect using DNS SRV Records (Python) Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/managing-connections.adoc Enable DNS SRV bootstrapping and provide the hostname from your SRV records to connect to the cluster. Requires SDK version 2.5+. ```python cluster = Cluster.connect("couchbase://example.com", ClusterOptions(PasswordAuthenticator("user", "password"))) ``` -------------------------------- ### Install Couchbase Python SDK v4.1.6 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install version 4.1.6 of the Couchbase Python SDK using pip. ```bash python3 -m pip install couchbase==4.1.6 ``` -------------------------------- ### Install Couchbase Python SDK 4.1.8 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install version 4.1.8 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.1.8 ``` -------------------------------- ### N1QL Query with Named Parameter and Options Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/.n1ql-queries-with-sdk.adoc Demonstrates using a named parameter with additional query options. Options are passed as keyword arguments. ```python result = cluster.query("SELECT " + """FROM `travel-sample` " + "WHERE airlinecode = $code LIMIT 2""", code="AA", scan_consistency=ScanConsistency.REQUEST_PLUS) ``` -------------------------------- ### Install Couchbase Python SDK 4.3.0 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Install version 4.3.0 of the Couchbase Python SDK using pip. ```console $ python3 -m pip install couchbase==4.3.0 ``` -------------------------------- ### Simple SQL++ Query SDK 2 vs SDK 3 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/migrating-sdk-code-to-3.n.adoc Compares the syntax for executing a simple SQL++ query in SDK 2 versus SDK 3. SDK 3 moves query execution to the Cluster level. ```python # SDK 2 result = bucket.n1ql_query("SELECT count(*) FROM `travel-sample`") ``` ```python # SDK 3 result = await cluster.query("SELECT count(*) FROM `travel-sample`") ``` -------------------------------- ### Configure Certificate-Based Authentication Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/migrating-sdk-code-to-3.n.adoc Set up certificate-based authentication by providing the path to the certificate and key files. ```python from couchbase.auth import CertificateAuthenticator cluster = Cluster("couchbases://127.0.0.1") cluster.authenticate(CertificateAuthenticator("/path/to/cert.pem", "/path/to/key.pem")) ``` -------------------------------- ### Create Deferred N1QL Indexes and Build Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/concept-docs/pages/n1ql-query.adoc Create indexes with the `defer_build` option to build them in the background, allowing multiple deferred indexes to be built together efficiently. ```sql CREATE PRIMARY INDEX ON `travel-sample`.inventory.hotel WITH {"defer_build": true}; CREATE INDEX ix_name ON `travel-sample`.inventory.hotel(name) WITH {"defer_build": true}; CREATE INDEX ix_email ON `travel-sample`.inventory.hotel(email) WITH {"defer_build": true}; BUILD INDEX ON `travel-sample`.inventory.hotel(`#primary`, `ix_name`, `ix_email`); ``` -------------------------------- ### Execute Simple SQL++ Query (Asyncio) Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/sqlpp-queries-with-sdk.adoc Demonstrates executing a simple SQL++ query using the asyncio API. Ensure you have an `ACluster` instance configured. ```python async def simple_query(cluster: ACluster): try: result = await cluster.query("SELECT " + "'Hello' AS greeting") print(f"Query result: {result.rows}") except Exception as e: print(f"Query failed: {e}") ``` -------------------------------- ### Create Cluster with Custom Client Settings Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/ref/pages/client-settings.adoc Use `ClusterOptions` to create a cluster instance with custom client settings, such as authentication and other options. ```python # Create a cluster using the custom client settings. cluster = Cluster.connect(connectionString, ClusterOptions( PasswordAuthenticator(username, password), option1=xxx, option2=yyy...)) ``` -------------------------------- ### Install Couchbase Python SDK v4.3.4 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.3.4 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.3.4 ``` -------------------------------- ### Read Your Own Writes Example Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/distributed-acid-transactions-from-the-sdk.adoc Demonstrates the 'Read Your Own Writes' capability within transactions, ensuring that a document inserted or modified within the same transaction can be immediately read. ```python result = await transaction.insert(collection, "my_doc_id", {"content": "initial content"}) result = await transaction.get(collection, "my_doc_id") assert result.content_as[dict]["content"] == "initial content" ``` -------------------------------- ### Fetch a KeyValue Document with SDK 3 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/migrating-sdk-code-to-3.n.adoc Demonstrates how to fetch a document using the default collection in SDK API 3. Includes options for setting a timeout. ```python from datetime import timedelta from couchbase.cluster import Cluster from couchbase.collection import GetOptions cluster=Cluster.connect("couchbases://10.192.1.104") collection=cluster.default_collection() get_result = collection.get("key", GetOptions(timeout=timedelta(seconds=3))) ``` -------------------------------- ### Install Couchbase Python SDK 4.6.0 Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-release-notes.adoc Installs version 4.6.0 of the Couchbase Python SDK using pip. ```bash $ python3 -m pip install couchbase==4.6.0 ``` -------------------------------- ### GET Endpoint for Caching Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/caching-example.adoc Implement a GET endpoint to retrieve data from Couchbase. Handles DocumentNotFoundException for cache misses. ```python from flask import Flask, request, jsonify from couchbase.cluster import Cluster from couchbase.exceptions import DocumentNotFoundException, CouchbaseException app = Flask(__name__) # Connect to Couchbase cluster = Cluster('couchbase://localhost') bucket = cluster.bucket('your_bucket_name') collection = bucket.default_collection() @app.route('/cache/', methods=['GET']) def get_data(key): try: result = collection.get(key) return jsonify(result.content) except DocumentNotFoundException: return jsonify({'error': 'Not Found'}), 404 except CouchbaseException as e: return jsonify({'error': str(e)}), 500 ``` -------------------------------- ### Build Deferred N1QL Indexes Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/concept-docs/pages/n1ql-query.adoc Creates N1QL indexes asynchronously and then builds them. Use this when you need to create multiple indexes without blocking the application thread. ```csharp var bucketManager = bucket.CreateManager(); await bucketManager.CreateN1qlPrimaryIndexAsync(defer = true); await bucketManager.CreateN1qlIndexAsync("ix_name", defer = true, new string[] { "name" }); await bucketManager.CreateN1qlIndexAsync("ix_email", defer = true, new string[] { "email" }); await bucketManager.BuildN1qlDeferredIndexesAsync(); await bucketManager.WatchN1qlIndexes(new List { "ix_name", "ix_email", "#primary"}, TimeSpan.FromSeconds(2)); ``` -------------------------------- ### Install Python 3 with Homebrew Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Install a compatible version of Python 3 using the Homebrew package manager on macOS. ```console $ brew install python3 ``` -------------------------------- ### Get and Update Bucket Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/provisioning-cluster-resources.adoc Retrieves bucket details and updates its settings. Ensure the bucket exists before attempting to get or update it. ```python cluster.bucket("myBucket").get_and_update_bucket( lambda settings: settings.set_max_size_per_node(20000000000) ) ``` -------------------------------- ### Create and populate an array document Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/howtos/pages/subdocument-operations.adoc Initialize a document with an empty array and then use the empty path (`""`) for subsequent sub-document array operations. This is useful when the document's primary content is an array. ```python collection.mutate_in( document_id, ( mutate_in.array_append("", "first_element"), mutate_in.array_append("", "second_element"), ) ) ``` -------------------------------- ### Activate Conda Environment Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/project-docs/pages/sdk-full-installation.adoc Activate the newly created Conda environment before installing the SDK. This ensures packages are installed in the correct isolated environment. ```bash conda activate test_env ``` -------------------------------- ### Create N1QL Indexes using SDK (C#) Source: https://github.com/couchbase/docs-sdk-python/blob/release/4.6/modules/concept-docs/pages/n1ql-query.adoc Manage N1QL indexes programmatically using the Couchbase SDK. This includes creating primary and secondary indexes. ```csharp var bucketManager = bucket.CreateManager(); await bucketManager.CreateN1qlPrimaryIndexAsync(); await bucketManager.CreateN1qlIndexAsync("index_name", new string[] { "name" }) await bucketManager.CreateN1qlIndexAsync("index_emai", new string[] { "email" }) ```