Sparql with Java Jena - java

I'm using next query in Wikidata Query Service:
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX xsd: <www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?lexemeId ?lemma WHERE {
?lexemeId <http://purl.org/dc/terms/language> wd:Q1860.
?lexemeId wikibase:lemma ?lemma.
FILTER (regex(?lemma, '^bank.*'))
}
It works fine when executing at https://query.wikidata.org/ but, if trying using Java Jena, it fails, cause:
Exception in thread "main" org.apache.jena.query.QueryParseException: Line 7, column 11: Unresolved prefixed name: wikibase:lemma
It can't resolve wikibase as a prefix.
Any clue?
Thx in advance, David.

When you use their SPARQL query page a number of prefixes are pre-declared, including wikibase. When querying the endpoint remotely these have to be declared. So use PREFIX wikibase: <http://wikiba.se/ontology#> in your query.
A list of prefixes is available in the wiki

Related

How to add a SERVICE clause to SelectBuilder using Apache Jena's query builder?

I’m using the Jena query builder from the Jena-extras and I was wondering if there was a way to add a SERVICE clause to a SelectBuilder object.
My code right now looks like this:
SelectBuilder builder = new SelectBuilder()
.addPrefix( "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" )
.addPrefix("dbo", "http://dbpedia.org/ontology/")
.addVar("?uri")
.addWhere("?uri", "rdf:type", "dbo:Company")
.setLimit(100);
Query query = builder.build();
System.out.println(query);
which outputs this:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?uri
WHERE
{ ?uri rdf:type dbo:Company}
LIMIT 100
I’ve also tried adding the SERVICE clause to a Query object after calling builder.build(), but it seems like it’s only possible through the ARQ API using body.addElement(new ElementService("http://any.domain/sparql", elementBlock)). The problem is that you can’t get the elementBlock from a Query object or SelectBuilder object.
Is there a way to add a SERVICE clause using the query builder or are there known workarounds without resorting to the verbose ARQ API, or should I try and extend the SelectBuilder API myself?
For anyone having the same problem, I asked the devs who gave me a useful hint (thank you, Andy Seaborne), and the solution seems to be as follows:
SelectBuilder builder = new SelectBuilder()
.addPrefix( "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" )
.addPrefix("dbo", "http://dbpedia.org/ontology/")
.addVar("?uri")
.addWhere("?uri", "rdf:type", "dbo:Company")
.setLimit(100);
Query query = builder.build();
ElementGroup body = new ElementGroup();
body.addElement(new ElementService("http://dbpedia.org/sparql", query.getQueryPattern()));
query.setQueryPattern(body);
System.out.println(query);
This code outputs the following query:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?uri
WHERE
{ SERVICE <http://dbpedia.org/sparql>
{ ?uri rdf:type dbo:Company}
}
LIMIT 100
Too bad that this isn't a part of the query builder's fluent API out of the box, but any savvy Java engineer can write an extension or helper for this.

Fetch snap SPARQL query using OWL API in java web

oke, I have this snap SPARQL query using protege
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX :<http://www.semanticweb.org/astrid/ontologies/2019/5/mpasiv2#>
SELECT ?resep_makanan
WHERE {
?resep_makanan rdf:type :resep_makanan.
?resep_makanan :resep_untuk :6-7_bulan.
}
ORDER BY ?resep_makanan
and it works, i get instance for my resep_makanan which are food name, with object property resep_untuk (recipe_for) baby who is 6-7_bulan (6-7_month old).
then in my java web code, i write
PREFIX :<http://www.semanticweb.org/astrid/ontologies/2019/5/mpasiv2#>
SELECT DISTINCT ?resep_makanan
WHERE {
Type (?resep_makanan, :resep_makanan),
PropertyValue(?resep_makanan, :resep_untuk, :6-7_bulan)
}
order by ?resep_makanan
But i get 0 query result.
Im using OWL API.
How should i write it correctly?
Looking up 'snap SPARQL' brings the https://github.com/protegeproject/snap-sparql-query project, which I believe is what you're actually using to run your queries (as it's used in Protege).
If that's the case, you're not using OWL API for the SPARQL part of your code, as OWL API does not support SPARQL itself; I don't know the snap sparql project but, as it integrates with Protege, which is OWL API based, I assume OWL API is used for interfacing a SPARQL based API with Protege. From the project dependencies, I think that's de-derivo-sparqldlapi.
To answer your question, if the same query does not produce a result outside of Protege this must depend on it not being sent to the same SPARQL endpoint or to a different set up of the endpoint (e.g., does your query depend on reasoning for its results?)
Some of this information might be in the code that you're running and have not shown here, but we won't be able to tell without seeing it.

