当前位置:网站首页>SPARQL基础入门练习
SPARQL基础入门练习
2022-06-27 07:56:00 【Zjhao666】
1、前言
知识图谱:Freebase,服务器:virtuoso
SPARQL基本语法表

一个SPARQL样例
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 .
}大致意思是
- PREFIX ns: <http://rdf.freebase.com/ns/>——PREFIX声明缩写,后面的ns表示http://rdf.freebase.com/ns/。例如,ns:m.0160w在被解释器执行时,等价于http://rdf.freebase.com/ns/m.0160w。
- SELECT DISTINCT ?x——搜索变量x(?x),且搜索结果要求去重(DISTINCT)
- WHERE {}——这一子句接在SELECT之后,{}内容表示搜索SELECT的具体筛选条件。
- FILTER (?x != ns:m.0160w)——要求变量x不是ns:m.0160w
- FILTER (!isLiteral(?x) OR lang(?x) = '' OR langMatches(lang(?x), 'en'))——这一子句有点复杂,是三个子句的组合,分别表示要求:
- 或,变量x不是文本(!isLiteral)
- 或,x的语言是空字符串
- 或,x的语言是英文(en)
- ns:m.0160w ns:location.country.currency_used ?x .——这实际上是一个三元组。表示变量x上存在一条边,是由实体ns:m.0160w指出,通过关系ns:location.country.currency_used,指向变量x。值得注意的是,凡是这样的三元组子句,后面都要跟一个英文句号.(见红点),不然解释器会报错。
在virtuoso服务器执行后,设置格式为html,结果如下。

此外,值得注意的是,对于
SELECT DISTINCT ?x而言,若写为
SELECT DISTINCT ?x1 ?x2 ?x3则返回满足后续(where子句里的)约束条件的(x1, x2, x3)组合。
2、基本操作(由简单到复杂)
2.1 无条件查询
最简单的形式
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT ?e1 ?r ?e2
where {?e1 ?r ?e2}这表示,不加任何限制地,查询图谱内所有的三元组。返回结果自然就有很多:

可以看到返回结果的顶上,还写上了查询语句里我们自己命名的e1,r,e2。
2.2 已知单个三元组的条件查询
加上一个三元组的约束
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
}即要求变量e1和r,满足三元组(?e1 ?r ns:m.0160w)。结果如下:

2.3 嵌套查询
当然,我们还可以再嵌套其他的三元组(嵌套查询),比如
PREFIX ns: <http://rdf.freebase.com/ns/>
SELECT DISTINCT ?e1 ?r
WHERE {
?e1 ?r ns:m.0160w .
?e1 ns:freebase.user_profile.hometown ?e2 .
}显然,这里我们就加了两个三元组约束。结果的条数自然就少得多了。

2.4 过滤参数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)
}即过滤出(即,只要这部分)变量r是ns:freebase.user_profile.hometown的情况。结果如下:

2.5 可选的约束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 }
}这样的话,第二个三元组约束就是可选满足的了,即若条件(?e1,ns:freebase.user_profile.hometown,?)成立,再查询?处的?e2;若不成立,就变量e2空着。部分结果显示如下:

可见结果和2.2已知单个三元组的条件查询,的结果几乎是一致的——只是最后多了e2一列。
2.6 任满足其一即可的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 }
}可见,满足三元组条件1(?e1 ?r ns:m.0160w)或条件2(?e1 ns:freebase.user_profile.hometown ?e2)中任意一个,就返回。部分结果如下:

条件1 UNION 条件2,和, 条件1 OPTIONAL 条件2 的区别就在于:
- UNION刻画“或”关系,要求条件1,2当中至少满足其中一个条件
- 条件1 OPTIONAL 条件2,仅要求必须满足条件1;至于条件2,若满足,也顺带执行查询。
2.7 统计计数 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 }
}此处与DISTINCT配合使用,即统计满足条件的无重复的e1,r,e2组合数量,结果如下:

2.8 语言限定
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')
}结合FILTER实现,即过滤出(只要这部分)语言不是中文(zh)的变量e1和r。

可见结果跟2.3嵌套查询是一致的。
3、参考
参考博客:
边栏推荐
- 【批处理DOS-CMD命令-汇总和小结】-批处理命令中的参数%0、%1、%2、%[0-9]、%0-9和批处理命令参数位置切换命令shift,dos命令中操作符%用法
- 盲测调查显示女码农比男码农更优秀
- 【批处理DOS-CMD命令-汇总和小结】-环境变量、路径变量、搜索文件位置相关指令——set、path、where,cmd命令的路径参数中有空格怎么办
- Is futures reverse documentary reliable?
- After working in a large factory for ten years with an annual salary of 400000 yuan, I was suddenly laid off. If the company wanted to abandon you, it wouldn't leave any kindness
- [13. number and bit operation of 1 in binary]
- js打印99乘法表
- [batch dos-cmd command - summary and summary] - environment variables, path variables, search file location related instructions - set, path, where, what if there are spaces in the path parameters of
- log4j:WARN No such property [zipPermission] in org. apache. log4j. RollingFileAppender.
- Online text digit recognition list summation tool
猜你喜欢

八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?

c#的初步认识

野風藥業IPO被終止:曾擬募資5.4億 實控人俞蘠曾進行P2P投資

L'introduction en bourse de Wild Wind Pharmaceutical a pris fin: Yu pinzeng, qui avait l'intention de lever 540 millions de RMB, a effectué un investissement P2P.

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

Experience record of Luogu's topic brushing

爬一个网页的所有导师信息

Preliminary understanding of C #

JS to judge the odd and even function and find the function of circular area
![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.
随机推荐
Publications under nature, science and cell
ACM course term summary
Testing network connectivity with the blackbox exporter
js来打印1-100间的质数并求总个数优化版
Etcd教程 — 第五章 Etcd之etcdctl的使用
[batch dos-cmd command - summary and summary] - map folder to virtual disk - subst
无论LCD和OLED显示技术有多好,都无法替代这个古老的显示数码管
2022 love analysis · panoramic report of it operation and maintenance manufacturers
L'enquête en aveugle a montré que les femmes étaient meilleures que les hommes.
Mysql-8 download, installation and configuration tutorial under Windows
How to bind SQL statements to web buttons
JS print 99 multiplication table
[12. maximum continuous non repeating subsequence]
R language calculates Spearman correlation coefficient in parallel to speed up the construction of co occurrence network
参考 | 升级 Win11 移动热点开不了或者开了连不上
[notes on c++ primer] Chapter 3 string, vector and array
Cookie加密6
05 observer mode
js求所有水仙花数
PayPal账户遭大规模冻结!跨境卖家如何自救?