无法从我的Graph实例看到我的顶点和边,但是能够从其他图实例看到

时间:2018-10-30 03:36:28

标签: cassandra graph-theory tinkerpop3 janusgraph

如果我使用应用程序内部的Graph实例添加顶点和边,然后查询那些查询结果,有时查询结果会向我发送顶点和边,有时却没有,则进行概念验证,但是如果我创建了JUnit测试指向服务器,我可以看到顶点和边缘保持不变。如果放下顶点或边也会发生同样的情况

What I'm missing?

============= Class to work with Vertex and Edge =================

public class JanusGraphRepository implements GraphRepository {

    private Graph graph;
    private GraphTraversalSource g;

    public JanusGraphRepository(Graph janusGraph) {
        this.graph = janusGraph;
        this.g = graph.traversal();
    }

    @Override
    public void dropE(Object id) {
        g.E(id).drop().iterate();
        g.tx().commit();
    }

    @Override
    public void dropV(Object id) {
        g.V(id).drop().iterate();
        g.tx().commit();
    }

    @Override
    public Vertex addV(final Object... keyValues) {
        Vertex v = graph.addVertex(keyValues);
        graph.tx().commit();
        return v;
    }

    @Override
    public Edge addE(String edgeLabel, Object fromVertex, Object toVertex,
            Object... keyValues) {
        Edge e = graph.vertices(fromVertex).next().addEdge(edgeLabel,
                graph.vertices(toVertex).next(), keyValues);
        graph.tx().commit();
        return e;
    }
}


======================== Code to get vertices and edges ======================

JanusGraphFactory.Builder config = JanusGraphFactory.build();
        config.set("storage.backend", "cql");
        config.set("storage.hostname", "10.2.1.134");
        config.set("storage.cql.keyspace", "janusgraph");
        config.set("index.search.backend", "elasticsearch");
        config.set("index.search.hostname", "10.2.1.134");
        // config.set("index.search.elasticsearch.client-only", "true");
        // ip address where cassandra is installed
        // config.set("storage.username", "cassandra");
        // config.set("storage.password", "cassandra");
        // config.set("storage.port", "8182");

        // Get the instance of graph
        JanusGraph graph = config.open();

        graph.vertices().forEachRemaining(x -> {
            System.out.println(x.id());
        });
        System.out.println("------ Edges -------");
        graph.edges().forEachRemaining(x -> {
            System.out.println(x.toString());
        });


Thanks

1 个答案:

答案 0 :(得分:2)

Cassandra中的突变不能保证立即可见。

我看到您正在使用Janus的CQL存储后端,这告诉我您正在使用Cassandra节点/集群。在Cassandra中,需要写入才能传播到每个节点,并且可能不会立即发生。听起来您可能正在经历这种情况。

特别是在需要将特定写入传递到拥有其索引范围的节点并且随后的读取最终从该节点读取的情况下,有可能陷入写入之后紧接着是立即写入的情况读取不会显示刚刚写入的数据。

出现写操作所需的时间取决于以下因素: 卡桑德拉集群;通常,即使规模很大,也不应超过几分钟,但这不能保证。 Cassandra优先考虑可用性,而不是突变的一致性。只要集群保持联机和可操作状态,变异就会最终使其集群中的所有副本最终,但不一定在写调用返回时出现。这种行为称为最终一致性

为了解决这个问题,您必须调整期望值;您不能期望刚刚写入集群的数据(因此,在将Cassandra用作后端时使用JanusGraph)将立即可用。

如果您的用例不能与Cassandra的一致性模型一起使用,我建议您看一下HBase或Storage Backends Section of the JanusGraph Manual中列出的其他后端之一。