【问题标题】:Listing all Vertices of specific class in OrientDB列出 OrientDB 中特定类的所有顶点
【发布时间】:2014-12-14 13:27:07
【问题描述】:

我最近开始探索图形数据库(尤其是 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"
}

我一直在尝试解决这个问题,尝试错误提示的“修复”。我也尝试过使用 TransactionalGraph 而不是 OrientGraph。

这就是问题所在...我也在使用类似的方法来获取单个资源。此方法在我使用“System.out.println”时有效,否则会失败并出现同样的错误。

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

任何帮助表示赞赏!

【问题讨论】:

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


    【解决方案1】:

    您应该在使用结果时让图表 (=connection) 保持打开状态。浏览结果集后可以移动graph.shutdown() 吗?

    【讨论】:

    • 如果我在关机前返回 JSON,那图连接不会无限期地打开吗?
    • 尝试删除 try{} 块内的 return
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多