当前位置:网站首页>SPARQL basic introductory exercise
SPARQL basic introductory exercise
2022-06-27 08:04:00 【Zjhao666】
1、 Preface
Knowledge map :Freebase, The server :virtuoso
SPARQL Basic grammar table

One SPARQL Examples
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?x
WHERE {
FILTER (?x != ns:m.0160w)
FILTER (!isLiteral(?x) OR lang(?x) = '' OR langMatches(lang(?x), 'en'))
ns:m.0160w ns:location.country.currency_used ?x .
}Roughly speaking
- PREFIX ns: <http://rdf.freebase.com/ns/>——PREFIX Declaration abbreviation , hinder ns Express http://rdf.freebase.com/ns/. for example ,ns:m.0160w When executed by the interpreter , Equivalent to http://rdf.freebase.com/ns/m.0160w.
- SELECT DISTINCT ?x—— Search for variables x(?x), And the search results require duplicate removal (DISTINCT)
- WHERE {}—— This clause follows SELECT after ,{} Content represents search SELECT Specific screening criteria for .
- FILTER (?x != ns:m.0160w)—— Required variable x No ns:m.0160w
- FILTER (!isLiteral(?x) OR lang(?x) = '' OR langMatches(lang(?x), 'en'))—— This clause is a bit complicated , It is a combination of three clauses , Respectively indicate requirements :
- or , Variable x It's not text (!isLiteral)
- or ,x The language of is an empty string
- or ,x My language is English (en)
- ns:m.0160w ns:location.country.currency_used ?x .—— This is actually a A triple . Said variable x There is an edge on , By entity ns:m.0160w Pointed out that , Through relationships ns:location.country.currency_used, Point to variable x. It is worth noting that , Any such triplet clause , It should be followed by an English full stop .( See red dot ), Otherwise, the interpreter will report an error .
stay virtuoso After the server executes , Format as html, give the result as follows .

Besides , It is worth noting that , about
SELECT DISTINCT ?xfor , If written as
SELECT DISTINCT ?x1 ?x2 ?x3Then it returns the following (where In Clause ) Of the constraints (x1, x2, x3) Combine .
2、 Basic operation ( From simple to complex )
2.1 Unconditional query
The simplest form
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT ?e1 ?r ?e2
where {?e1 ?r ?e2}This means , Without any restriction , Query all triples in the graph . Naturally, there are many returned results :

You can see To the top of the returned result , We also wrote our own name in the query statement e1,r,e2.
2.2 The conditional query of a single triplet is known
Add a triple constraint
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
}I.e. requirement Variable e1 and r, Satisfy triples (?e1 ?r ns:m.0160w). give the result as follows :

2.3 nested queries
Of course , We can also nest other triples ( nested queries ), such as
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
?e1 ns:freebase.user_profile.hometown ?e2 .
}obviously , Here we add two triple constraints . The number of results is naturally much less .

2.4 Filter parameters FILTER
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
FILTER(?r=ns:freebase.user_profile.hometown)
}Filter out ( namely , as long as This part ) Variable r yes ns:freebase.user_profile.hometown The situation of . give the result as follows :

2.5 Optional constraint OPTIONAL
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r ?e2
WHERE {
?e1 ?r ns:m.0160w .
OPTIONAL { ?e1 ns:freebase.user_profile.hometown ?e2 }
}such Words , The second triplet constraint is optional , If the conditions (?e1,ns:freebase.user_profile.hometown,?) establish , The query again ? Situated ?e2; If not , Just variables e2 empty . Some of the results are shown below :

so Results and 2.2 The conditional query of a single triplet is known , The results are almost consistent —— But in the end, there were more e2 A column of .
2.6 Any one of them can be satisfied UNION
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r ?e2
WHERE {
{ ?e1 ?r ns:m.0160w } UNION { ?e1 ns:freebase.user_profile.hometown ?e2 }
}so , Meet the three elements Group condition 1(?e1 ?r ns:m.0160w) Or conditions 2(?e1 ns:freebase.user_profile.hometown ?e2) Any one of , Just go back to . Some of the results are as follows :

