Using Cypher JDBC Driver to Access GES
Introduction
The Cypher JDBC Driver is designed for GES. It is developed based on Neo4j JDBC Driver and provides a method of using JDBC to access GES and perform cypher queries.
The driver greatly reduces the CPU and memory usage for returning a large amount of data requested by high-concurrent cypher queries to avoid JVM caching of a complete request body. The driver parses a response body into streaming data instead obtaining an entire body and then parsing it.
Configuring Dependencies
- Download the SDK and driver. For details, see Managing Connections .
- If the Maven source is available (configured with a Maven source that can download JAR files from an open source repository), decompress huaweicloud-ges-sdk-java-xxx.zip and go to the maven-install directory. From there, execute either the ges-sdk-java-maven-install.bat or ges-sdk-java-maven-install.sh file to install graph-sdk-xxx.jar and cypher-jdbc-driver-xxx.jar to your local Maven repository. This will allow you to configure your POM dependencies and use the Cypher JDBC Driver in your Maven project.
<dependency> <groupId>com.huawei.ges</groupId> <artifactId>cypher-jdbc-driver</artifactId> <version>xxx</version> // Enter the version number of the Cypher JDBC driver. </dependency> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc</artifactId> <version>xxx<version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>xxx</version> </dependency>
- If the Maven source is unavailable (not configured with a Maven source or the Maven source cannot download JAR files from an open source repository), decompress huaweicloud-ges-sdk-java-xxx.zip and import the cypher-jdbc-driver-xxx-with-dependencies.jar file in the jars directory to the project or import all packages in the cypher-jdbc-driver-xxx.jar and cypher-jdbc-lib directories to the project.
Parameter Description
Type |
Parameter Description |
url |
The jdbc:ges: prefix is used to concatenate the URL for the GES Cypher API. On the Connection Management page of the GES console, select the name of the graph you want to access from the Available Instance drop-down list. The URL of the Cypher API is the value of JDBC connection string. |
prop |
Properties object, including configurations required for connecting to GES APIs. For details, see Table 2. |
Type |
Parameter Description |
X-Auth-Token |
Token obtained through IAM authentication. |
parse-json |
Whether data is converted to vertices and edges. The default value is false.
|
deserializer-type |
Policy for parsing Cypher statements. The options are lazy and eager, with lazy as the default.
|
limit |
Flow rate, with a default value of 100000. The kernel returns data to the server-side web application in batches, which are then organized into a stream and sent to the frontend. limit determines the batch size when the kernel returns data to the web application. For the same query, a smaller limit value results in more interactions between the GES kernel and the web application, but the JDBC client receives the first record faster. However, this may increase the overall query time. |
Authentication
GES Cypher JDBC Driver supports two authentication modes: Token-based and AK/SK-based.
- The following is sample code for token-based authentication:
import com.huawei.ges.jdbc.io.model.GesElement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; public class CypherJDBCClient { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { String token = ""; // Call the IAM API to obtain the value. String url = ""; // The value is jdbc:ges:JDBC connection string. You can obtain JDBC connection string from the Connection Management page of the GES console. Class.forName("com.huawei.ges.jdbc.Driver").newInstance(); Properties prop = new Properties(); prop.setProperty("X-Auth-Token", token); prop.setProperty("deserializer-type", "lazy"); prop.setProperty("parse-json", "true"); prop.setProperty("limit", "10000"); try (Connection conn = DriverManager.getConnection(url, prop)) { String query = "match (m) return m limit 1"; try (PreparedStatement stmt = conn.prepareStatement(query)) { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { GesElement.GesVertex vertex = (GesElement.GesVertex) rs.getObject("m"); System.out.println(vertex.getId()); System.out.println(vertex.getLabels()); System.out.println(vertex.getProperties()); } } } } catch (SQLException e) { System.out.println("Execute SQL query error."); } } }
- To authenticate using an AK/SK, you can generate a signature using the methods provided by the GES service-plane SDK. For how to import the dependencies for the service-plane SDK, refer to Downloading and Installing the SDK.
The following is sample code for AK/SK-based authentication. For how to obtain the AK, SK, and region code, see Obtaining Initialization Parameters.
import com.huawei.ges.graph.v1.auth.aksk.HttpRestClient; import com.huawei.ges.jdbc.io.model.GesElement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import java.util.Properties; public class CypherJDBCClientByAKSK { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { // Hard-coded or plaintext AK and SK are insecure. So, encrypt your AK and SK and store them in the configuration file or environment variables. // In this example, the AK and SK are stored in environment variables. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK. String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); String regionCode = ""; String url = ""; // The value is jdbc:ges:JDBC connection string. You can obtain JDBC connection string from the Connection Management page of the GES console. Map<String, String> iamHeader = HttpRestClient.getIamSignHeaders(ak, sk, regionCode); // Methods provided by the GES service-plane SDK, which provides guidance on generating signatures. Class.forName("com.huawei.ges.jdbc.Driver").newInstance(); doCypherQuery(url, iamHeader); } public static void doCypherQuery(String url, Map<String, String> iamHeaders) { Properties prop = new Properties(); for (Map.Entry<String, String> pair : iamHeaders.entrySet()) { prop.setProperty(pair.getKey(), pair.getValue()); } prop.setProperty("deserializer-type", "lazy"); prop.setProperty("parse-json", "true"); prop.setProperty("limit", "10000"); try (Connection conn = DriverManager.getConnection(url, prop)) { String query = "match (m) return m limit 1"; try (PreparedStatement stmt = conn.prepareStatement(query)) { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { GesElement.GesVertex vertex = (GesElement.GesVertex) rs.getObject("m"); System.out.println(vertex.getId()); System.out.println(vertex.getLabels()); System.out.println(vertex.getProperties()); } } } } catch (SQLException e) { System.out.println("Execute SQL query error."); } } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot