简体   繁体   English

如何以tinkerpop / gremlin格式而不是DSE图形格式返回Vertex?

[英]How to return a Vertex in the tinkerpop/gremlin format instead of the DSE graph format?

I am trying to return a Vertex (in tinkerpop format) that it was just created with Gremlin: 我试图返回它刚刚用Gremlin创建的Vertex(以tinkerpop格式):

DseCluster dseCluster = DseCluster.builder()
        .addContactPoint(DbC.dseHost)
        .build();
DseSession dseSession = dseCluster.connect();
GraphTraversal traversal = graph.addV(VertexLabels.User)
        .property("username", "testuser")
GraphStatement graphStatement = DseGraph.statementFromTraversal(
    traversal
);
GraphResultSet grs = dseSession.executeGraph(graphStatement.setGraphName(DbC.graphName));
Vertex v = grs.one().as(Vertex.class);

and I am getting this exception... 我得到了这个例外......

java.lang.ClassCastException: com.datastax.driver.dse.graph.DefaultVertex cannot be cast to org.apache.tinkerpop.gremlin.structure.Vertex

How could the code be changed so that it returns in gremlin.structure.Vertex format instead of the DSE Graph Vertex format? 如何更改代码以便以gremlin.structure.Vertex格式而不是DSE Graph Vertex格式返回?

I am using: 我在用:

<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>dse-driver</artifactId>
    <version>1.1.1-beta1</version>
</dependency>
<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>java-dse-graph</artifactId>
    <version>1.0.0-beta1</version>
</dependency>

I hope this can be done otherwise migration from TitanDB will be painful.. 我希望这可以做到,否则从TitanDB迁移将是痛苦的..

In Datastax 1.1 it seems that you can't cast to Vertex directly, there is no indication of this in the documentation. 在Datastax 1.1 ,您似乎无法直接转换为Vertex ,文档中没有任何迹象。

Instead you can access VertexProperty ( org.apache.tinkerpop.gremlin.structure.VertexProperty ) using .getProperties(String) . 相反,您可以使用.getProperties(String)访问VertexPropertyorg.apache.tinkerpop.gremlin.structure.VertexProperty .getProperties(String)

GraphNode n = dseSession.executeGraph("g.V().hasLabel('test_vertex_meta_props')").one();
Vertex vertex = n.asVertex();

// there can be more than one VertexProperty with the key "meta_property"
Iterator<VertexProperty> metaProps = vertex.getProperties("meta_property");

VertexProperty metaProp1 = metaProps.next();
// the value of the meta property
int metaProp1Value = metaProp1.getValue().asInt();
// the properties of the meta property itself
Iterator<Property> simpleProps1 = metaProp1.getProperties();
Property simpleProp11 = simpleProps1.next();
double simplePropValue11 = simpleProp11.getValue().asDouble(); 
Property simpleProp12 = simpleProps1.next();
double simplePropValue12 = simpleProp12.getValue().asDouble(); 

// **multi value** meta property.
VertexProperty metaProp2 = metaProps.next();

Via: Datastax Manual (1.1) 通过: Datastax手册(1.1)

According to the lengthy discussion I had with Datastax Team through jira and emails: 根据我与Datastax团队通过jira和电子邮件进行的冗长讨论:

It is indeed possible to have Fluent API and get back pure Gremlin/tinkerpop objects. 确实可以使用Fluent API并获得纯Gremlin / tinkerpop对象。 This is possible as illustrated here ( java-dse graph 1.x documentation ) using next(), toList() directly on GraphTraversalSource and not using executeGraph() which will return the DSE Objects. 这可以如此处所示( java-dse graph 1.x文档 )直接在GraphTraversalSource上使用next(),toList()而不使用将返回DSE对象的executeGraph()。

So the above code changes to: 所以上面的代码改为:

Vertex user = graph.addV("User")
                 .property("username", "testuser").next();

where graph is a GraphTraversalSource<Vertex,Vertex> object and Vertex is a org.apache.tinkerpop.gremlin.structure.Vertex object. 其中graphGraphTraversalSource<Vertex,Vertex>对象, Vertexorg.apache.tinkerpop.gremlin.structure.Vertex对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM