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

为君一笑 提交于 2019-12-31 04:03:09

问题


I am trying to return a Vertex (in tinkerpop format) that it was just created with Gremlin:

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?

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..


回答1:


In Datastax 1.1 it seems that you can't cast to Vertex directly, there is no indication of this in the documentation.

Instead you can access VertexProperty (org.apache.tinkerpop.gremlin.structure.VertexProperty) using .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)




回答2:


According to the lengthy discussion I had with Datastax Team through jira and emails:

It is indeed possible to have Fluent API and get back pure Gremlin/tinkerpop objects. 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.

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.



来源:https://stackoverflow.com/questions/41261845/how-to-return-a-vertex-in-the-tinkerpop-gremlin-format-instead-of-the-dse-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!