TuGraph C++ SDK

This document is the usage instruction of TuGraph C++ SDK

1.Instructions

C++ Client can use RPC to connect to lgraph_server to import data, execute stored procedures, call Cypher and other operations.

2.Demo

2.1.Instantiate the client object

Introduce dependencies and instantiate

2.1.1. Instantiate a single node client object

When starting the server in single-node mode, the client is instantiated in the following format

RpcClient client("127.0.0.1:19099", "admin", "73@TuGraph");
RpcClient(const std::string& url, const std::string& user, const std::string& password);
@param url: tugraph host looks like ip:port
@param user: login user name
@param password: login password

2.1.2. Instantiate the HA cluster to directly connect to the client object

When the HA cluster deployed on the server can be directly connected using the URL configured in ha_conf, the client is instantiated according to the following format

RpcClient client("127.0.0.1:19099", "admin", "73@TuGraph");
RpcClient(const std::string& url, const std::string& user, const std::string& password);
@param url: tugraph host looks like ip:port
@param user: login user name
@param password: login password

The user only needs to pass in the url of any node in the HA cluster, and the client will automatically maintain the connection pool based on the query information returned by the server, and there is no need to manually restart the client when the HA cluster expands horizontally.

2.1.3. Instantiate the HA cluster to indirectly connect to the client object

When the HA cluster deployed on the server cannot use the URL configured in ha_conf to connect directly but must use an indirect URL (such as the Alibaba Cloud public network URL), the client is instantiated according to the following format.

std::vector<std::string> urls = {"189.33.97.23:9091", "189.33.97.24:9091", "189.33.97.25:9091"};
TuGraphDbRpcClient client = new TuGraphDbRpcClient(urls, "admin", "73@TuGraph");
RpcClient(std::vector<std::string>& urls, std::string user, std::string password)
@param urls: tugraph host list
@param user: login user name
@param password: login password

Because the URL that the user connects to is different from the information configured when the server starts, the client connection pool cannot be automatically updated by sending a request to the cluster, so it is necessary to manually pass in the URLs of all nodes in the cluster when starting the client, and when the cluster node changes Manually restart the client.

2.2.Call cypher

    std::string str;
    bool ret = client.CallCypher(str,
        "CALL db.createVertexLabel('actor', 'name', 'name', string, false, 'age', int8, true)");
    bool CallCypher(std::string& result, const std::string& cypher,
                    const std::string& graph = "default", bool json_format = true,
                    double timeout = 0, const std::string& url = "");
    @param [out] result      The result.
    @param [in]  cypher      inquire statement.
    @param [in]  graph       (Optional) the graph to query.
    @param [in]  json_format (Optional) Returns the format, true is json,Otherwise, binary
                             format.
    @param [in]  timeout     (Optional) Maximum execution time, overruns will be interrupted.
    @param [in]  url         (Optional) Node address of calling cypher.
    @returns True if it succeeds, false if it fails.

Among them, in the client in HA mode, by specifying the url parameter, a read request can be directed to a certain server.

2.3.Call stored procedure

    std::string str;
    bool ret = client.CallProcedure(str, "CPP", "test_plugin1", "bcefg");
    bool CallProcedure(std::string& result, const std::string& procedure_type,
                       const std::string& procedure_name, const std::string& param,
                       double procedure_time_out = 0.0, bool in_process = false,
                       const std::string& graph = "default", bool json_format = true,
                       const std::string& url = "");
    @param [out] result              The result.
    @param [in]  procedure_type      the procedure type, currently supported CPP and PY.
    @param [in]  procedure_name      procedure name.
    @param [in]  param               the execution parameters.
    @param [in]  procedure_time_out  (Optional) Maximum execution time, overruns will be
                                     interrupted.
    @param [in]  in_process          (Optional) support in future.
    @param [in]  graph               (Optional) the graph to query.
    @param [in]  json_format         (Optional) Returns the format, true is json,Otherwise,
                                     binary format.
    @param [in]  url                 (Optional) Node address of calling procedure.
    @returns True if it succeeds, false if it fails.

Among them, in the client in HA mode, by specifying the url parameter, a read request can be directed to a certain server.

2.4.Load stored procedure

    std::string str;
    bool ret = client.LoadProcedure(str, code_sleep, "PY", "python_plugin1", "PY", "this is a test plugin", true)
    bool LoadProcedure(std::string& result, const std::string& source_file,
                       const std::string& procedure_type, const std::string& procedure_name,
                       const std::string& code_type, const std::string& procedure_description,
                       bool read_only, const std::string& version = "v1",
                       const std::string& graph = "default");
    @param [out] result                  The result.
    @param [in]  source_file             the source_file contain procedure code.
    @param [in]  procedure_type          the procedure type, currently supported CPP and PY.
    @param [in]  procedure_name          procedure name.
    @param [in]  code_type               code type, currently supported PY, SO, CPP, ZIP.
    @param [in]  procedure_description   procedure description.
    @param [in]  read_only               procedure is read only or not.
    @param [in]  version                 (Optional) the version of procedure.
    @param [in]  graph                   (Optional) the graph to query.
    @returns True if it succeeds, false if it fails.

