Skip to content

Nebula CPP

Nebula CPP is a C++ client for connecting to and managing the Nebula Graph database.

Prerequisites

  • You have installed C++ and GCC 4.8 or later versions.

Compatibility with Nebula Graph

Nebula Graph version Nebula CPP version
2.5.0 2.5.0
2.0.1 2.0.0
2.0.0 2.0.0

Install Nebula CPP

  1. Clone the Nebula CPP source code to the host.

    • (Recommended) To install a specific version of Nebula CPP, use the Git option --branch to specify the branch. For example, to install v2.5.0, run the following command:

      $ git clone --branch v2.5.0 https://github.com/vesoft-inc/nebula-cpp.git
      
    • To install the daily development version, run the following command to download the source code from the master branch:

      $ git clone https://github.com/vesoft-inc/nebula-cpp.git
      
  2. Change the working directory to nebula-cpp.

    $ cd nebula-cpp
    
  3. Create a directory named build and change the working directory to it.

    $ mkdir build && cd build
    
  4. Generate the makefile file with CMake.

    Note

    The default installation path is /usr/local/nebula. To modify it, add the -DCMAKE_INSTALL_PREFIX=<installation_path> option while running the following command.

    $ cmake -DCMAKE_BUILD_TYPE=Release ..
    

    Note

    If G++ does not support C++ 11, add the option -DDISABLE_CXX11_ABI=ON.

  5. Compile Nebula CPP.

    To speed up the compiling, use the -j option to set a concurrent number N. It should be \(\min(\text{CPU}core number,\frac{the_memory_size(GB)}{2})\).

    $ make -j{N}
    
  6. Install Nebula CPP.

    $ sudo make install
    
  7. Update the dynamic link library.

    $ sudo ldconfig
    

Use Nebula CPP

Compile the CPP file to an executable file, then you can use it. The following steps take using SessionExample.cpp for example.

  1. Use the example code to create the SessionExample.cppfile.

  2. Run the following command to compile the file.

    $ LIBRARY_PATH=<library_folder_path>:$LIBRARY_PATH g++ -std=c++11 SessionExample.cpp -I<include_folder_path> -lnebula_graph_client -o session_example
    
    • library_folder_path: The storage path of the Nebula Graph dynamic libraries. The default path is /usr/local/nebula/lib64.
    • include_folder_path: The storage of the Nebula Graph header files. The default path is /usr/local/nebula/include.

For example:

$ LIBRARY_PATH=/usr/local/nebula/lib64:$LIBRARY_PATH g++ -std=c++11 SessionExample.cpp -I/usr/local/nebula/include -lnebula_graph_client -o session_example

Core of the example code

This sub-section shows the core of the example code. For all the code, see SessionExample.

nebula::init(&argc, &argv);
auto address = "192.168.xx.1:9669";
nebula::ConnectionPool pool;
pool.init({address}, nebula::Config{});
auto session = pool.getSession("root", "nebula");

auto result = session.execute("SHOW HOSTS");
std::cout << *result.data;

std::atomic_bool complete{false};
session.asyncExecute("SHOW HOSTS", [&complete](nebula::ExecutionResponse&& cbResult) {
    std::cout << *cbResult.data;
    complete.store(true);
});
session.release();

Last update: September 2, 2021
Back to top