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 ()
    1. $ cd cpp_redis
    2. $ git submodule init && git submodule update
    • Create a build directory and move into it
    1. $ mkdir build && cd build
    • Generate the Makefile using CMake
    • Build and install the library
    1. $ make
    2. $ make install
    1. #include <cpp_redis/cpp_redis>
    2. #include<iostream>
    3. #include<vector>
    4. #include<string>
    5. using namespace std;
    6. int main() {
    7. cpp_redis::client client;
    8. if (status == cpp_redis::client::connect_state::dropped) {
    9. std::cout << "client disconnected from " << host << ":" << port << std::endl;
    10. }
    11. });
    12. string userid = "1";
    13. vector<pair<string, string>> userProfile;
    14. userProfile.push_back(make_pair("name", "John"));
    15. userProfile.push_back(make_pair("age", "35"));
    16. userProfile.push_back(make_pair("language", "Redis"));
    17. // Insert the data
    18. client.hmset(userid, userProfile, [](cpp_redis::reply& reply) {
    19. cout<< "HMSET returned " << reply << ": id=1, name=John, age=35, language=Redis" << endl;
    20. });
    21. client.hgetall(userid, [](cpp_redis::reply& reply) {
    22. if (reply.is_array()) {
    23. retVal = reply.as_array();
    24. }
    25. cout << "Query result:" <<endl;
    26. for (int i = 0; i < retVal.size(); i=i+2) {
    27. cout << retVal[i] << "=" <<retVal[i+1] << endl;
    28. }
    29. });
    30. // synchronous commit, no timeout
    31. client.sync_commit();
    32. return 0;
    33. }

    To compile the file, run the following command.

    1. $ ./ybredis_hello_world

    You should see the following output.

    1. HMSET returned OK: id=1, name=John, age=35, language=Redis
    2. Query result:
    3. age=35
    4. name=John