SPARQL basic mode query

Keywords: SQL Apache

The last one is a brief introduction to SPARQL. This article introduces the basic schema query of SPARQL

Still use the example from the previous article:

<rdf:RDF  
  xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'  
  xmlns:vCard='http://www.w3.org/2001/vcard-rdf/3.0#'  
   >  
  
  <rdf:Description rdf:about="http://somewhere/JohnSmith/">  
    <vCard:FN>John Smith</vCard:FN>  
    <vCard:N rdf:parseType="Resource">  
    <vCard:Family>Smith</vCard:Family>  
    <vCard:Given>John</vCard:Given>  
    </vCard:N>  
  </rdf:Description>  
  
  <rdf:Description rdf:about="http://somewhere/RebeccaSmith/">  
    <vCard:FN>Becky Smith</vCard:FN>  
    <vCard:N rdf:parseType="Resource">  
    <vCard:Family>Smith</vCard:Family>  
    <vCard:Given>Rebecca</vCard:Given>  
    </vCard:N>  
  </rdf:Description>  
  
  <rdf:Description rdf:about="http://somewhere/SarahJones/">  
    <vCard:FN>Sarah Jones</vCard:FN>  
    <vCard:N rdf:parseType="Resource">  
    <vCard:Family>Jones</vCard:Family>  
    <vCard:Given>Sarah</vCard:Given>  
    </vCard:N>  
  </rdf:Description>  
  
  <rdf:Description rdf:about="http://somewhere/MattJones/">  
    <vCard:FN>Matt Jones</vCard:FN>  
    <vCard:N  
    vCard:Family="Jones"  
    vCard:Given="Matthew"/>  
  </rdf:Description>  
  
</rdf:RDF>  
The picture shows:


RDF is known as some triples. For the previous example, if you want to find the Subject and Object of all FullName (predicate)

Use the following statement to find:

SELECT ?x ?name
WHERE {?x  <http://www.w3.org/2001/vcard-rdf/3.0#FN>  ?name}

We learned from the previous article that Select is used to find the required content, and Where statement is the search condition, which is similar to Sql statement.

Where {? x < http://www.w3.org/2001/vcard-rdf/3.0 ා FN >? Name} in this statement, "x" is the Subject to be searched, "name" is the Object to be searched, "http://www.w3.org/2001/vcard-rdf/3.0 ා FN > is the required condition.

The results are as follows:

----------------------------------------------------
| x                                | name          |
====================================================
| <http://somewhere/RebeccaSmith/> | "Becky Smith" |
| <http://somewhere/SarahJones/>   | "Sarah Jones" |
| <http://somewhere/JohnSmith/>    | "John Smith"  |
| <http://somewhere/MattJones/>    | "Matt Jones"  |
----------------------------------------------------

Isn't it so simple up to now. Come on, keep looking down...

What if we want to do two-step query? If you want to know that FamilyName is Smith's, who is GivenName? The answer is Rebecca and John

The query statement is as follows:

SELECT ?givenName
WHERE
  { ?y  <http://www.w3.org/2001/vcard-rdf/3.0#Family>  "Smith" .
    ?y  <http://www.w3.org/2001/vcard-rdf/3.0#Given>  ?givenName .
  }
Results:

-------------
| givenName |
=============
| "John"    |
| "Rebecca" |
-------------
We can also search for blank nodes: for example, to find a blank node whose FamilyName is Smith:

Query code:

SELECT ?y ?givenName
WHERE
 { ?y vcard:Family "Smith" .
   ?y vcard:Given  ?givenName .
 }
Results:

--------------------
| y    | givenName |
====================
| _:b0 | "John"    |
| _:b1 | "Rebecca" |
--------------------
By the way, I forgot to use SparQL with Jena package, which can be downloaded from http://jena.apache.org/download/index.cgi.




Posted by Levan on Fri, 01 May 2020 22:27:02 -0700