sparql查询语句的写法

一.

在这个网址里面去做一个查询,后面出来的栏目,是select里面标明的,对于?book是自己重命名的显示的字符,rdf:type  dbo:Book是一个整体的命名格式吧,这个会出来它的网页如下图:


只有book一栏,点进去,会看到很多具体的关键字,对于别的关键字的查询构建方法是比如对于rdfs:comment这个属性,自己构建的查询语句:

这样才可以出现2的属性(也就是SELECT里面也要相应的加进去,对于其他属性和这个重命名的调用方式差不多,注意里面标红的,还有这个在线查询的网址也要注意),也就是只有第一个类比较特殊,别的类都和第二个类差不多,后面是重命名,可以随意重命名)。第一行的是一个完整的主谓宾,第二行没有主语,所以上面使用分号,默认使用的是上一行的主语,如果下面的带了主语,则可以使用英文的句号,如下图:

    

对于整体的dbo的类型,一般都是对应的英语名,第一个字母大写即可,比如:图书Book,电影Film,游戏Game等等。

二.以上程序在Python里面的实现就是这样的

    

#import urllib2
#from datatime import datatime
from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://dbpedia.org/sparql")

sparql.setQuery("""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT?book ?com
WHERE { 
        ?book rdf:type dbo:Book;
        rdfs:comment   ?com.

      }

""")
#英语的过滤语言的简写是EN,在这里中文语言是ZH,FILTER是一个过滤器
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
#result_1=JSON.dumps(results)
print(results)
#for result in results["results"]["bindings"]:
    #print(result["name"]["value"],result["date"]["value"],result["abstract"]["value"],"\n")
下面具体怎么样输出,等自己去看他的json结构再输出。

三.一个比较完整的sparql查询语句的构建(不是上面的例子,但是功能上面的比较完整,还有过滤,排序等的信息)

扫描二维码关注公众号,回复: 1900168 查看本文章

import urllib
from datetime import datetime
from datetime import *
from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF

sparql = SPARQLWrapper("http://dbpedia.org/sparql")

sparql.setQuery("""
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>

    Select distinct ?birthdate ?thumbnail ?scientist ?name ?description WHERE {
    ?scientist rdf:type dbo:Scientist ;
               dbo:birthDate ?birthdate ;
               rdfs:label ?name ;
               dct:description ?description
    FILTER ((lang(?name)="en")&&(lang(?description)="en")&&(STRLEN(STR(?birthdate))>6)&&(SUBSTR(STR(?birthdate),6)=STR("05-14")) ).
    OPTIONAL { ?scientist dbo:thumbnail ?thumbnail .} 
    } ORDER BY ?birthdate
    """)

sparql.setReturnFormat(JSON)
results = sparql.query().convert()
四.对于定义命名的注意事项

    

    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>

        这些算是重命名(重定向),也就是dbo是这样自己重新定义的,别的也类似,所以三中的下面的调用就用这个形式,也就是可以使用原始网页的命名方式,也可以自己利用重命名的方式去写,如下图所示:


五.对于里面有很多个类需要查询的情况

book_sel="""
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
    
    select distinct ?bookURI ?bookName ?authorName 
                         ?authorMovement ?bookGenre ?bookAbstract 
                         where { ?bookURI rdf:type dbo:Book . 
                                ?bookURI  dbo:author ?author .
                                ?bookURI  dbo:abstract ?bookAbstract .  
                                ?bookURI rdfs:label ?bookName .
                                ?author rdfs:label ?authorName . 
                           FILTER (lang(?authorName) = \"en\" && lang(?bookName) = \"en\" && 
                           lang(?bookAbstract) = \"en\").
                           }LIMIT 100"""
sparql.setQuery(book_sel)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
#以上这些,是为了有多个需要查询的sparql语句时,不知道选择哪个,那么而这样去构建

里面的LIMIT只是一个限制条件,为了查询的更快,限制查询100个就好了,目的只是为了测试一下

六.对于匹配的查询,就是让某一项等于一个什么具体的值去进行筛选

game_sel = """
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX dc: <http://purl.org/dc/elements/1.1/>
    select distinct ?bookURI ?bookName ?authorName 
                          ?bookAbstract 
                         where { ?bookURI rdf:type dbo:Game . 
                                ?bookURI  dbo:genre ?author .
                                ?author rdfs:label ?authorName .
                                ?bookURI  dbo:abstract ?bookAbstract . 
                                ?bookURI rdfs:label ?bookName .

            FILTER (lang(?authorName) = \"en\"  && lang(?bookName) = \"en\" && 
            lang(?bookAbstract) = \"en\" && regex((?bookName),"%s")).
                           }
                        """%("Ars Magica")
#其中的关键之处在于,不是之前的“==”的形式,而是一个正则的表达方式,和print("%d",i)的思路,如下:

猜你喜欢

转载自blog.csdn.net/qinglv1/article/details/80720148