列出OrientDB中特定类的所有顶点

时间:2014-12-14 13:27:07

标签: spring orientdb tinkerpop tinkerpop-blueprint tinkerpop-frames

我最近开始探索图形数据库(特别是Neo4j和OrientDB)并遇到了一个我似乎无法解决的问题。

我正在运行OrientDB的本地安装(OrientDB Server v2.0-M3处于活动状态。)。 我使用Tinkerpops连接到图形并对图形运行查询。 我在本地Tomcat 7服务器上使用Java和Spring。 测试我的API我在Chrome上使用Postman。

这是我错误的GET方法:

@RequestMapping(value = "/articles", method = RequestMethod.GET)
public
@ResponseBody
Vector<Article> list() {
    OrientGraph graph = new OrientGraph("remote:/local/path/to/orientdb/databases/mydb", "user", "password");

    FramedGraphFactory factory = new FramedGraphFactory();
    FramedGraph manager = factory.create(graph);

    Vector<Article> articles = new Vector<>();

    try {
        Iterable<Vertex> vertices = graph.getVerticesOfClass("Article", false);

        Iterator<Vertex> it = vertices.iterator();

        if (it.hasNext()) {
            do {
                Article a = (Article) manager.frame(it.next(), Article.class);
                articles.add(a);
            } while (it.hasNext());
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        graph.shutdown();
    }

    return articles;
}

这会产生以下错误:

{
"timestamp": 1418562889304,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.http.converter.HttpMessageNotWritableException",
"message": "Could not write JSON: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db); (through reference chain: java.util.Vector[0]->$Proxy43[\"name\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Database instance is not set in current thread.    Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db); (through reference chain: java.util.Vector[0]->$Proxy43[\"name\"])",
"path": "/articles"
}

我一直试图解决这个问题,尝试修复&#34;修复&#34;错误表明。我还试图使用TransactionalGraph而不是OrientGraph。

这里的问题......我也使用类似的方法来获取单一资源。如果我使用&#34; System.out.println&#34;,则此方法有效,否则会因相同错误而失败。

@RequestMapping(value = "/article", method = RequestMethod.GET)
public
@ResponseBody
Article get(
        @RequestParam(value = "number", required = true) long number
) {
    TransactionalGraph graph = new OrientGraph("remote:/path/to/local/orientdb/orientdb/databases/mydb", "user", "password");

    FramedGraphFactory factory = new FramedGraphFactory();
    FramedGraph manager = factory.create(graph);

    Article article = null;

    try {
        Iterable<Article> articles = manager.getVertices("number", number, Article.class);

        article = articles.iterator().next();

        System.out.println(article.getName());

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        graph.shutdown();
    }

    return article;
}

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

您应该在使用结果时打开图表(=连接)。您可以在浏览结果集后移动graph.shutdown()吗?

相关问题