Orientdb与OCommandSQL匹配返回null顶点

时间:2017-07-17 09:10:40

标签: java orientdb orientdb2.2 tinkerpop-blueprint

我是OrientDB和蓝图的新手。我正在尝试使用OrientDB Java API执行简单的MATCH查询。以下是我的代码:

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

}

当我运行它时,在顶点中给我null。简单Select * from Person工作正常,返回非空顶点。我使用的是OrientDB的2.2.22版本。

任何链接或提示将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:3)

以下链接非常有用http://useof.org/java-open-source/com.orientechnologies.orient.core.metadata.schema.OSchemaProxy

实际上我们需要在匹配查询中返回$elements

以下代码有效:

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

希望它会帮助某人。谢谢!