繁体   English   中英

用java进行Sparql查询

[英]Sparql query with java

A有一个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>

简答

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

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." .

正如我上面提到的,查询有一些语法错误; 三重模式应以a结尾. 不是; ,应该是:

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

这不会返回任何有用的东西,因为?description中没有使用?description 使用Jena的ARQ(并将查询编写为schema.query),我们输出一行(因为模式匹配),但是带有未绑定的变量:

$ /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 .
}

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

$ /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!" |
-------------------------------------------------------------------------------------------------------------------

RDF / XML附录

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

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:schema="http://schema.org/">
  <schema:Review>
    <schema:reviewRating rdf:parseType="Resource">
    </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:description>
    <schema:datePublished>2013-04-30</schema:datePublished>
    <schema:author>Ann S.</schema:author>
  </schema:Review>
  <schema:Review>
    <schema:reviewRating rdf:parseType="Resource">
    </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:description>
    <schema:datePublished>2013-05-18</schema:datePublished>
    <schema:author>Judson C.</schema:author>
  </schema:Review>
  <schema:Rating>
    <schema:ratingValue>5.0</schema:ratingValue>
  </schema:Rating>
</rdf:RDF>

我想这个问题实际上是关于SPARQL,而不是java。

如果作品写作你有没有尝试过:

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

要么:

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

暂无
暂无

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

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