当前位置:网站首页>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
边栏推荐
- MySQL about auto increment sum cannot be empty
- L'enquête en aveugle a montré que les femmes étaient meilleures que les hommes.
- Remote connection raspberry pie in VNC Viewer Mode
- PayPal account has been massively frozen! How can cross-border sellers help themselves?
- 基础知识 | js基础
- [13. number and bit operation of 1 in binary]
- [batch dos-cmd command - summary and summary] - map folder to virtual disk - subst
- 盲测调查显示女码农比男码农更优秀
- 【10. 差分】
- JS uses the while cycle to calculate how many years it will take to grow from 1000 yuan to 5000 yuan if the interest rate for many years of investment is 5%
猜你喜欢

05 observer mode
![[12. maximum continuous non repeating subsequence]](/img/eb/230cd6062e28374c86863f2122e43b.png)
[12. maximum continuous non repeating subsequence]

What is a magnetic separator?

win命令行中导入、导出数据库相关表

js成绩奖惩例题

Cookie encryption 7 fidder analysis phase

Remote connection raspberry pie in VNC Viewer Mode
![【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法](/img/05/19299c47d54d4ede95322b5a923093.png)
【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法
![log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.](/img/2c/425993cef31dd4c786f9cc5ff081ef.png)
log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.

参考 | Win11 开启热点之后电脑不能上网
随机推荐
【c ++ primer 笔记】第3章 字符串、向量和数组
Cookie加密6
参考 | 升级 Win11 移动热点开不了或者开了连不上
Speech signal processing - concept (I): time spectrum (horizontal axis: time; vertical axis: amplitude), spectrum (horizontal axis: frequency; vertical axis: amplitude) -- Fourier transform -- > time
野风药业IPO被终止:曾拟募资5.4亿 实控人俞蘠曾进行P2P投资
Recognize the ordering of O (nlogn)
[11. two dimensional difference]
c#的初步认识
[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
[batch dos-cmd command - summary and summary] - output / display command - echo
参考 | Win11 开启热点之后电脑不能上网
【批处理DOS-CMD命令-汇总和小结】-环境变量、路径变量、搜索文件位置相关指令——set、path、where,cmd命令的路径参数中有空格怎么办
JS find the number of all daffodils
05 observer mode
File and multipartfile overview
JS output all prime numbers between 1-100 and calculate the total number
第6届蓝桥杯
Stream常用操作以及原理探索
【论文阅读】Intrinsically semi-supervised methods
JS print 99 multiplication table