Skip to content
Advertisement

Using the ‘astar’ function of OrientDB: an SQL call in Java

I’m using OrientDB to represent large city maps and calculate the shortest traversal times between a pair of nodes when the need arises. I have the following method:

public void getShortestPath(int from, int to){
    String query = "SELECT astar(?, ?, time, direction=OUT, customHeuristicFormula=EUCLIDEAN) FROM V";
    OResultSet set = db.query(query, getNode(from).getProperty("@rid"), getNode(to).getProperty("@rid"));
}

The getNode(nodeID) method returns the OVertex object present in the database, hence we can identify the @rid. Clearly, this method serves no purpose as is.

My issue comes when trying to call the astar query on the database (i.e. line two of the method). I’m getting the following error: OCommandSQLParsingException: Error parsing query upon reaching the first ( (i.e. error encountering the open bracket). Removing the brackets entirely simply resulted in the same error occurring on the # in front of the first @rid value.

I can’t seem to find any example of using this function in practice, and (at least I think) I’m using the function call as suggested by the documentation. Look forward to hearing your thoughts.

I’m using the most recent version of OrientDB: 3.2.3

Advertisement

Answer

Turns out the error had nothing to do with the bracket itself. Passing the "direction='OUT'" and "customHeuristicFormula='EUCLIDEAN'" parameters in as part of the string was the problem. The below block did the trick.

    String sql = "SELECT ASTAR(" + getNode(from).getProperty("@rid") + ", " + getNode(to).getProperty("@rid") + ", time) FROM V";
    try(OResultSet set = db.query(sql, "direction='OUT'", "customHeuristicFormula='EUCLIDEAN'")) {
         // some code...
    }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement