图引擎服务 GES-使用Cypher JDBC Driver访问GES:使用示例

时间:2024-04-18 11:08:22

使用示例

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. 
            // ... 
        } 
    } 
}
support.huaweicloud.com/devg-ges/ges_05_0049.html