SPARQL Examples

From Devwiki
Revision as of 13:53, 19 August 2019 by Tvf (talk | contribs) (Example SPARQL Queries)
(diff) ←Older revision | view current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Example SPARQL Queries

Find PID for phaseCode

select ?pid from <#ri> where {
 ?pid <http://dev.llgc.org.uk/digitisation/identifiers/phaseCode> "ACCWP060" .
 } 

Find PID for original file name

select ?pid from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/original_name> "pjs00001" .
} 

Find PID for Project code

select ?pid from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/projectCode> "PJS" .
}

Find PID for Catalogue record

select ?pid from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/catalogue> "vtls004432228" .
}

Find PIDs and original file names

select ?pid ?original_name from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/projectCode> "ILW" .
?pid <http://dev.llgc.org.uk/digitisation/identifiers/original_name> ?original_name .
}

Find PhaseCode, PID and Title from DC datastream when Phase code starts with PJS

select ?phase ?pid ?title from <#ri> where {
  ?pid <http://dev.llgc.org.uk/digitisation/identifiers/phaseCode> ?phase .
FILTER(contains(?phase, 'PJS'))
 ?pid <dc:title> ?title .
 }

Find PIDs with specific Rights assigned to object

select ?pid from <#ri> where {
 ?pid <http://web.resource.org/cc/license> <http://creativecommons.org/licenses/by-nc-sa/4.0/> .
 }

These are mostly of use internally but may help others to see examples. These are mostly SPARQL 1.0 compliant as this is what Fedora 3.3 uses.


Find all Newspaper titles which aren't part of the Europeana colleciton Had to run two sparql queries and diff the results:

Query to get all Newspaper titles:

select ?title ?code  from <#ri> where {
   ?title <fedora-model:hasModel> <info:fedora/model:scif-newspapers-title> .
   ?title <http://dev.llgc.org.uk/digitisation/identifiers/nlw_id> ?code 
}order by ?code

Query to get all titles which are part of the europeana collection:

select ?title ?code  from <#ri> where {
   ?title <fedora-model:hasModel> <info:fedora/model:scif-newspapers-title> .
   ?title <http://dev.llgc.org.uk/digitisation/identifiers/nlw_id> ?code  .
   ?title <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:europeana-newspaper-title> 
}order by ?code

Query to find fedora sdefs and bdeps for pid:

select $model $sdef $sdep from <#ri> where{
  <info:fedora/llgc-id:2035358> <info:fedora/fedora-system:def/model#hasModel> $model .
  $model <info:fedora/fedora-system:def/model#hasService> $sdef .
  $sdep <info:fedora/fedora-system:def/model#isDeploymentOf> $sdef .
  $sdep <info:fedora/fedora-system:def/model#isContractorOf> $model
}

Query to get wiki data back for images:

SELECT ?object ?objectLabel ?handle ?dateCreated ?creator ?creatorLabel ?creatorID ?dipictsLabel ?coords
WHERE
{
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
   ?object wdt:P361 wd:Q21542493 .
   ?object wdt:P1184 ?handle .
   optional { 
      ?object wdt:P170 ?creator
      optional { ?creator wdt:P214 ?creatorID}
   }
   optional { ?object wdt:P571 ?dateCreated }
   optional { 
     ?object wdt:P180 ?dipicts  .
     ?dipicts wdt:P625 ?coords   
   } 
} order by ?handle

Note: label is created using fieldnameLabel the capital L is important in Label

Query to get back dipicts information including people and places:

SELECT ?object ?objectLabel ?handle ?id ?dateCreated ?creator ?creatorLabel ?creatorID ?typeLabel ?dipicts ?dipictsLabel ?coords
WHERE
{
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
  ?object wdt:P361 wd:Q21651637 .
  optional { ?object wdt:P1184 ?handle }
  optional { ?object wdt:P217 ?id }
  optional { 
     ?object wdt:P170 ?creator
     optional { ?creator wdt:P214 ?creatorID}
  }
  optional { ?object wdt:P571 ?dateCreated }
  optional { 
    ?object wdt:P180 ?dipicts  .
    ?dipicts wdt:P31 ?type
    optional {
    ?dipicts wdt:P625 ?coords
             }
  } 
} order by ?handle

--GlenRobson 10:18, 24 January 2017 (GMT)