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 }
Related
I am currently using spring-data-jpa version 1.9.4.
I have a MySql table with columns project(integer), summary(varchar), and description(varchar).
I have a regex that I would like to use to search the summary and/or description field meaning that if it finds it in summary does not need to apply regex to description.
The repository method I am attempting to use is:
List<Issue> findByProjectAndSummaryOrDescriptionRegex(long project, String regex)
The error I am receiving is:
java.lang.IllegalArgumentException: Unsupported keyword REGEX (1):
[MatchesRegex, Matches, Regex]
It is difficult in my company environment to update/upgrade versions, so if the issue is NOT my syntax but rather the then if someone knows which version now supports 'Regex' for query derivation or where I could find that specific information I would be grateful. I have looked at the Changelog and it appears that 1.9.4 should support but it appears not.
Thanks for your help!
JD
EDIT 1: I am aware of the #Query annotation but have been asked by my lead to only use that as a last resort if I cannot find the correct version which supports keyword REGEX [MatchesRegex, Matches, Regex]
I would recommend using native query (with #Query annotation) if the Spring data syntax does not work, e.g.:
#Query(nativeQuery=true, value="SELECT * FROM table WHERE project = ?1 AND (summary regexp ?2 OR description regexp ?2)")
List<Issue> findByProjectAndSummaryOrDescription(long project, String regex);
Update
If native query is not an option then (a) could you try it with single column and see if that works and (b) could you try by appending regex to both the columns, e.g.:
List<Issue> findByProjectAndDescriptionRegex(long project, String regex);
List<Issue> findByProjectAndSummaryRegexOrDescriptionRegex(long project, String regex, String regex);
In a followup, I discovered by doing some digging that the authoratative list will reside in the org.springframework.data.jpa.repository.query.JpaQueryCreator class. So for future folks that want to know which keywords from the 'Documented' list are ACTUALLY implemented, look inside JpaQueryCreator and you will the keywords supported as case arguments inside a switch!
Hope this helps!
PS - as suspected, REGEX was not supported in my version
try tu use #Query with param nativeQuery = true inside You can use database regexp_like function :
#Query(value = "select t.* from TABLE_NAME t where regexp_like(t.column, ?1)", nativeQuery = true)
Documentation :
https://www.techonthenet.com/oracle/regexp_like.php
I'm adventuring with sparql and a java application, I've found a few connection basics to get up and running as it were but fear I'm making a mistake which will later turn into something worse.
Every connection suggestion regardless of the library used says to connect to "http://dbpedia.org/sparql/" yet this doesn't work for me.
I checked the url that is returned when I run a query using the online editor and noticed the live prefix, so I added that as my connection string, and it works. That is to say, my connection string looks like "http://live.dbpedia.org/sparql"
And it does return the result, however, the result has the XML Schema attached which is making me wonder whether or not it's because of this live. I've added in.
Below is the simple connection code I'm using, is this correct? Any and all help greatly appreciated thank you.
If the 'live' is correct, is it possible to extra the just the value wihtout the Schema?
StringBuilder sb = new StringBuilder();
sb.append("PREFIX dbr: <http://dbpedia.org/resource/> \n");
sb.append("PREFIX dbp: <http://dbpedia.org/property/> \n");
sb.append("PREFIX dbo: <http://dbpedia.org/ontology/> \n");
sb.append("SELECT ?dob \n");
sb.append("WHERE {dbr:Tony_Blair dbp:birthDate ?dob} \n");
Query query = QueryFactory.create(sb.toString());
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://live.dbpedia.org/sparql", query);
try {
ResultSet results = qexec.execSelect();
for ( ; results.hasNext() ; )
{
QuerySolution soln = results.nextSolution() ;
System.out.println(soln.get("?dob"));
}
the result being:
1953-05-06^^http://www.w3.org/2001/XMLSchema#date
Well the result as you show it is missing some brackets and quotes, but I assume that is caused by how you copy-pasted it. More usually it would look like this:
"1953-05-06"^^<http://www.w3.org/2001/XMLSchema#date>
But in essence your query and code is correct. The "attached XML Schema" here is the datatype of the returned literal string.
An RDF literal consists of a lexical value (in your case "1953-05-06") and a datatype (in your case http://www.w3.org/2001/XMLSchema#date). It can also, optionally have a language tag e.g. "colour"#en-UK.
If you wish to remove the datatype from the result and only retrieve the lexical value, you can use the STR() function as part of the SELECT clause in your query:
SELECT (STR(?dob) as ?date_of_birth)
As for the connection string that you are struggling with: there are two separate DBPedia endpoints. The "regular" one is http://dbpedia.org/sparql (no trailing slash) - this queries a static dataset that is synced/updated with Wikipedia changes every so 6 months or so. The "live" endpoint, http://live.dbpedia.org/sparql, is an effort to have a more up-to-date dataset ready for querying. See https://wiki.dbpedia.org/online-access/DBpediaLive for more details.
I know I can get the AST using the explain extended command. My question is, how to get the same using the Java API.
My goal is to get the following data about queries:
Database source and target (if applied).
Table source and target (if applied).
Fields involved in the query.
I know I can get them above data directly from query string using Regex, but I want to use Java API.
Do you have any other idea how to do that?
You can use Hive Parser for this. It simply takes the query and convert it into AST which is similar explain command. Here is a simple example:
ParseDriver pd = new ParseDriver();
ASTNode tree = pd.parse("Select * from table1");
System.out.println(tree.dump());
The output for this will be
nil
TOK_QUERY
TOK_FROM
TOK_TABREF
TOK_TABNAME
table1
TOK_INSERT
TOK_DESTINATION
TOK_DIR
TOK_TMP_FILE
TOK_SELECT
TOK_SELEXPR
TOK_ALLCOLREF
<EOF>
I am now using solr to query .I want to find all the documents whose key "title" contains text "Bifidobacterium bifidum" or key "abstract" contains text "Bifidobacterium bifidum".So , I write my query like below:
String queryCondition = "title:*Bifidobacterium bifidum* OR abstract:*Bifidobacterium bifidum*";
From the result ,I find out that the returned result is not what I want ,documents whose title contains "Bifidobacterium" or "bifidum" , or whose title contains "Bifidobacterium" or "bifidum" are all returned . So , my question is ,how should I write my query to satisfy my query need?
The * is special symbol, a wildcard. Similar to regular expressions, it tells Solr to match everything. So querying for bifidum* would return everything that starts with bifidum. Not what you want, right?
When reading about Solr's query syntax in the manual, you will find a section named Specifying Terms for the Standard Query Parser there is written
A phrase is a group of words surrounded by double quotes such as "hello dolly"
This is what you need ...
I am using the webservice that will send the request for one of the column as Dran & Hyle , but i get the exception as a expected valid begining name character. due to the special character &
Below is the insert statement in my java .
public static final String PetInsert= insert into pet values(?,?,?);
I believe set define off will not work in java code , it is understood only by sql developer.
Any help is appreciated
From Java, it is recommended to use PreparedStatement when creating a statement to query the database. Read more in the documentation.
Not so sure, but can it be that in the XML of the web service the error is located? Then somewhere
"Dran & Hyle"
should go into the XML. Normally it is done automatically. So it would be the unlikely case of creating the XML oneself with Strings.
In that case use apache's StringEscapeUtils:
s = StringEscapeUtils.escaleXML(s);
P.S. I found set define off of #aUserHimself plausible.