当前位置:网站首页>RDF query language SPARQL

RDF query language SPARQL

2022-07-01 04:39:00 Necther

We have already introduced the semantic web technology stack RDF,RDFS/OWL. This time we will introduce the last core technical standard ——SPARQL(RDF,OWL and SPARQL The three core technologies called semantic web ).RDF It is essentially a data model , So how do we get there RDF Search on ? Similar use SQL Query relational databases , We use SPARQL Inquire about RDF Formatted data . This article first briefly introduces SPARQL The history of , Then we give several specific examples based on the data in our practice article .

One 、SPARQL

SPARQL namely SPARQL Protocol and RDF Query Language The recursive abbreviation of , Dedicated for access and operation RDF data , Is one of the core technologies of semantic web .W3C Of RDF Data access team (RDF Data Access Working Group, RDAWG) It's standardized . stay 2008 year ,SPARQL 1.0 Become W3C Official recommended standards .2013 Years issued SPARQL 1.1. Relative to the first version , It supports RDF Update of diagram , Provide more powerful queries , such as : Subquery 、 Aggregation operation ( Like we used to count) wait .

from SPARQL We can know the full name of , It consists of two parts : Protocol and query language .

1. The query language is easy to understand , It's like SQL Used to query data in a relational database ,XQuery Used for query XML data ,SPARQL Used for query RDF data .

2. Agreement means that we can pass HTTP The protocol is on the client side and SPARQL The server (SPARQL endpoint) Transfer queries and results between , This is also the biggest difference from other query languages .

One SPARQL Query is essentially a with variables RDF chart , Take Ronaldo as we mentioned before RDF Take the data :

<http://www.kg.com/person/1> <http://www.kg.com/ontology/chineseName> " Ronaldo · Louis · Nasario · Virtue · Lima "^^string.

We replace the attribute values with variables (SPARQL in , A variable is represented by a question mark and a variable name .), namely :

<http://www.kg.com/person/1> <http://www.kg.com/ontology/chineseName> ?x.

SPARQL Query is based on the idea of graph matching . We compare the above query with RDF Figure to match , Find all subgraphs that match the matching pattern , Finally, we get the value of the variable . For the above example , stay RDF When a matching subgraph is found in the graph , take " Ronaldo · Louis · Nasario · Virtue · Lima " and “?x” binding , We get the final result . In short ,SPARQL The query is divided into three steps :

1. Build query graph pattern , The expression is with variables RDF.

2. matching , Match to a subgraph that matches the specified graph pattern .

3. binding , Bind the result to the variable corresponding to the query graph pattern .

Two 、 Example

In the form of practice RDF Take movie data as an example , Let's introduce how to use SPARQL Inquire about :

1. be-all RDF A triple .

2. What movies did Stephen Chow play in ?

3. Who are the actors in the hero movie ?

4. Gong Li's performance score is greater than 7 What are your movies ?

How to query all data ? Refer to the query process we introduced in the first part , Query all the data that we don't have any known values ,SPO Each triplet is an unknown variable . Corresponding SPARQL The query language is :

PREFIX : <http://www.kgdemo.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <XML Schema>
PREFIX vocab: <http://localhost:2020/resource/vocab/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX map: <http://localhost:2020/resource/#>
PREFIX db: <http://localhost:2020/resource/>

SELECT * WHERE {
  ?s ?p ?o
}

SPARQL Some key words of :

  • SELECT, Specify the variables we want to query . Here we query all variables , use * Instead of .
  • WHERE, Specify the graph schema we want to query . Meaning and SQL Of WHERE There is no difference between .
  • FROM, Specify the query RDF Data sets . We have only one graph here , So it's not necessary FROM key word .
  • PREFIX, be used for IRI Abbreviation .

Here are some query results of this statement :

       s                   p             o

db:genre/12 [http]	:genreName	" adventure "
db:genre/12 [http]	rdf:type	:Genre
db:genre/14 [http]	:genreName	" fantasy "
db:genre/14 [http]	rdf:type	:Genre
db:genre/16 [http]	:genreName	" Animation "
db:genre/16 [http]	rdf:type	:Genre
db:genre/18 [http]	:genreName 	" The plot "
db:genre/18 [http]	rdf:type	:Genre
db:genre/27 [http]	:genreName	" Terror "
db:genre/27 [http]	rdf:type    :Genre

“ What movies did Stephen Chow play in ”:

PREFIX : <http://www.kgdemo.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <XML Schema>
PREFIX vocab: <http://localhost:2020/resource/vocab/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX map: <http://localhost:2020/resource/#>
PREFIX db: <http://localhost:2020/resource/>

SELECT ?n WHERE {
  ?s rdf:type :Person.
  ?s :personName ' Stephen Chow '.
  ?s :hasActedIn ?o.
  ?o :movieTitle ?n
}

