You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorial/markdown/c++/cxx-quickstart.md
+15-16Lines changed: 15 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ path: "/tutorial-cxx-quickstart"
4
4
title: Quickstart in Couchbase with C++
5
5
short_title: C++ Quickstart
6
6
description:
7
-
- Learn to to use Couchbase C++ SDK to interact with the database.
7
+
- Learn to use the Couchbase C++ SDK to interact with the database.
8
8
- See how you can fetch data from Couchbase using SQL++ queries
9
9
- Explore how you can perform search operations using Search indexes.
10
10
- Explore CRUD operations in action with Couchbase
@@ -23,10 +23,9 @@ sdk_language:
23
23
length: 30 Mins
24
24
---
25
25
26
-
<!-- [abstract] -->
27
-
# Quickstart in Couchbase with C++
26
+
## Introduction
28
27
29
-
In this tutorial, you will learn how to connect to a Couchbase Capella cluster to create, read, update, and delete documents, how to write simple parametrized SQL++ queries and how to create, and perform simple and complex text search using Search indexes.
28
+
In this tutorial, you will learn how to connect to a Couchbase Capella cluster to create, read, update, and delete documents, how to write simple parameterized SQL++ queries, and how to create and perform simple and complex search operations using Search indexes.
30
29
31
30
## Prerequisites
32
31
@@ -61,7 +60,7 @@ Specifically, you need to do the following:
61
60
- Create the [database credentials](https://docs.couchbase.com/cloud/clusters/manage-database-users.html) to access the travel-sample bucket (Read and Write) used in the application.
62
61
-[Allow access](https://docs.couchbase.com/cloud/clusters/allow-ip-address.html) to the Cluster from the IP on which the application is running.
63
62
64
-
All configuration for communication with the database is read from the environment variables. We have provided a convenience feature in this quickstart to setup the required environment variables using a shell script `setup_env_vars.sh`. Change the values of the following lines:
63
+
All configuration for communication with the database is read from the environment variables. We have provided a convenience feature in this quickstart to setup the required environment variables using a shell script `set_env_vars.sh`. Change the values of the following lines:
This will set the environment variables for that session.
81
80
82
81
### Install Dependencies and Building
83
82
84
-
This project makes use of CMake and CPM to install dependencies.
83
+
This project makes use of CMake and CPM to install dependencies.
85
84
86
85
87
86
```sh
@@ -91,7 +90,7 @@ cmake ..
91
90
cmake --build .
92
91
```
93
92
94
-
This will download and install all the dependencies required for the project to built. Along with that it will build the executable required to run the application.
93
+
This will download and install all the dependencies required for the project to be built. Along with that it will build the executable required to run the application.
95
94
96
95
## Running The Application
97
96
@@ -127,12 +126,12 @@ This quickstart utilizes two collections: **airline** and **hotel**. The **airli
127
126
128
127
## Code Review
129
128
130
-
To begin this tutorial, clone the repo and open it up in the IDE of your choice. Now you can explore about how to interact with Couchbase Server using the C++ SDK.
129
+
To begin this tutorial, clone the repo and open it up in the IDE of your choice. Now you can explore how to interact with Couchbase Server using the C++ SDK.
131
130
132
-
We have separated out the SDK code and the main function. The `db.h` and `db.cpp` contain the declaration and the implementation of utility functions we will use to parse environment variables and create a connection to the cluster. `operations.h` and `operations.cpp` contain all the functions that perform operations on the db. Both `db.cpp` and `operations.cpp` are combined to make a static library. The tests are similarly separated out in the `tests` folder which utilise the library created earlier. The `main.cpp` is the executable which is also built by linking the library and contains code that demonstrates the usage of the functions we defined earlier to initeract with the db.
131
+
We have separated out the SDK code and the main function. The `db.h` and `db.cpp` contain the declaration and the implementation of utility functions we will use to parse environment variables and create a connection to the cluster. `operations.h` and `operations.cpp` contain all the functions that perform operations on the database. Both `db.cpp` and `operations.cpp` are combined to make a static library. The tests are similarly separated out in the `tests` folder which utilize the library created earlier. The `main.cpp` is the executable which is also built by linking the library and contains code that demonstrates the usage of the functions we defined earlier to interact with the database.
133
132
134
133
### Connecting to the Cluster
135
-
In `db.h`, we include the required header files to work with C++ SDK in order to for implement the functions required to initialize the DB. In the `db.cpp` we implement the functions that help us connect to the db. We begin by implementing few utility functions that will help us later. The `parseEnvironmentVariables` serves as a utility to get the values set for a list of environment variables. This enables us to get the connection parameters and credentials, set by running `source set_env_vars.sh`. Following this `checkScopeAndColExists` and `checkSearchEnabled` are implemented, used to check for existence of scope and collection of given name and to check if search service enabled respectively. Finally we have the InitCluster function which returns the connection objects as a tuple.
134
+
In `db.h`, we include the required header files to work with C++ SDK in order to implement the functions required to initialize the database. In the `db.cpp` we implement the functions that help us connect to the database. We begin by implementing a few utility functions that will help us later. The `parseEnvironmentVariables` serves as a utility to get the values set for a list of environment variables. This enables us to get the connection parameters and credentials set by running `source set_env_vars.sh`. Following this,`checkScopeAndColExists` and `checkSearchEnabled` are implementedto check for the existence of a scope and collection of a given name and to verify if the search service is enabled respectively. Finally we have the InitCluster function which returns the connection objects as a tuple.
Operations for interacting with the db is defined and implemented in `operations.h` and `operations.cpp`
166
+
Operations for interacting with the database are defined and implemented in `operations.h` and `operations.cpp`.
168
167
### Insert Document
169
168
Insert function is the equivalent of the POST request and can be used to insert new documents to the collection. We can pass the document to be inserted as a JSON string or as a JSON file path, the function takes in file_flag which is used to differentiate between the two.
170
169
- The value gets converted to the type `tao::json::value` and inserts it to the collection if `file_flag=false`
Read function is equivalent to GET requests and can be used fetch documents using the `doc_id`.
198
+
Read function is equivalent to GET requests and can be used to fetch documents using the `doc_id`.
200
199
- First checks if the document exists using `col.exists(doc_id)`.
201
200
- If the document exists, it retrieves the document's content using `col.get(doc_id)` and returns it after converting it to `tao::json::value` for easier usage on return.
202
201
- If an error occurs (e.g., document not found), it prints an error message and returns an empty tao::json::value object.
@@ -231,7 +230,7 @@ auto res = Delete(col, doc_id);
231
230
We can use the `Query` function to execute any N1QL (SQL++) query on a scope.
232
231
- Executes the N1QL query using the provided `scope.query(query, opts)`.
233
232
- Returns the result of the query if successful. The result is added to a `std::vector<std::string>` object that contains the `id, country, avg_rating, title`.
234
-
- We can pass `opts` parameter, which can be used to insert positonal parameters in the query.
233
+
- We can pass `opts` parameter, which can be used to insert positional parameters in the query.
235
234
- If there is an error, it prints an error message and returns an empty result object.
The `Filter` function aims to demo the construction and execution of a `conjuction_query` which can be described as an `AND` operation on two or more types of filters. This particular implementation performs a conjunction on `couchbase::match_query("United States").field("country")` and `couchbase::term_query("San Diego").field("city")`.
299
+
The `Filter` function aims to demo the construction and execution of a `conjunction_query` which can be described as an `AND` operation on two or more types of filters. This particular implementation performs a conjunction on `couchbase::match_query("United States").field("country")` and `couchbase::term_query("San Diego").field("city")`.
0 commit comments