javaorientdborientdb2.2tinkerpop-blueprint

Orientdb Match with OCommandSQL is returning null Vertices


I am new to OrientDB and blueprint. I am trying to execute simple MATCH query using OrientDB Java API. Below is my code :

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;

public class OrientApp {

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    TransactionalGraph graph = new OrientGraph("remote:localhost/GratefulDeadConcerts", "admin", "admin");
    /*
     * Iterable<Vertex> vertices = (Iterable<Vertex>) (((OrientGraph) graph)
     * .command(new OCommandSQL(
     * "MATCH {class: Person, as: liker} -likes- {class:Person, as: like},{as:liker} -living_in- {class: City, where: (name='Bangalore')},{as:like} -living_in- {class: City, where: (name='Delhi')} RETURN liker.name,like.name"
     * )) .execute());
     */

    Iterable<Vertex> vertices = (Iterable<Vertex>) (((OrientGraph) graph)
            .command(new OCommandSQL("MATCH {class: Person, as: person} RETURN person")).execute());

    /*
     * Iterable<Vertex> vertices = (Iterable<Vertex>) (((OrientGraph) graph)
     * .command(new OCommandSQL("select * from person")).execute());
     */for (Vertex v : vertices) {
        System.out.println(v);
    }
    System.out.println(graph);
}

}

When I run it gives me null in Vertices. The simple Select * from Person is working fine and returns not null vertices. I am using 2.2.22 version of OrientDB.

Any links or hints will be appreciated. Thanks!


Solution

  • Following link is useful http://useof.org/java-open-source/com.orientechnologies.orient.core.metadata.schema.OSchemaProxy

    Actually we need to return $elements in Match query.

    Following code worked:

    import com.orientechnologies.orient.core.sql.OCommandSQL;
    import com.tinkerpop.blueprints.TransactionalGraph;
    import com.tinkerpop.blueprints.Vertex;
    import com.tinkerpop.blueprints.impls.orient.OrientGraph;
    
    public class OrientApp {
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
    
        TransactionalGraph graph = new OrientGraph("remote:localhost/GratefulDeadConcerts", "admin", "admin");
        Iterable<Vertex> vertices = (Iterable<Vertex>) (((OrientGraph) graph)
                .command(new OCommandSQL("MATCH {class: Person, as: person, where: (age>10)} RETURN $elements"))
                .execute());
    
        for (Vertex v : vertices) {
            System.out.println(v.getProperty("age").toString());
        }
        System.out.println(graph);
    }
    }
    

    Hope it will help someone. Thanks!