Search code examples
sparqlrdfamazon-neptune

How to find leaf nodes in the graph using SPARQL?


RDF Graph

I have to find out all the leaf nodes of this graph with a generic query, this is because the leaf node is getting created from item X after 2 steps in some cases, in 1 step in some cases and in 3 steps in some cases.

Can someone help to write a generic RDF traversal query?


Solution

  • What you will need is to look for all nodes what are not the 'subject' of any triple, i.e. that don't have an arrow coming out of them.

    This is done with a query like:

    SELECT DISTINCT ?b
    WHERE{
    ?a :create ?b .
    FILTER NOT EXISTS {?b :create ?c}
    }