Regular Expresions queries with HSQLDB in Java

I am trying to create a regex filter for my app. I am using HSQLDB to store my messages and regex.pattern[1] class to match the incoming messages. I noticed that regex.pattern and LIKE in HSQLDB uses diferent matching "teqniques".
Example
I want to match: {"auth_user":"YQ==","auth_pass":"ZGFz"}.
With HSQLDB: SELECT * FROM messages LIKE %auth%
With regex.pattern: \bauth or auth
My Questions
Is there any way to get the input from user and query with RLIKE or REGEX in HSQLDB?
Is there any easily way to convert regex.pattern in HSQLDB query?
[1] https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Thank you in advance.
EDIT 1: The messages I get are not only JSON formated.
EDIT 2: I tried REGEXP_MATCHES like #JorgeCampos and #fredt mention to me but I get the following exception
SQL Error [S1000]: java.lang.ClassCastException: org.hsqldb.types.ClobDataID cannot be cast to java.lang.String
when I execute the following command
SELECT * FROM WEBSOCKET_MESSAGE WHERE REGEXP_MATCHES(PAYLOAD_UTF8, '^a.*');
Use REGEXP_MATCHES(column_name, regular_expression)
The function uses Java regular expression syntax.
If the column type is CLOB, use a cast to VARCHAR
REGEXP_MATCHES(CAST (column_name AS LONGVARCHAR), regular_expression)

Escape special characters in SPARQL queries

I'm trying to query dpbedia to get the categories of some wikipedia articles using Jena and ARQ
For example:
select ?category { dbpedia:ARTICLE_NAME dcterms:subject ?category }
Here is an example of a working query
SPARQL results
The problem is when there are special characters in ARTICLE_NAME for example "Parma_F.C.", where there is "."
select ?category { dbpedia:Parma_F.C. dcterms:subject ?category }
ERROR
So, I would like to ask you if someone had a solution for that.
Thanks in advance
The identifier dbpedia:Parma_F.C. is a so-called prefixed name, that is, an abbreviated form of a full URI. The full syntax rules for it are described in the SPARQL 1.1 Query Language specification.
The problem is specifically the full stop at the end of the prefixed name. According to the SPARQL grammar, a prefixed name cannot end on a full stop unless it's escaped. The fix is simply to use a backslash:
dbpedia:Parma_F.C\.
What you can also do as an alternative, is just write out the full URI. The dbpedia prefix maps to the http://dbpedia.org/resource/ namespace, so the full URI in SPARQL would become:
<http://dbpedia.org/resource/Parma_F.C.>
and the full query would become:
select ?category { <http://dbpedia.org/resource/Parma_F.C.> dcterms:subject ?category }

jcr query construct

I am building a jcr query and receive data from repository. Here is my code:
String queryString = "SELECT * FROM public:hours";
try {
// get session
Session session = requestContext.getSession();
// create query from queryString constructed
Query q = session.getWorkspace().getQueryManager().createQuery(queryString, Query.JCR_SQL2);
// execute query and retrieve result
QueryResult result = q.execute();
// debug line
log.error("query is", q.getStatement());
....
But this can not execute successfully. It gives me an error that
Repositorty Failed:
[INFO] [talledLocalContainer] javax.jcr.query.InvalidQueryException: Query:
[INFO] [talledLocalContainer] SELECT * FROM public:(*)hours; expected: <end>
In the jcr-shell, it works if I type in query sql "select * from public:hours" and will give me proper results.
I searched many references but almost every example is the same as mine. so I am not sure where the problem is.
Anyone have experience with this please help.
If you're using the JCR-SQL2 query language, then you should quote the selector name with square brackets:
SELECT * FROM [public:hours]
For details, see Section 6.7.4 of the JSR-283 (aka, JCR 2.0) specification, which is also available online. Note that the square bracket quote characters are not required if the name were to be a valid SQL92 identifier. The node type names containing namespace prefixes always need to be quoted, since the ':' character is not allowed in SQL92 identifiers.
Of course, this assumes that you have a node type named "public:hours", where "public" is the namespace prefix.

Categories