Query in OrientDB

百般思念 提交于 2019-12-13 09:12:51

问题


I try to print a query through the java console but nothing comes out. this is my code someone could help me. I'm new to OrientDB and I'm just learning. The query I need is to know the shortest path between two nodes and print this query on the Java console. It does not give me any errors but nothing comes out.

public class Graph {
    private static final String DB_PATH = "C:/OrientDataBase/shortest_path";
    static OrientGraphNoTx DBGraph;
    static OrientGraphFactory factory;

 public static void main(String[] args) {
        factory = new OrientGraphFactory("plocal:"+DB_PATH);
        DBGraph = factory.getNoTx();
        HashMap<String, Vertex> nodes = new HashMap<String, Vertex>();

    for(int i = 0; i <= 1000; i++)
    {
        Vertex v = DBGraph.addVertex("class:V");
        v.setProperty("vertexID", i+"");
        nodes.put(i+"", v);
    }


    try(BufferedReader br = new BufferedReader(new FileReader("C:/OrientDataBase/sp1.csv"))) {
        int i=0;
        for(String line; (line = br.readLine()) !=null ; ) {
            if(i==0){
                i++;
            }
            else{
            String[] vertices = line.split(",");
            String vertex1 = vertices[0];
            String vertex2 = vertices[1];
            String weight= vertices[2];
            vertex2 = vertex2.replaceAll(" ", "");

            Vertex v1 = nodes.get(vertex1);
            Vertex v2 = nodes.get(vertex2);


            Edge eLives = DBGraph.addEdge(null, v1, v2, "belongs");
            eLives.setProperty("weight", weight);
            System.out.println(v1+","+v2+","+weight);

            String query = "select expand(shortestPath) from (select shortestPath(#10:0,#10:2,BOTH))";

            Iterable<OrientVertex> res = DBGraph.command(new OCommandSQL(query)).execute();

            while(res.iterator().hasNext()){
            OrientVertex v = res.iterator().next();
            System.out.println("rid: "+v.getId().toString()+"\tn:"+v.getProperty("n"));
            }

            }
        }

    }
    catch (IOException e) {
        e.printStackTrace();
    }



}
}

回答1:


I tried your code and you have to put the ticks when you do the query so, it becomes:

String query = "select expand(shortestPath) from (select shortestPath(#10:0,#10:2,'BOTH'))";

I used this csv file.

Hope it helps.

Regards



来源:https://stackoverflow.com/questions/40039917/query-in-orientdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!