Search code examples
sparqlgraphdbnamed-graphs

insert from two named graphs?


I'm looking for an easy way to insert triples from two or more named graphs (but not the entire unnamed default graph) into another named graph. I'm using GraphDB.

I guess this could be done by writing out the same query multiple times in the WHERE section, wrapped in multiple GRAPH specifications, and then unioning them together, but my WHEREs are long and I'd prefer not to write them out multiple times.

Let's say I have loaded some data like this:

INSERT DATA {
  GRAPH <http://example.com/ngA> {
    <http://example.com/person1> <http://example.com/name> "Arthur" .
  }
  GRAPH <http://example.com/ngB> {
    <http://example.com/person1> <http://example.com/name> "Brian" .
  }
  GRAPH <http://example.com/ngC> {
    <http://example.com/person1> <http://example.com/name> "Charlie" .
  }
}

I can copy all of the triples of a certain pattern from the default unnamed graph into a new named graph with something like this:

INSERT {
  GRAPH <http://example.com/ngZ> {
    ?s <http://example.com/moniker> ?o .
  }
}
WHERE
  { ?s  <http://example.com/name>  ?o }

An easy way to SELECT for triples of a given pattern from two or more (but not all) named graphs is

SELECT  *
FROM <http://example.com/ngA>
FROM <http://example.com/ngB>
WHERE
  { ?s  <http://example.com/name>  ?o }

What if I want to copy those triples, from those specified graphs, into another graph?

I'm getting an error from GraphDB 8.3 (and from the sparql.org validator) when I try to

INSERT {
  GRAPH <http://example.com/ngZ> {
    ?s <http://example.com/moniker> ?o .
  }
}
WHERE
  { SELECT  *
FROM <http://example.com/ngA>
FROM <http://example.com/ngB>
WHERE
  { ?s  <http://example.com/name>  ?o } }

Solution

  • Try this query:

    PREFIX ex: <http://example.com/>
    
    INSERT {
      GRAPH ex:ngZ { ?s ex:moniker ?o }
    }
    WHERE {
      GRAPH ?g { ?s ex:name ?o }  
      FILTER (?g IN ( ex:ngA, ex:ngB ) )
    }
    

    And then:

    PREFIX ex: <http://example.com/>
    
    SELECT * 
    FROM NAMED ex:ngZ
    WHERE { 
        GRAPH ?g { ?s ?p ?o }
    } LIMIT 100 
    

    Is it what you need?

    By the way, there exist COPY (use with caution!) and ADD.