Part of the result :

n

" Kung fu "
"琉 Glass bottle "
" True colour of a hero "
" Shaolin Soccer "
" Journey to the West 1001 - the moonlight treasure box "
" The Yangtze River seven "
" The grand finale of the journey to the West: the fate of fairy shoes "
" founding "
" Death judge "
" The dragon is at the end of the earth "
" Da Nei's secret agent sent nothing "

In our case , Can don't “?s rdf:type :Person”, Here is just to make the query graph more specific ( In a complex relationship RDF In the figure , There may be different classes with the same property name . such as , The attribute names of cat and dog names are "name", We want to inquire about a cat named Tom ; If the type is not specified , The return result may also contain a dog named Tom ). In diagram mode , Every RDF Split with English periods .

“ Who are the actors in the hero movie ”:

PREFIX : <http://www.kgdemo.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <XML Schema>
PREFIX vocab: <http://localhost:2020/resource/vocab/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX map: <http://localhost:2020/resource/#>
PREFIX db: <http://localhost:2020/resource/>

SELECT ?n WHERE {
  ?s rdf:type :Movie.
  ?s :movieTitle ' hero '.
  ?a :hasActedIn ?s.
  ?a :personName ?n
}

result :

n

" Jet Li "
" Tony Leung "
" Maggie Cheung "
" Zhang ziyi "
" Donnie Yen "

“ Gong Li's performance score is greater than 7 What are your movies ”:

PREFIX : <http://www.kgdemo.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <XML Schema>
PREFIX vocab: <http://localhost:2020/resource/vocab/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX map: <http://localhost:2020/resource/#>
PREFIX db: <http://localhost:2020/resource/>

SELECT ?n WHERE {
  ?s rdf:type :Person.
  ?s :personName ' Gong Li '.
  ?s :hasActedIn ?o.
  ?o :movieTitle ?n.
  ?o :movieRating ?r.
FILTER (?r >= 7)
}

result :

n

"2046"
"Memoirs of a Geisha"
" King of the Qin Dynasty "
" The red lantern is high "
" Farewell my concubine "
" Alive "
" Tang Baihu points autumn fragrance "
" Qiuju has a lawsuit "
" Inulin "
"Hong gao liang"
" Painting spirit "
" Wind and moon "
"Piao Liang Ma Ma"
"The Hand"

Here we use FILTER key word , You can constrain the value of variables .

SPARQL More detailed syntax and functions will not be introduced here . Readers can refer to W3C Of file perhaps SPARQL Of the query Example , There are also special books to explain SPARQL 1.1(Learning SPARQL: Querying and Updating with SPARQL 1.1)

And a little more , About the map of knowledge , There is a very important concept , The open world hypothesis (Open-world assumption,OWA). This assumption means that what is not stated at present is unknown , In other words, the information not contained in the knowledge map is unknown . How to understand ? First of all, we have to admit that the knowledge map cannot contain all the complete information . In the case of our movie data , Obviously , Its data is very incomplete . Even if we have a very complete picture of film knowledge , It contains all the movies of the moment 、 Actors and other information , In the real world , Information is also dynamically changing and growing . namely , We have to admit that the information of the knowledge map itself is incomplete . With this premise , Let's consider the second one in the example SPARQL sentence :

Stephen Chow starred in the movie in the above query results . Based on the film knowledge map we built , put questions to : Stephen Chow starred 《 Crouching tiger, hidden dragon 》 Do you ? according to OWA, The answer we get is “ I do not know! ”, contrary , If it is a closed world assumption (Closed-world assumption), The answer we get is “ No acting ”.

We need to consider the open world assumption when designing ontologies and developing related applications . A simple example , Question answering system based on knowledge map , User questions “ Stephen Chow starred 《 Crouching tiger, hidden dragon 》 Do you ?”, The appropriate answer is “ I do not know! ” instead of “ No acting ”. Intuitively this is the same as one person asking another this question , If we know the answer to the question , We will give a positive answer , If you don't know , We tend to reply “ I don't know ”,“ I'm not sure ”,“ Let me check ”, Instead of making a vow to answer “ No acting ”. After all , Most people have “ know one's limitations ”, Know that you always have something you don't know . From this point of view , People and knowledge maps are similar , We all exist in OWA In the world of .

3、 ... and 、 summary

This paper mainly introduces RDF query language SPARQL And its basic usage , Hope to make readers understand SPARQL Have a preliminary understanding . The next article is practice , How to use D2RQ establish SPARQL endpoint And make relevant queries on our data .

Reference material

1. Learn SPARQL - Cambridge Semantics

2. SPARQL By Example

3. SPARQL Query Language for RDF

原网站

版权声明
本文为[Necther]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160248205589.html