简体   繁体   中英

How can I identify what is being returned from a Gremlin query? Like vertices or edges or labels, etc

I have a Java application which allows user to push Gremlin queries from UI and those queries are executed at a backend gremlin server.

I want to know what is being returned in the ResultSet. It can be a list of Vertices or list of Edges or it can be list of String (may be labels) as well. So that I can show proper UI pertaining to that type of object being returned.

There is no certainty what is being returned just be looking at the initial part of query. For example:

g.V() // returns list of Vertices
g.V().outE() // returns list of Edges
g.E().outV() // returns list of Vertices
g.E().outV().label() // returns list of Strings

String query = "g.E().outV().label()";
ResultSet resultSet = client.submit(query);
resultSet.forEach(result -> result.getString()); // getString or getVertex or getEdge ???

Analysing whole query to identify the returned object is not an intelligent and foolproof approach.

What can be the best approach to identify the return type? Besides trying all with try...catch and then choosing wherever it succeeded.

You really don't have much choice but to test the result to see its type. Gremlin results are also not always homogeneous. For example, someone could send this:

g.V(1).outE().path().
  unfold().
  inject(1, [1L, "xyz", [x:"don't care that this traversal is crazy"]], Double.NaN)

Forget that no one would likely do that and just consider that they could. You get a mix of vertices, edges, a list with a long a double and embedded map. You don't know the result until you have it and even then you may need to detect the types inside of the container collections. If you have different sorts of rendering in your UI for each type you have no choice but to recursively do type detection. You will probably also want some sort of default rendering that can handle things you don't know how to render.

If you switch off from Gryo serialization (I presume you're using that as the default for your client ) to GraphSON you do get a more reduced set of types to expect. You can see those in the IO documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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