I need to add some of the data from graph A to graph B. Some of the data in graph A is already in graph B, and I don't want to add in duplicate data. How would I write a SPARQL query to find the data that are in graph A but not in graph B? I need to compare the data on five fields. A SELECT query would look like this.

SELECT
  *
WHERE {
  graph <http://sparql.tw.rpi.edu/source/dataset/graphB> {
    ?measurement
    pol:hasSite ?Site ;
    time:inXSDDateTime ?TimeStamp ;
    pol:hasUSGSPCode ?PCode ;
    pol:hasValue ?Value ;
    repr:hasUnit ?Unit .
  }
}

asked 17 Nov '12, 23:23

Hammer's gravatar image

Hammer
235
accept rate: 0%

edited 17 Nov '12, 23:28


RDF defines that data is not duplicated in an RDF set, so your triplestore should ignore any duplicate values if you attempt to insert them into the graph when they are already present. In that case you can do a simple INSERT/WHERE:

INSERT {
graph <http://sparql.tw.rpi.edu/source/dataset/graphB> {
   ?measurement
   pol:hasSite ?Site ;
   time:inXSDDateTime ?TimeStamp ;
   pol:hasUSGSPCode ?PCode ;
   pol:hasValue ?Value ;
   repr:hasUnit ?Unit .
 }
} WHERE {
   graph <http://sparql.tw.rpi.edu/source/dataset/graphA> {
    ?measurement
    pol:hasSite ?Site ;
    time:inXSDDateTime ?TimeStamp ;
    pol:hasUSGSPCode ?PCode ;
    pol:hasValue ?Value ;
    repr:hasUnit ?Unit .
 }

}

Or, even more simply, if you want add all of the triples from GraphA to GraphB that did not exist in GraphB to begin with then do a SPARQL ADD operation

ADD <http://sparql.tw.rpi.edu/source/dataset/graphA> TO <http://sparql.tw.rpi.edu/source/dataset/graphB>

If you are not actually attempting to modify GraphA but just calculate the difference between the two via a query, then consider the MINUS operator:

SELECT  *
WHERE {
  graph <http://sparql.tw.rpi.edu/source/dataset/graphA> {
    ?measurement
    pol:hasSite ?Site ;
    time:inXSDDateTime ?TimeStamp ;
    pol:hasUSGSPCode ?PCode ;
    pol:hasValue ?Value ;
    repr:hasUnit ?Unit .
  }
MINUS {  graph <http://sparql.tw.rpi.edu/source/dataset/graphB> {
    ?measurement
    pol:hasSite ?Site ;
    time:inXSDDateTime ?TimeStamp ;
    pol:hasUSGSPCode ?PCode ;
    pol:hasValue ?Value ;
    repr:hasUnit ?Unit .
  }
}
}
link

answered 17 Nov '12, 23:43

harschware's gravatar image

harschware ♦
7.6k1616
accept rate: 20%

edited 17 Nov '12, 23:58

Thanks for the help. For the present, I think the MINUS operator will suit my purpose.

(24 Nov '12, 16:18) Hammer Hammer's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,243

Asked: 17 Nov '12, 23:23

Seen: 684 times

Last updated: 24 Nov '12, 16:18