问题
I would like to write this SPARQL query in Java using Jena:
prefix dbpediaont: <http://dbpedia.org/ontology/>
prefix dbpedia: <http://dbpedia.org/resource/>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?resource where {
dbpedia:Fred_Guy rdf:type ?resource
filter strstarts(str(?resource), "http://dbpedia.org/ontology")
}
I'm using this code:
public QueryExecution query(){
String stringa = "http://dbpedia.org/resource/Fred_Guy";
ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
"prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
"prefix dbpedia: <http://dbpedia.org/resource/>\n" +
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"\n" +
"select ?resource where {\n" +
"?mat rdf:type ?resource\n" +
"filter strstarts(str(?resource), http://dbpedia.org/ontology)\n" +
"}" );
Resource risorsa = ResourceFactory.createResource(stringa);
qs.setParam( "mat", risorsa );
//System.out.println( qs );
QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );
ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );
while ( results.hasNext() ) {
System.out.println( results.next().get( "resource" ));
}
// A simpler way of printing the results.
ResultSetFormatter.out( results );
return exec;
}
I get this error:
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Line 7, column 34: Unresolved prefixed name: http:
at com.hp.hpl.jena.sparql.lang.ParserBase.throwParseException(ParserBase.java:661)
at com.hp.hpl.jena.sparql.lang.ParserBase.resolvePName(ParserBase.java:274)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.PrefixedName(SPARQLParser11.java:4892)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.iri(SPARQLParser11.java:4872)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.iriOrFunction(SPARQLParser11.java:4674)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.PrimaryExpression(SPARQLParser11.java:3887)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.UnaryExpression(SPARQLParser11.java:3802)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.MultiplicativeExpression(SPARQLParser11.java:3669)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.AdditiveExpression(SPARQLParser11.java:3567)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.NumericExpression(SPARQLParser11.java:3560)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.RelationalExpression(SPARQLParser11.java:3492)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.ValueLogical(SPARQLParser11.java:3485)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.ConditionalAndExpression(SPARQLParser11.java:3464)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.ConditionalOrExpression(SPARQLParser11.java:3443)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Expression(SPARQLParser11.java:3436)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.BuiltInCall(SPARQLParser11.java:4108)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Constraint(SPARQLParser11.java:2283)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Filter(SPARQLParser11.java:2211)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.GraphPatternNotTriples(SPARQLParser11.java:1888)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.GroupGraphPatternSub(SPARQLParser11.java:1765)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.GroupGraphPattern(SPARQLParser11.java:1702)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.WhereClause(SPARQLParser11.java:446)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.SelectQuery(SPARQLParser11.java:134)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.Query(SPARQLParser11.java:50)
at com.hp.hpl.jena.sparql.lang.sparql_11.SPARQLParser11.QueryUnit(SPARQLParser11.java:41)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11$1.exec(ParserSPARQL11.java:49)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:98)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:139)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at com.hp.hpl.jena.query.ParameterizedSparqlString.asQuery(ParameterizedSparqlString.java:1384)
at MyPackage.Test.query(Test.java:769)
The error is on "http://dbpedia.org/ontology" in strstarts FILTER, because it because it must be between "", right? How I can write this code in Java? If I write "http://dbpedia.org/ontology" between "", the first " is seen from code as closing of the query.
What am I doing wrong here?
回答1:
I have resolved my problem using this code:
public QueryExecution query(){
String stringa = "http://dbpedia.org/resource/Fred_Guy";
ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
"prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
"prefix dbpedia: <http://dbpedia.org/resource/>\n" +
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"\n" +
"select ?resource where {\n" +
"?mat rdf:type ?resource\n" +
"filter strstarts(str(?resource), dbpediaont:)\n" +
"}" );
Resource risorsa = ResourceFactory.createResource(stringa);
qs.setParam( "mat", risorsa );
//System.out.println( qs );
QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );
ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );
while ( results.hasNext() ) {
System.out.println( results.next().get( "resource" ));
}
// A simpler way of printing the results.
ResultSetFormatter.out( results );
return exec;
}
In particular, in FILTER as the second arguments, I have written the "dbpediaont" label.
This code, however, still produces an error:
Exception in thread "main" HttpException: 500
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:340)
at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:276)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:345)
This error is due to the fact that the code need to write
dbpediaont:
with
str()
since it's an IRI, not a string:
filter strstarts(str(?resource), str(dbpediaont:))
The latter problem is reported on HttpException error when I call SPARQL query (on DBPedia) in Java Code
I hope this answer can help someone.
来源:https://stackoverflow.com/questions/24576599/to-write-a-query-sparql-in-java-code-using-strstarts-filter