Develop C++ Apps
- installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. Ifnot please follow these steps in the .
- have C++11.
We use the driver. To install the library do the following:
- Clone the
cpp_redis
repository
- Get the networking module ()
$ cd cpp_redis
$ git submodule init && git submodule update
- Create a build directory and move into it
$ mkdir build && cd build
- Generate the Makefile using CMake
- Build and install the library
$ make
$ make install
#include <cpp_redis/cpp_redis>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
cpp_redis::client client;
if (status == cpp_redis::client::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});
string userid = "1";
vector<pair<string, string>> userProfile;
userProfile.push_back(make_pair("name", "John"));
userProfile.push_back(make_pair("age", "35"));
userProfile.push_back(make_pair("language", "Redis"));
// Insert the data
client.hmset(userid, userProfile, [](cpp_redis::reply& reply) {
cout<< "HMSET returned " << reply << ": id=1, name=John, age=35, language=Redis" << endl;
});
client.hgetall(userid, [](cpp_redis::reply& reply) {
if (reply.is_array()) {
retVal = reply.as_array();
}
cout << "Query result:" <<endl;
for (int i = 0; i < retVal.size(); i=i+2) {
cout << retVal[i] << "=" <<retVal[i+1] << endl;
}
});
// synchronous commit, no timeout
client.sync_commit();
return 0;
}
To compile the file, run the following command.
$ ./ybredis_hello_world
You should see the following output.
HMSET returned OK: id=1, name=John, age=35, language=Redis
Query result:
age=35
name=John