2.5.List stored procedures

    std::string str;
    bool ret = client.ListProcedures(str);
    bool ListProcedures(std::string& result, const std::string& procedure_type,
                        const std::string& version = "any",
                        const std::string& graph = "default", const std::string& url = "");
    @param [out] result          The result.
    @param [in]  procedure_type  (Optional) the procedure type, "" for all procedures,
                                 CPP and PY for special type.
    @param [in]  version         (Optional) the version of procedure.
    @param [in]  graph           (Optional) the graph to query.
    @param [in]  url             Node address of calling procedure.
    @returns True if it succeeds, false if it fails.

Among them, in the client in HA mode, by specifying the url parameter, the request can be directed to a certain server.

2.6.Delete stored procedure

    std::string str;
    bool ret = client.DeleteProcedure(str, "CPP", "test_plugin1");
    bool DeleteProcedure(std::string& result, const std::string& procedure_type,
                         const std::string& procedure_name, const std::string& graph = "default");
    @param [out] result              The result.
    @param [in]  procedure_type      the procedure type, currently supported CPP and PY.
    @param [in]  procedure_name      procedure name.
    @param [in]  graph               (Optional) the graph to query.
    @returns True if it succeeds, false if it fails.

2.7.Import from a byte stream schema

    std::string str;
    bool ret = client.ImportSchemaFromContent(str, sImportContent["schema"]);
    bool ImportSchemaFromContent(std::string& result, const std::string& schema,
                                 const std::string& graph = "default", bool json_format = true,
                                 double timeout = 0);
    @param [out] result      The result.
    @param [in]  schema      the schema to be imported.
    @param [in]  graph       (Optional) the graph to query.
    @param [in]  json_format (Optional) Returns the format, true is json,Otherwise, binary
                             format.
    @param [in]  timeout     (Optional) Maximum execution time, overruns will be interrupted.
    @returns True if it succeeds, false if it fails.

2.8.Import node and edge data from a byte stream

    std::string str;
    ret = client.ImportDataFromContent(str, sImportContent["person_desc"], sImportContent["person"],",");
    bool ImportDataFromContent(std::string& result, const std::string& desc,
                               const std::string& data, const std::string& delimiter,
                               bool continue_on_error = false, int thread_nums = 8,
                               const std::string& graph = "default", bool json_format = true,
                               double timeout = 0);
    @param [out] result              The result.
    @param [in]  desc                data format description.
    @param [in]  data                the data to be imported.
    @param [in]  delimiter           data separator.
    @param [in]  continue_on_error   (Optional) whether to continue when importing data fails.
    @param [in]  thread_nums         (Optional) maximum number of threads.
    @param [in]  graph               (Optional) the graph to query.
    @param [in]  json_format         (Optional) Returns the format, true is json,Otherwise,
                                     binary format.
    @param [in]  timeout             (Optional) Maximum execution time, overruns will be
                                     interrupted.
    @returns True if it succeeds, false if it fails.

2.9.Import schema from a file

    std::string conf_file("./yago.conf");
    std::string str;
    ret = client.ImportSchemaFromFile(str, conf_file);
    bool ImportSchemaFromFile(std::string& result, const std::string& schema_file,
                              const std::string& graph = "default", bool json_format = true,
                              double timeout = 0);
    @param [out] result      The result.
    @param [in]  schema_file the schema_file contain schema.
    @param [in]  graph       (Optional) the graph to query.
    @param [in]  json_format (Optional) Returns the format, true is json,Otherwise, binary
                             format.
    @param [in]  timeout     (Optional) Maximum execution time, overruns will be interrupted.
    @returns True if it succeeds, false if it fails.

2.10.Import edge data from a file

    std::string conf_file("./yago.conf");
    std::string str;
    ret = client.ImportDataFromFile(str, conf_file, ",");
    bool ImportDataFromFile(std::string& result, const std::string& conf_file,
                            const std::string& delimiter, bool continue_on_error = false,
                            int thread_nums = 8, int skip_packages = 0,
                            const std::string& graph = "default", bool json_format = true,
                            double timeout = 0);
    @param [out] result              The result.
    @param [in]  conf_file           data file contain format description and data.
    @param [in]  delimiter           data separator.
    @param [in]  continue_on_error   (Optional) whether to continue when importing data fails.
    @param [in]  thread_nums         (Optional) maximum number of threads.
    @param [in]  skip_packages       (Optional) skip packages number.
    @param [in]  graph               (Optional) the graph to query.
    @param [in]  json_format         (Optional) Returns the format, true is json,Otherwise,
                                     binary format.
    @param [in]  timeout             (Optional) Maximum execution time, overruns will be
                                     interrupted.
    @returns True if it succeeds, false if it fails.