当前位置:网站首页>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
边栏推荐
- Knowledge supplement: redis' basic data types and corresponding commands
- JS rotation chart
- [leetcode skimming] February summary (updating)
- [godot] unity's animator is different from Godot's animplayer
- 2022-02-15 (399. Division evaluation)
- 2022 t elevator repair new version test questions and t elevator repair simulation test question bank
- Task04 mathematical statistics
- One click shell to automatically deploy any version of redis
- [human version] Web3 privacy game in the dark forest
- 尺取法:有效三角形的个数
猜你喜欢
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends

Knowledge supplement: redis' basic data types and corresponding commands

2022年化工自动化控制仪表操作证考试题库及答案

I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own

Custom components in applets

2022年聚合工艺考试题及模拟考试

Dual contractual learning: text classification via label aware data augmentation reading notes

JVM栈和堆简介

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

Openresty rewrites the location of 302
随机推荐
OSPF notes [dr and bdr]
Common UNIX Operation and maintenance commands of shell
测量三相永磁同步电机的交轴直轴电感
[deep learning] (4) decoder mechanism in transformer, complete pytoch code attached
2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
Maixll dock quick start
Task04 mathematical statistics
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
TASK04|数理统计
2022年煤气考试题库及在线模拟考试
什么是权限?什么是角色?什么是用户?
Note de développement du système embarqué 80: application du concepteur Qt à la conception de l'interface principale
25.k sets of flipped linked lists
Odeint and GPU
Internet winter, how to spend three months to make a comeback
OdeInt与GPU
Basic usage, principle and details of session
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch / 3.4.2 multiple registration protocol (MRP)
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
2022 gas examination question bank and online simulation examination