简体   繁体   English

如何使用SPARQL查询RDF三元组列表?

[英]How to query for a list of RDF triples using SPARQL?

I have an RDF database (AFAIK) where I store Java objects which are mapped to RDF triples. 我有一个RDF数据库(AFAIK),其中存储了映射到RDF三元组的Java对象。

What I want to do now is query for a list of all objects of a specific type. 我现在想做的是查询特定类型的所有对象的列表。

I imagine something like this, where I have put in my imagination of the syntax: 我想象这样的事情,我在语法中有了自己的想像力:

PREFIX xyz: <http://foo.com/myclassname#>
SELECT *
WHERE {
  xyz:* name ?name
}

I want to say that I want all triples which represent names of the subject with type myclassname. 我想说的是,我希望所有三元组都代表类型为myclassname的主题名称。 How to do this correctly? 如何正确做到这一点?

Right now I can't give you error messages, because I do not have a running system, but I'm sure I do not understand SPARQL queries well enough, so please shed some light on this. 目前,我没有错误消息,因为我没有正在运行的系统,但是我确定我不太了解SPARQL查询,因此请对此有所了解。

SPARQL queries are generally in the form ?subject ?predicate ?object . SPARQL查询通常采用“ ?subject ?predicate ?object的形式。 So you need to run a simple query like this to know if you need to put your restriction in the place of ?Predicate or you need to add another triple ( ?object ?x ?y ) to your query. 因此,您需要运行一个像这样的简单查询,以了解是否需要将限制放在?Predicate位置,还是需要向查询中添加另一个三元组( ?object ?x ?y )。 Please refer to SPARQL for begginers for more examples. 请参阅SPARQL了解更多示例。

Your answer in short is that yes, you can query based on a specific type, but you need to know where to put this type restriction. 简而言之,您的答案是,可以基于特定类型进行查询,但是您需要知道在何处放置此类型限制。

So for example in DBpedia if you want to get the labels of everything you can say: 因此,例如在DBpedia中,如果要获取所有内容的标签,可以说:

select distinct *
where 
    {?s rdfs:label ?o}
LIMIT 100

If you want this label to be a name, you need to specify the type of your object: 如果希望此标签为名称,则需要指定对象的类型:

select distinct *
where 
    {?s rdfs:label ?o.
    ?s a dbpedia-owl:Person}
 LIMIT 100

The exact syntax of the SPARQL query depends on how exactly your data is modeled in RDF. SPARQL查询的确切语法取决于RDF中数据建模的准确性。 Let's assume that objects of a class MyClass are represented as follows in your RDF database, and that they have a name property my:name : 假设在您的RDF数据库中,类MyClass对象表示如下,并且它们的名称属性为my:name

@prefix my: <http://example.org/my/> .

my:object1 a my:MyClass . 
my:object1 my:name "object 1" .

A query to get back all objects and their names modeled in this exact fashion would look like this: 检索以这种完全相同的方式建模的所有对象及其名称的查询如下所示:

PREFIX my: <http://example.org/my/>

SELECT ?x ?name
WHERE { 
     ?x rdf:type my:MyClass ;
        my:name ?name .
}

SPARQL queries work as graph pattern matchers : the query looks for any combination of triples matching the pattern specified in the WHERE-clause: so we're looking for two triples, one expressing that the rdf:type property has the value my:MyClass , and one expressing that another triple with the same subject should have a property called my:name , of which we want back the value (bound to the variable ?name ). SPARQL查询用作图形模式匹配器 :查询查找与WHERE子句中指定的模式匹配的三元组的任何组合:因此,我们正在寻找两个三元组,一个表示rdf:type属性的值为my:MyClass ,一个表示另一个具有相同主题的三元组应该具有一个名为my:name的属性,我们想要返回该属性的值(绑定到变量?name )。

As you can see, it's dependent on your specific RDF model: in your database, the name of the class ( my:MyName ) and the name of the property ( my:name ) are probably different. 如您所见,它取决于特定的RDF模型:在数据库中,类的名称( my:MyName )和属性的名称( my:name )可能不同。 So you will need to find out how exactly your data is modeled as RDF before you can write a query that works in your specific case. 因此,在编写适用于特定情况的查询之前,您需要了解将数据如何精确地建模为RDF。

Edit as an aside: SPARQL has a shorthand notation for the property rdf:type , which is a . 放在一边编辑 :SPARQL具有rdf:type属性的缩写符号,它是a So the bit ?x rdf:type my:MyClass is often written as ?xa my:MyClass . 因此, ?x rdf:type my:MyClass通常写为?xa my:MyClass I wanted to stress the fact that you are querying triples/properties, which is why I used the actual property name. 我想强调一下您正在查询三元组/属性的事实,这就是为什么我使用实际的属性名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM