远程远程端点 RDFLib / Redland 上的 SPARQL 查询

发布于 2024-11-05 17:12:10 字数 1485 浏览 1 评论 0原文

我正在尝试查询远程端点并获取 owl:sameAs 映射,我尝试了 RDFLib 和 Redland,但都不适合我,可能我没有正确处理名称空间。

这是我在 RDFLib 中的尝试:

    import rdflib

    rdflib.plugin.register('sparql', rdflib.query.Processor, 'rdfextras.sparql.processor', 'Processor')
    rdflib.plugin.register('sparql', rdflib.query.Result, 'rdfextras.sparql.query', 'SPARQLQueryResult')

    g = rdflib.Graph()

    query = """
        SELECT *
        FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
        WHERE {
             ?s a http://purl.org/ontology/mo/MusicArtist;
                http://www.w3.org/2002/07/owl#sameAs ?o .
        }Limit 50
    """

    for row in g.query(query):
        print row

这是 Redland:

import RDF
model = RDF.Model()

query = """
    SELECT *
    FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
    WHERE {
         ?s a http://purl.org/ontology/mo/MusicArtist;
            http://www.w3.org/2002/07/owl#sameAs ?o .
    }Limit 50
"""

for statement in RDF.Query(query ,query_language="sparql").execute(model):
    print statement

您能否提示其中任何一个有什么问题? 我遇到的另一个困难是:是否可以获得对象的数据集名称?例如:如果有:

?s = http://www.bbc.co.uk/music/artists/eb5c8564-927d-414d-b152-c7b48a2c9d8b#artist
predicate = http://www.w3.org/2002/07/owl#sameAs
?0 = http://dbpedia.org/resource/The_Boy_Least_Likely_To

我可以获取此示例中“Dbpedia”的名称吗?或者我有 SameAs 链接的任何其他数据集? (或者我可能可以在对象字符串中查找感兴趣的数据集名称)提前非常非常感谢

I'm trying to query remote endpoints and get get owl:sameAs mappings, I've tried both RDFLib and Redland but neither worked for me, probably I'm not dealing with namespaces correctly.

Here is my attempt in RDFLib:

    import rdflib

    rdflib.plugin.register('sparql', rdflib.query.Processor, 'rdfextras.sparql.processor', 'Processor')
    rdflib.plugin.register('sparql', rdflib.query.Result, 'rdfextras.sparql.query', 'SPARQLQueryResult')

    g = rdflib.Graph()

    query = """
        SELECT *
        FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
        WHERE {
             ?s a http://purl.org/ontology/mo/MusicArtist;
                http://www.w3.org/2002/07/owl#sameAs ?o .
        }Limit 50
    """

    for row in g.query(query):
        print row

And here is Redland:

import RDF
model = RDF.Model()

query = """
    SELECT *
    FROM <http://api.talis.com/stores/bbc-backstage/services/sparql>
    WHERE {
         ?s a http://purl.org/ontology/mo/MusicArtist;
            http://www.w3.org/2002/07/owl#sameAs ?o .
    }Limit 50
"""

for statement in RDF.Query(query ,query_language="sparql").execute(model):
    print statement

Can you please give a hint what is wrong in any one of those?
Yet another difficulty I have: Is it possible to get dataset name of the object? For example: if there is:

?s = http://www.bbc.co.uk/music/artists/eb5c8564-927d-414d-b152-c7b48a2c9d8b#artist
predicate = http://www.w3.org/2002/07/owl#sameAs
?0 = http://dbpedia.org/resource/The_Boy_Least_Likely_To

Can I get name of the "Dbpedia" in this example? Or any other dataset to which I'm having sameAs link? (Or probably I could just look-up interested dataset names in the object string) thank you very VERY much in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝咒 2024-11-12 17:12:10

各种事情:

你是对的,你需要将任何 URI 括在 < > 中。正确的查询是:

SELECT ?s ?o WHERE {
         ?s a <http://purl.org/ontology/mo/MusicArtist>;
            <http://www.w3.org/2002/07/owl#sameAs> ?o .
    } limit 50

... 查看结果 这里

FROM 并没有像你想象的那样在 rdflib 或 redland 中实现。它不会获取远程 SPARQL 端点,而是获取远程图或本地存储中具有该名称的图。在您的情况下,您想要使用 SERVICE 请参阅它如何与 Jena 一起使用< /a>.不幸的是,rdflib 和 redland 都没有为 SPARQL 实现 SERVICE 子句,但有一些解决方法可以解决这个问题。

一种可能的解决方案是使用 SPARQLWrapper for python。这很简单,这里有该库的代码:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://api.talis.com/stores/bbc-backstage/services/sparql")
sparql.setQuery("""
    SELECT ?s ?o
    WHERE {
         ?s a <http://purl.org/ontology/mo/MusicArtist>;
            <http://www.w3.org/2002/07/owl#sameAs> ?o .
    } limit 50
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print result["s"]['value'], result["o"]['value']

如您所见,远程 SPARQL 端点成为查询外部的参数。

Various things:

You are right, you need to enclose any URI within < >. The correct query is:

SELECT ?s ?o WHERE {
         ?s a <http://purl.org/ontology/mo/MusicArtist>;
            <http://www.w3.org/2002/07/owl#sameAs> ?o .
    } limit 50

... see the results here.

FROM is not implemented in rdflib or redland as you think it is. It does not fetch remote SPARQL endpoints it fetches remote graphs or graphs with that name in a local store. In your case you want to use SERVICE see how it works here with Jena. Unfortunately, neither rdflib nor redland implement the SERVICE clause for SPARQL but there are workarounds to sort this out.

One possible solution is to use SPARQLWrapper for python. It is trivial, here you have your code with that library:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("http://api.talis.com/stores/bbc-backstage/services/sparql")
sparql.setQuery("""
    SELECT ?s ?o
    WHERE {
         ?s a <http://purl.org/ontology/mo/MusicArtist>;
            <http://www.w3.org/2002/07/owl#sameAs> ?o .
    } limit 50
""")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print result["s"]['value'], result["o"]['value']

As you can see the remote SPARQL endpoint becomes a parameter outside the query.

残花月 2024-11-12 17:12:10

Redland 目前不支持在 FROM 中使用 SPARQL 端点。您在这里使用的是加载到 RDF 数据集中的图形名称。当您使用 model.context_add_statement(statement, context) 加载三元组 (s, p, o) + c 时,也称为 redland 上下文

Rasqal GIT 确实支持解析 SERVICE但尚未在查询中执行它。

Redland does not currently support using SPARQL endpoints in FROM. What you are using here are are graph names that you load into the RDF Dataset. Also known as redland contexts when you load a triple (s, p, o) + c with something like model.context_add_statement(statement, context)

Rasqal GIT does support parsing SERVICE but not yet executing it in a query.

放手` 2024-11-12 17:12:10

您还可以考虑使用 Virtuoso 与 RedLand,因为它实现了 SPARQL-FED用于远程查询执行的“服务”参数,如这些 在线示例

You could also consider using Virtuoso with RedLand as it implement the SPARQL-FED "Service" param for remote query execution as demonstrated in these online examples

谁的年少不轻狂 2024-11-12 17:12:10

博客条目中有另一个简单的解决方案 http://terse-words.blogspot.com/2012/01/get-real-data-from-semantic-web.html 这使代码相当干净。它还使用 SPARQLWrapper。

There's another simple solution in the blog entry at http://terse-words.blogspot.com/2012/01/get-real-data-from-semantic-web.html which keeps the code fairly clean. It uses SPARQLWrapper as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文