sparql查询

参考资料:
SPARQL基础知识:https://blog.csdn.net/qq_22938671/article/details/89071370
RDF 和 SPARQL 初探:以维基数据为例
http://www.ruanyifeng.com/blog/2020/02/sparql.html
SPARQL——语义网的查询语言
https://blog.csdn.net/Jenny_oxaza/article/details/108540522
SparQL查询语法
https://zhuanlan.zhihu.com/p/524282156
使用SPARQL查看数据集中数据类型
https://cloud.tencent.com/developer/news/374001
sparql官方教程
https://www.w3.org/TR/sparql11-query/#propertypath-arbitrary-length
sparql路径查询
https://blog.csdn.net/qq_21120275/article/details/101295531

0、SPARQL查询语句的小技巧

1、当不知道该如何写查询语句的结构时,可以先将所有内容查询SELECT ?subject ?p ?object where { ?subject ?p ?object }出来,再写查询语句SELECT ?object where { bmm:bridge rdfs:comment ?object }
在这里插入图片描述
在这里插入图片描述

在protege中执行sparql属性值查询时,会显示属性值的数据类型(这可能是protege的sparql查询的插件的影响)。在jena fuseki server中执行sparql查询就不会显示属性值的数据类型。
在这里插入图片描述

1、SPARQL基础查询语句

日常代码记录
查询实例的类型(实例有默认类和自定义的所属类)
SELECT   ?object  
where {bmm:桥梁1 rdf:type ?object  }

在这里插入图片描述

联合查询

针对下文的自编的owl文件的查询

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX user: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>

//1、通过排序查询得到最大值
SELECT ?subject ?object
	WHERE { ?subject  user:age ?object}
ORDER BY DESC(?object) limit 1

//升序输出,可求最小
SELECT ?subject ?object
	WHERE { ?subject  user:age ?object}
ORDER BY ASC(?object) limit 2

//2、过滤查询 ?object.FILTER 中间点
SELECT ?subject ?object
	WHERE { ?subject  user:age ?object.FILTER(?object > 16)
}

//计数
SELECT   (COUNT(?subject) AS ?count_person)
	WHERE { ?subject  user:age ?object.FILTER(?object > 16)}

//嵌套查询,可以通过嵌套查询先筛选出2组数据,再查询两组数据的最大值,从而实现2组数据的比较
SELECT ?subject ?object
	WHERE 
{ 
SELECT ?subject ?object
	WHERE { ?subject  user:age ?object.FILTER(?object > 16)}

}

ORDER BY ASC(?object) limit 1


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

自编的owl文件

 <?xml version="1.0"?>
<rdf:RDF xmlns="http://www.co-ode.org/ontologies/pizza/pizza.owl#"
     xml:base="http://www.co-ode.org/ontologies/pizza/pizza.owl"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl">
        <rdfs:comment xml:lang="en">An example ontology that contains all constructs required for the various versions of the Pizza Tutorial run by Manchester University (see http://www.co-ode.org/resources/tutorials/)</rdfs:comment>
        <owl:versionInfo xml:lang="en">v.1.4. Added Food class (used in domain/range of hasIngredient), Added several hasCountryOfOrigin restrictions on pizzas, Made hasTopping invers functional</owl:versionInfo>
        <owl:versionInfo xml:lang="en">v.1.5. Removed protege.owl import and references. Made ontology URI date-independent</owl:versionInfo>
        <owl:versionInfo rdf:datatype="http://www.w3.org/2001/XMLSchema#string">version 1.5</owl:versionInfo>
    </owl:Ontology>
    


    <!-- 
    ///
    //
    // Data properties
    //
    ///
     -->

    


    <!-- http://www.co-ode.org/ontologies/pizza/pizza.owl#age -->

    <owl:DatatypeProperty rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#age">
        <rdfs:domain rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#People"/>
    </owl:DatatypeProperty>
    


    <!-- 
    ///
    //
    // Classes
    //
    ///
     -->

    


    <!-- http://www.co-ode.org/ontologies/pizza/pizza.owl#People -->

    <owl:Class rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#People"/>
    


    <!-- 
    ///
    //
    // Individuals
    //
    ///
     -->

    


    <!-- http://www.co-ode.org/ontologies/pizza/pizza.owl#刘飞 -->

    <owl:NamedIndividual rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#刘飞">
        <rdf:type rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#People"/>
        <age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">28</age>
    </owl:NamedIndividual>
    


    <!-- http://www.co-ode.org/ontologies/pizza/pizza.owl#张南 -->

    <owl:NamedIndividual rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#张南">
        <rdf:type rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#People"/>
        <age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">15</age>
    </owl:NamedIndividual>
    


    <!-- http://www.co-ode.org/ontologies/pizza/pizza.owl#张山南 -->

    <owl:NamedIndividual rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#张山南">
        <rdf:type rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#People"/>
        <age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">30</age>
    </owl:NamedIndividual>
</rdf:RDF>



<!-- Generated by the OWL API (version 4.5.9.2019-02-01T07:24:44Z) https://github.com/owlcs/owlapi -->




2、使用python对RDF文件进行sparql查询

python中用rdflib生成rdf,用sparql查询
https://blog.csdn.net/vuscity/article/details/79869828
如何使用SPARQL在OWL本体中插入新实例
https://cloud.tencent.com/developer/ask/sof/1555685
python包rdflib文档
https://rdflib.readthedocs.io/en/stable/
https://github.com/RDFLib/rdflib
Python第三方库——rdflib
https://blog.csdn.net/qq_46905110/article/details/116450208

9、注意事项

1、在protege 同时查询多个本体

通过protege 的 gather ontologies功能可以将多个本体文件加载进来实现同时查询多个本体,可以使用union语句
在这里插入图片描述

PREFIX test2: <http://www.semanticweb.org/zhangyang/ontologies/2022/6/test2#>
PREFIX test:<http://www.semanticweb.org/ZhangYang/ontologies/2022/6/BMMOntology#>

SELECT ?subject ?object
	WHERE { { ?subject  test2:haveCom  ?object } UNION { ?subject  test:ispartof  ?object } }
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值