简体   繁体   中英

Sparql query with java

A have an RDF file and I want to make a select SPARQL query that will select descriptions of a specific author. I tried a query like below but with no success. I am new to SPARQL so if anyone can help I would appreciate it…

MY TRY:

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

The RDF file looks like this:

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

Short Answer

The short answer is that the query isn't syntactically well formed, and that it doesn't return the things you're looking for. The query needs to terminate its triple pattern with a full stop ( . ), not a semicolon ( ; ). The fixed query should look something like this:

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

However, this SELECT s the variable ?description which isn't used in the query, so it's always empty. You probably either want to SELECT ?review or else add to the pattern schema:description ?description .

Long Answer

A bit more detail that shows that this works follows. The RDF you provided isn't well formed, so it's hard to test against it. I took what you provided, and wrote it up as N3 to get the following. (RDF/XML is at the end of the answer, too.)

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

The query, as I mentioned above, has some syntax errors; the triple pattern should end with a . , not a ; , and should be:

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

This doesn't return anything useful though, because ?description isn't used in the query. Using Jena's ARQ (and writing the query as schema.query) we output with one row (since the pattern matched), but with an unbound variable:

$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
  --query schema.query \
  --data schema.n3
---------------
| description |
===============
|             |
---------------

Here's a query that binds ?description :

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

As expected, it gives some useful results:

$ /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 Appendix

Here's the RDF/XML serialization of the data at hand.

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

I suppose the question is really on SPARQL, not java.

Have you tried if works writing:

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

or:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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