当前位置:网站首页>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
边栏推荐
- Difference between cookie and session
- 为什么香港服务器最适合海外建站使用
- 总结全了,低代码还需要解决这4点问题
- 测量三相永磁同步电机的交轴直轴电感
- 2022 t elevator repair question bank and simulation test
- Odeint et GPU
- Shell之一键自动部署Redis任意版本
- [ue4] event distribution mechanism of reflective event distributor and active call event mechanism
- Rule method: number of effective triangles
- 2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
猜你喜欢

2022危险化学品生产单位安全生产管理人员题库及答案

TCP server communication flow

Section 27 remote access virtual private network workflow and experimental demonstration

Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

Offline installation of Wireshark 2.6.10

Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)

Use winmtr software to simply analyze, track and detect network routing

嵌入式系统开发笔记79:为什么要获取本机网卡IP地址

2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
随机推荐
测量三相永磁同步电机的交轴直轴电感
Maixll-Dock 快速上手
2022年煤气考试题库及在线模拟考试
C language games (I) -- guessing games
What are permissions? What are roles? What are users?
Extension fragment
Pytorch(四) —— 可视化工具 Visdom
js 图片路径转换base64格式
软件研发的十大浪费:研发效能的另一面
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
【LeetCode】100. Same tree
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
25.k sets of flipped linked lists
Section 27 remote access virtual private network workflow and experimental demonstration
Grey correlation cases and codes
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
JS image path conversion Base64 format
[send email with error] 535 error:authentication failed
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?