TuGraph Java SDK
1.Compile java client code
cd deps/tugraph-db-client-java
sh local_build.sh
2.Demo
2.1.Instantiate the client object
Introduce dependencies
import com.alipay.tugraph.TuGraphDbRpcClient;
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
TuGraphDbRpcClient client = new TuGraphDbRpcClient("127.0.0.1:19099", "admin", "73@TuGraph");
public TuGraphDbRpcClient(String url, String user, String pass)
@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
TuGraphDbRpcClient client = new TuGraphDbRpcClient("127.0.0.1:19099", "admin", "73@TuGraph");
public TuGraphDbRpcClient(String url, String user, String pass)
@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.
List<String> urls = new ArrayList<>();
urls.add("189.33.97.23:9091");
urls.add("189.33.97.24:9091");
urls.add("189.33.97.25:9091");
TuGraphDbRpcClient client = new TuGraphDbRpcClient(urls, "admin", "73@TuGraph");
public TuGraphDbRpcClient(List<String> urls, String user, 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
String res = client.callCypher("CALL db.edgeLabels()", "default", 10);
log.info("db.edgeLabels() : " + res);
@param cypher: inquire statement.
@param graph: the graph to query.
@param timeout: Maximum execution time, overruns will be interrupted
@param url: (Optional) Node address of calling cypher
@return: the result of cypher query execution
public String callCypher(String cypher, String graph, double timeout, String url)
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
String result = client.callProcedure("CPP", "khop", kHopParamGen(), 1000, false, "default");
log.info("testCallProcedure : " + result);
@param procedureType: the procedure type, currently supported CPP and PY
@param procedureName: procedure name
@param param: the execution parameters
@param procedureTimeOut: Maximum execution time, overruns will be interrupted
@param inProcess: Running query or not
@param graph: the graph to query
@param url: (Optional) Node address of calling procedure
@return: the result of procedure execution
public String callProcedure(String procedureType, String procedureName, String param, double procedureTimeOut,
boolean inProcess, String graph, String url)
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
boolean result = client.loadProcedure("./test/procedure/khop.so", "CPP", "khop", "SO", "test loadprocedure", true, "v1", "default");
log.info("loadProcedure : " + result);
@param sourceFile: the source_file contain procedure code
@param procedureType: the procedure type, currently supported CPP and PY
@param procedureName: procedure name
@param codeType: code type, currently supported PY, SO, CPP, ZIP
@param procedureDescription: procedure description
@param readOnly: procedure is read only or not
@param version: The version of procedure
@param graph: the graph to query.
@return: the result of procedure execution
public boolean loadProcedure(String sourceFile, String procedureType, String procedureName, String codeType,
String procedureDescription, boolean readOnly, String version, String graph) throws Exception
2.5.List stored procedures
String result = client.listProcedures("CPP", "any", "default");
log.info("listProcedures : " + result);
@param procedureType: the procedure type, currently supported CPP and PY
@param version: The version of procedure
@param graph: the graph to query.
@param url: (Optional) Node address of listing procedure
@return: the list of procedure
public String listProcedures(String procedureType, String version, String graph, String url) throws Exception
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
String result = client.deleteProcedure("CPP", "sortstr", "default");
log.info("loadProcedure : " + result);
@param procedureType: the procedure type, currently supported CPP and PY
@param procedureName: procedure name
@param graph: the graph to query.
@return: the result of procedure execution
public boolean deleteProcedure(String procedureType, String procedureName, String graph) throws Exception
2.7.Import from a byte stream schema
boolean ret = client.importSchemaFromContent(schema, "default", 1000);
log.info("importSchemaFromContent : " + ret);
@param schema: the schema to be imported
@param graph: the graph to query.
@param timeout: Maximum execution time, overruns will be interrupted
@return: the result of import schema
public boolean importSchemaFromContent(String schema, String graph, double timeout) throws UnsupportedEncodingException
2.8.Import node and edge data from a byte stream
boolean ret = client.importDataFromContent(personDesc, person, ",", true, 16, "default", 1000);
log.info("importDataFromContent : " + ret);
@param desc: data format description
@param data: the data to be imported
@param delimiter: data separator
@param continueOnError: whether to continue when importing data fails
@param threadNums: maximum number of threads
@param graph: the graph to query.
@param timeout: Maximum execution time, overruns will be interrupted
@return: the result of import data
public boolean importDataFromContent(String desc, String data, String delimiter, boolean continueOnError,
int threadNums, String graph, double timeout) throws UnsupportedEncodingException
2.9.Import schema from a file
boolean ret = client.importSchemaFromFile("./test/data/yago.conf", "default", 1000);
log.info("importSchemaFromFile : " + ret);
@param schemaFile: the schema_file contain schema
@param graph: the graph to query.
@param timeout: Maximum execution time, overruns will be interrupted
@return: the result of import schema
public boolean importSchemaFromFile(String schemaFile, String graph, double timeout)
throws UnsupportedEncodingException, IOException
2.10.Import edge data from a file
boolean ret = client.importDataFromFile("./test/data/yago.conf", ",", true, 16, 0, "default", 1000000000);
log.info("importDataFromFile : " + ret);
@param confFile: data file contain format description and data
@param delimiter: data separator
@param continueOnError: whether to continue when importing data fails
@param threadNums: maximum number of threads
@param skipPackages: skip packages number
@param graph: the graph to query.
@param timeout: Maximum execution time, overruns will be interrupted
@return: the result of import data
public boolean importDataFromFile(String confFile, String delimiter, boolean continueOnError, int threadNums,
int skipPackages, String graph, double timeout) throws IOException, UnsupportedEncodingException