Sads
Sads

Reputation: 557

Convert GraphResultSet JSON using Graphson

I'm trying to convert the GraphResultSet object to JSON format similar to datastax studio returns. I'm trying to use Graphson. Is there any sample codes convert the result object to JSON?

i tried the following from the tikerpop blueprints but its not working

List<GraphNode> gf=((GraphResultSet) resultSet).all();
Vertex v = (Vertex) gf.get(0).asVertex();
                JSONObject json = null;
                try {
                    json = GraphSONUtility.jsonFromElement((Element) v,getElementPropertyKeys((Element) v, false), GraphSONMode.COMPACT);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

I'm getting a GraphResultSet object from dse, It has vertex and edges. I wanted to output in JSON format.

Upvotes: 0

Views: 1039

Answers (2)

newkek
newkek

Reputation: 46

There is no direct way for now to convert a DSE driver graph object into JSON. However if using the DSE driver 1.5.0 you can configure the driver to use GraphSON1 if you are looking for simple JSON responses. Then simply output the String representation of GraphNode:

DseCluster dseCluster = DseCluster.builder().addContactPoint("127.0.0.1")
  .withGraphOptions(
    new GraphOptions()
      .setGraphName("demo")
      // GraphSON version is set here:
      .setGraphSubProtocol(GraphProtocol.GRAPHSON_1_0)
  )
  .build();

DseSession dseSession = dseCluster.connect();

// create query
GraphStatement graphStatement = [.....];

GraphResultSet resultSet = dseSession.executeGraph(graphStatement);
for (GraphNode gn : resultSet) {
    String json = gn.toString();
}

Upvotes: 2

Alex Ott
Alex Ott

Reputation: 87299

You can't directly cast between com.datastax.driver.dse.graph.DefaultVertex & com.tinkerpop.blueprints.Element.

There is GraphSONUtils class (src) in DSE Java driver that should be able to handle these conversions. But because it's in the "internal" package, I expect that changes may happen any time.

Upvotes: 1

Related Questions