Search code examples
javagremlintinkerpop3

How to find graph schema in Gremlin?


I wanna to find all node and edge properties in graph. How can I list node (or edge) properties that exist in graph?

for example if nodes has 3 non-reserved properties such as NAME, education, gender. I wanna a methods like

g.V().schema().toList();
// result: [ID, LABEL, NAME, GENDER, EDUCATION]

Solution

  • If all nodes have a same properties. we can find the properties of the first vertex and generalize it to all nodes:

    TinkerGraph tg = TinkerGraph.open() ;
    tg.io(IoCore.graphml()).readGraph("src\\main\\resources\\air-routes.graphml");
    GraphTraversalSource g = tg.traversal();
    
    g.V().propertyMap().select(Column.keys).next();
    // result = {LinkedHashSet@1831}  size = 12
    // 0 = "country"
    // 1 = "code"
    // 2 = "longest"
    // 3 = "city"
    // 4 = "elev"
    // 5 = "icao"
    // 6 = "lon"
    // 7 = "type"
    // 8 = "region"
    // 9 = "runways"
    // 10 = "lat"
    // 11 = "desc"
    

    but If there is no guaranty to each node has a same set of properties, I don't find any other solution instead of retrieving all properties in a Map List and find distinct property with java collection methods (outside Gremlin).