Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

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()+"	n:"+v.getProperty("n"));
            }

            }
        }

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



}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'))";

enter image description here

I used this csv file.

enter image description here

Hope it helps.

Regards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...