Conditions 1 UNION Conditions 2, and , Conditions 1 OPTIONAL Conditions 2 The difference is :
- UNION Characterization “ or ” Relationship , Requirements 1,2 among At least one of them Conditions
- Conditions 1 OPTIONAL Conditions 2, Only required Conditions must be met 1; As for the conditions 2, If meet , The query is also executed incidentally .
2.7 Statistics count COUNT
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT COUNT DISTINCT ?e1 ?r ?e2
WHERE {
{ ?e1 ?r ns:m.0160w } UNION { ?e1 ns:freebase.user_profile.hometown ?e2 }
}Here and DISTINCT In combination with , That is to say, the statistics of the non repeated e1,r,e2 Number of combinations , give the result as follows :

2.8 Language qualification
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
?e1 ns:freebase.user_profile.hometown ?e2 .
FILTER(lang(?e1)!='zh')
}combination FILTER Realization , Filter out ( As long as this part ) Language is not chinese (zh) Of Variable e1 and r.

The result is similar to 2.3 Nested queries are consistent .
3、 Reference resources
Reference blog :
- sparql Summary of common query statements _TechOnly1988 The blog of -CSDN Blog _sparql intersection
- sparql Brief introduction - Introduction to grammar _minosisterry The blog of -CSDN Blog _sparql grammar
- sparql Brief introduction - Introduction to grammar _minosisterry The blog of -CSDN Blog _sparql grammar
边栏推荐
- 准备好迁移上云了?请收下这份迁移步骤清单
- 基础知识 | js基础
- js例题打印1-100之间所有7的倍数的个数及总和
- Common operation and Principle Exploration of stream
- File 与 MultipartFile概述
- Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
- 2、项目使用的QT组件
- Import and export database related tables from the win command line
- JS to determine whether the number entered by the user is a prime number (multiple methods)
- Speech signal processing - concept (4): Fourier transform, short-time Fourier transform, wavelet transform
猜你喜欢

【12. 最大连续不重复子序列】

Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?

(笔记)Anaconda-Navigator闪退解决方法

【c ++ primer 笔记】第4章 表达式

lvgl 说明3关于lvgl guider的使用

认识O(NlogN)的排序
![[11. two dimensional difference]](/img/b2/da624f8a7f97c46b8e346cf6d6da49.png)
[11. two dimensional difference]
![[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command](/img/05/19299c47d54d4ede95322b5a923093.png)
[batch dos-cmd command - summary and summary] - parameters%0,%1,%2,%[0-9],%0-9 in the batch command and batch command parameter position switching command shift, operator% usage in the DOS command

Game asset reuse: a new way to find required game assets faster

JS example print the number and sum of multiples of all 7 between 1-100
随机推荐
ACM course term summary
js用while循环计算假如投资多年的利率为5%,试求从1000块增长到5000块,需要花费多少年
Programming life - what do you think of the 35 year old bottleneck of programmers?
Binary tree structure and heap structure foundation
js中判断奇偶的函数,求圆面积的函数
准备好迁移上云了?请收下这份迁移步骤清单
Coggle 30 days of ML July competition learning
Common operation and Principle Exploration of stream
【论文阅读】Intrinsically semi-supervised methods
File and multipartfile overview
参考 | 升级 Win11 移动热点开不了或者开了连不上
js求所有水仙花数
[compilation principles] review outline of compilation principles of Shandong University
The 6th Blue Bridge Cup
Speech synthesis: tacotron explains [end-to-end speech synthesis model] [compared with traditional speech synthesis, it does not have complex phonetics and acoustic feature modules, but only uses < te
Preliminary understanding of C #
二叉树结构以及堆结构基础
期货反向跟单—交易员的培训问题
js中如何查看程序运行时间(计时器)
Implementation principle of similarity method in Oracle