凯发k8国际娱乐官网入口-k8凯发> 图引擎服务 ges> > 使用cypher jdbc driver访问ges
更新时间:2023-11-17 gmt 08:00

使用cypher jdbc driver访问ges-凯发k8国际娱乐官网入口

功能介绍

ges cypher jdbc driver是专为ges编写的jdbc驱动,基于neo4j jdbc driver中的接口,提供了使用jdbc访问ges并进行cypher查询的一种方法。下载相应的sdk和驱动的具体操作请参考连接管理

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

依赖配置

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

 
      com.huawei.ges 
      cypher-jdbc-driver 
      xxx //此处需要输入当前cypher jdbc驱动的版本号
 

参数说明

表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对象的形式返回。

deserializer-type

解析cypher响应的策略,可选项为lazy和eager,默认为lazy。

  • 取值为lazy时,采用流式解析cypher的策略,cypher返回体不常驻内存。
  • 取值为eager时为获取整个json后解析。

limit

流速控制,默认值100000,内核以批的形式返回给server侧的webapp,由webapp整理成流返回给前端。limit的含义为内核返回给webapp时的批的大小。对同一条查询,limit越小时,ges内核侧交互次数增多,jdbc client拿到第一条记录的时间越快,整体查询时间变长。

使用示例

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的配置详见,并且需要您输入获取到的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 = ""; 
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全
   // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量huaweicloud_sdk_ak和huaweicloud_sdk_sk
     string accesskey = system.getenv("huaweicloud_sdk_ak");
     string secretkey = system.getenv("huaweicloud_sdk_sk"); 
     string regionname = "cn-north-4xxx"; 
     public static graphinfo getgraphinfo() { 
         //正式代码应该通过正常方式初始化graphinfo对象。
         graphinfo info = getgraphinfobyyourself(); 
         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 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 iamheaders) { 
         properties prop = new properties(); 
         for (map.entry 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."); 
         } 
     } 
 }
分享:
网站地图