用java进行Sparql查询

时间:2013-05-23 15:38:03

标签: java sparql

有一个RDF文件,我想做一个选择SPARQL查询,它将选择特定作者的描述。我尝试过如下查询,但没有成功。我是SPARQL的新手,所以如果有人可以提供帮助,我会很感激...

我的尝试:

String query = "PREFIX schema: <"+Constants.SCHEMA+"> \n" + 
               "SELECT ?description \n" +
               "WHERE { \n" +
                   "?review a schema:Review; \n" +
                   "schema:author \"Judson C.\"; \n" +
               "}"; 

RDF文件如下所示:

<rdf:Description rdf:nodeID="A3">
    <schema:ratingValue>5.0</schema:ratingValue>
    <rdf:type rdf:resource="http://schema.org/Rating" />
</rdf:Description>
<rdf:Description rdf:nodeID="A4">
    < schema:reviewRating rdf:nodeID="A5"/>
    <schema:description>OMG Love this place!! But glad I don't have to
        stand in line for an hour like back in the day to get my fix!</schema:description>
    <schema:datePublished>2013-05-18</schema:datePublished>
    <schema:author>Judson C.</schema:author>
    <rdf:type rdf:resource="http://schema.org/Review" />
</rdf:Description>
<rdf:Description rdf:nodeID="A6">
    <schema:reviewRating rdf:nodeID="A7" />
    <schema:description>If you're ever wondering of where to stuff your
        face with a good ol' sandwich, you'll definitely have to pop in at
        Ike's Place. Not only are the different sandwiches uniquely named,
        they're also uniquely flavor profiled. A wonderful experience
        indeed!Sandwiches I've personally ordered are the Love Triangle,
        Nacho girl, and the Al Bundy. For all you people out there who loved
        the tv show, Married with Children, I think you could appreciate the
        sentiment of having a sammie named after the 'No Ma'am enthusiast. I
        know I did. Ahhh... *tucks hand under pant waistband*All kidding
        aside, these sandwiches are pretty legit. You go to the counter and
        order whichever sandwich sounds appealing and they can either add
        chips or a drink to your meal. Simple. They have a wide range of
        sandwiches that are made to please any meat lover, vegetarian, or
        vegan palate alike. The huge variety is an A+ in my book. They can
        even individualize and customize your sandwich to your liking. This
        means that even the pickiest of eaters are welcome!So if you're
        interested in a fun place to enjoy a good sandwich, most definitely
        give 'em a try!</schema:description>
    <schema:datePublished>2013-04-30</schema:datePublished>
    <schema:author>Ann S.</schema:author>
    <rdf:type rdf:resource="http://schema.org/Review" />
</rdf:Description>

2 个答案:

答案 0 :(得分:2)

简答

简短的回答是,查询在语法上没有良好的形成,并且它不会返回您正在寻找的内容。查询需要使用句点(.)终止其三元模式,而不是分号(;)。固定查询应如下所示:

PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
  ?review a schema:Review ;
          schema:author "Judson C." .
}

但是,SELECT这个变量?description并未在查询中使用,因此它始终为空。您可能要么SELECT ?review要么添加到模式schema:description ?description

长答案

更多详细信息表明此作品如下。您提供的RDF格式不正确,因此很难对其进行测试。我拿了你提供的东西,并把它写成N3以获得以下内容。 (RDF / XML也在答案的最后。)

@prefix schema: <http://schema.org/> .

[]
  a schema:Rating ;
  schema:ratingValue "5.0" .

[]
  a schema:Review ;
  schema:reviewRating [] ;
  schema:description "OMG Love this place!!  But glad I don't have to stand in line for an hour like back in the day to get my fix!" ;
  schema:datePublished "2013-05-18" ;
  schema:author "Judson C." .

[]
 a schema:Review ;
 schema:reviewRating [] ;
 schema:description "If you're ever wondering of where to stuff your face with a good ol' sandwich, you'll definitely have to pop in at Ike's Place. Not only are the different sandwiches uniquely named, they're also uniquely flavor profiled. A wonderful experience indeed!Sandwiches I've personally ordered are the Love Triangle, Nacho girl, and the Al Bundy. For all you people out there who loved the tv show, Married with Children, I think you could appreciate the sentiment of having a sammie named after the 'No Ma'am enthusiast. I know I did. Ahhh... *tucks hand under pant waistband*All kidding aside, these sandwiches are pretty legit. You go to the counter and order whichever sandwich sounds appealing and they can either add chips or a drink to your meal. Simple. They have a wide range of sandwiches that are made to please any meat lover, vegetarian, or vegan palate alike. The huge variety is an A+ in my book. They can even individualize and customize your sandwich to your liking. This means that even the pickiest of eaters are welcome!So if you're interested in a fun place to enjoy a good sandwich, most definitely give 'em a try!" ;
 schema:datePublished "2013-04-30" ;
 schema:author "Ann S." .

如上所述,查询存在一些语法错误;三重模式应该以{{1​​}}结束,而不是.,并且应该是:

;

这并不会返回任何有用的内容,因为PREFIX schema: <http://schema.org/> SELECT ?description WHERE { ?review a schema:Review ; schema:author "Judson C." . } 未在查询中使用。使用Jena的ARQ(并将查询写为schema.query),我们输出一行(因为模式匹配),但是带有未绑定的变量:

?description

这是一个绑定$ /usr/local/lib/apache-jena-2.10.0/bin/arq \ --query schema.query \ --data schema.n3 --------------- | description | =============== | | --------------- 的查询:

?description

正如所料,它提供了一些有用的结果:

PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
  ?review a schema:Review ;
          schema:author "Judson C." ;
          schema:description ?description .
}

RDF / XML附录

这里是手头数据的RDF / XML序列化。

$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
  --query schema2.query \
  --data schema.n3
-------------------------------------------------------------------------------------------------------------------
| description                                                                                                     |
===================================================================================================================
| "OMG Love this place!!  But glad I don't have to stand in line for an hour like back in the day to get my fix!" |
-------------------------------------------------------------------------------------------------------------------

答案 1 :(得分:0)

我认为问题实际上是关于SPARQL,而不是java。

你是否尝试写作:

?review schema:author "Judson C."^^xsd:string

或:

?review schema:author ?author:
FILTER(str(?author)=="Judson C.")