Print the shortest path to the java console using orientdb

淺唱寂寞╮ 提交于 2019-12-24 08:25:27

问题


I want to print in the Java console shortestpath between two vertices. I can not print anything or if you have any way to do that would appreciate it.

String subquery = "Select shortestpath(17:10, 17:14, BOTH) ";
Iterable<OrientVertex> result = orientDBGraph.command(new OSQLSynchQuery<OrientVertex>(subquery)).execute();
Assert.assertTrue(result.iterator().hasNext());
System.out.println(result);

for (OrientVertex d : result) {
  System.out.println("Shortest path from " + ((OrientVertex) d.getProperty("$current")).getProperty("name") + " and "
    + ((Iterable<OrientVertex>) d.getProperty("$target")).iterator().next().getProperty("name") + " is: "
    + d.getProperty("path"));
}

回答1:


Code:

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String dbName = "ytrewa";

        OrientGraphFactory dbfactory = new OrientGraphFactory("remote:127.0.0.1:2424/"+dbName, "root", "root").setupPool(1, 50);

        OrientGraph g = dbfactory.getTx();

        try {
            String query = "select expand(shortestPath) from (select shortestPath(#9:0,#9:1,BOTH))";
            Iterable<OrientVertex> res = g.command(new OCommandSQL(query)).execute();

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

        } finally { 
            g.shutdown();
        }

    }

}

Output:

rid: #9:0
rid: #10:0
rid: #12:0
rid: #9:1


来源:https://stackoverflow.com/questions/40027499/print-the-shortest-path-to-the-java-console-using-orientdb

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