Reputation: 1919
I have in Java the following code, that uses the Virtuoso Jena provider API. I would like to do a query using an alias. I tried putting parenthesis but nothing worked. If I put the same query in the endpoint directly it works.
query = "select ?a AS ?count where {?a <uri> ?b.} limit 10";
VirtGraph set = new VirtGraph (url, "user", "pass");
Query sparql = QueryFactory.create(query);
VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparql, set);
vqe.execSelect();
I recieve this error:
com.hp.hpl.jena.query.QueryParseException:
Lexical error at line 1, column 13. Encountered: " " (32), after : "AS"
at com.hp.hpl.jena.sparql.lang.ParserSPARQL.perform(ParserSPARQL.java:99)
The SPARQL syntax for an alias is: (?var AS ?alias)
What syntax should I use in the query to make it work with an alias?
Thanks.
Upvotes: 1
Views: 706
Reputation: 9434
I see you also asked this on the OpenLink Software Support Forums... (ObDisclaimer: I work for OpenLink Software.)
I've also posted this answer there.
The error you're encountering is coming from the Jena parser, not from Virtuoso nor the Virtuoso Jena Provider.
First thing is to correct the query to use proper SPARQL syntax. Virtuoso is more forgiving than Jena, but it's best to conform to the spec --
SELECT ( ?a AS ?count )
WHERE { ?a a ?b }
LIMIT 10
Then you might want to change your query a bit, because I don't think the results you'll see from the above are what you're looking for --
SELECT ( COUNT(?a) AS ?count )
?b
WHERE { ?a a ?b }
LIMIT 10
If the error continues, I'd check the versions of all relevant components -- Jena, Virtuoso Jena Provider, Virtuoso JDBC Driver, Virtuoso.
Then, assuming all components are up to date, and given the query is working directly against Virtuoso, you may want to bypass the Jena parser as described in our documentation, and perhaps report a bug against Jena.
Upvotes: 1
Reputation: 16630
The SPARQL syntax is (expr AS ?var)
including the ( )
.
query = "select (?a AS ?count) where {?a <a> ?b.} limit 10";
Upvotes: 2