文档首页/ 图引擎服务 GES/ 开发指南/ 使用Cypher JDBC Driver访问GES
更新时间:2022-12-08 GMT+08:00

使用Cypher JDBC Driver访问GES

功能介绍

GES Cypher JDBC Driver是专为GES编写的JDBC驱动,基于Neo4j JDBC Driver中的接口,提供了使用JDBC访问GES并进行cypher查询的一种方法。

尤其是当cypher请求返回数据量较大、并发数高、JVM缓存完整请求体有困难的场景下,该组件内置了一种可以流式解析响应body体的方法,与获得整个body体再解析相比,极大地降低了cpu和内存的占用。

依赖配置

使用前需进行工程导入,并配置maven工程,pom依赖配置如下:

<dependency>
      <groupId>com.huawei.ges</groupId>
      <artifactId>cypher-jdbc-driver</artifactId>
      <version>1.1.0</version>
 </dependency>

使用参数

表1 JDBC getConnection参数说明

参数

释义

url

GES Cypher API的URL,添加前缀jdbc:ges:http(s)为前缀以方便JDBC Driver识别,是DriverManager.getConnection的第一个参数。

prop

Properties对象,包含连接GES API所需的各项配置,详见表2

表2 Properties参数信息

参数

释义

X-Auth-Token

通过iam鉴权接口获取到的token。

parse-json

是否转换点边对象,默认值为"false", 取值为false时,cypher返回体中的点和边将以map形式返回,为true时,以GesElement对象的形式返回。

使用示例

package org.example;
 
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
 
public class App 
{
    static String ip = "${ip}";
    static int port = 80;
    static String projectId = "${projectId}";
    static String graphName = "${graphName}";
    static String token = "${x_auth_token}";
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class.forName("com.huawei.ges.jdbc.Driver").newInstance();
        String url = "jdbc:ges:http://{{graph_ip}}:{{graph_port}}/ges/v1.0/{{project_id}}/graphs/{{graph_name}}/action?action_id=execute-cypher-query";
        url = url.replace("{{graph_ip}}", ip).replace("{{graph_port}}",port + "").replace("{{project_id}}", projectId).replace("{{graph_name}}", graphName);
        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 1000";
            try(PreparedStatement stmt = conn.prepareStatement(query)){
                try(ResultSet rs = stmt.executeQuery()){
                    Object o = null;
                    while(rs.next()) {
                        o = rs.getObject("m");
                        processVertex(o);
                    }
                }
            }
        } catch (SQLException e) {
            // here process exception.
            // ...
        }
    }
}

鉴权方法

GES Cypher JDBC Driver支持Token和AK/SK两种鉴权模式。

  • Token鉴权相关参数详见使用参数使用示例
  • AKSK鉴权需要依赖GES业务面SDK获取AK/SK签名后,使用签名进行鉴权操作。

    导入业务面SDK依赖详见工程导入,GraphInfo的配置详见初始化GES业务面客户端,并且需要您输入获取到的AccessKey,secretKey和regionName参数。

    以AK/SK鉴权方式为例,代码示例如下:
    import com.huawei.ges.jdbc.io.model.GesElement;
    import com.huawei.graph.sdk.GraphInfo;
    import com.huawei.graph.sdk.exception.GraphSdkException;
    import com.huawei.graph.sdk.utils.HttpRestClient;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Properties;
    import java.util.Map;
    
    public class CypherJDBCClientByAKSK {
    
        private static final Logger logger = LoggerFactory.getLogger("CypherJDBCClientByAKSK");
        private static String ip = "";
        private static int port = 80;
        private static String projectId = "";
        private static String graphName = "";
        String accessKey = "here is access key";
        String secretKey = "here is secret key";
        String regionName = "cn-north-4";
        public static GraphInfo getGraphInfo() {
            //正式代码应该通过正常方式初始化graphInfo对象。
            GraphInfo info = getGraphInfoByYourSelf();
           // 此处需要输出您的AK/SK信息
            info.setAccessKey(accessKey);    
            info.setSecretKey(secretKey);   
          // 此处需要输出您的regionName
            info.setRegionName(regionName);
            return info;
        }
    
        public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, GraphSdkException {
    
            GraphInfo info = getGraphInfo();
    
            Map<String, String> iamHeader = HttpRestClient.getIamSignHeaders(info);
            Class.forName("com.huawei.ges.jdbc.Driver").newInstance();
            String url = "jdbc:ges:http://{{graph_ip}}:{{graph_port}}/ges/v1.0/{{project_id}}/graphs/{{graph_name}}/action?action_id=execute-cypher-query";
            url = url.replace("{{graph_ip}}", ip).replace("{{graph_port}}", port + "").replace("{{project_id}}", projectId).replace("{{graph_name}}", graphName);
            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()) {
                        Object o = null;
                        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) {
                logger.info("Execute SQL query error.");
            }
        }
    }