简体   繁体   中英

Query in OrientDB

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();
    }



}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM