SPARQL - learning by example - sparql

In short, if someone instructs me, "Get a list of databases which include Oracle, Mysql, DB2, MSSQL, etc.," how can I actually establish which fields and information I need to create the query? I downloaded DBpedia and grepped the triples, e.g.,
$ `grep 'Oracle' * | grep 'Database'
article_categories_en.nt:<http://dbpedia.org/resource/Oracle_Database> <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:Relational_database_management_systems> .`
How do I create a SPARQL query from that initial question?
Using that snippet above, I know Oracle is a company and one of its products is an RDBMS by the same name. I tried the lookup tool and searching the web but did not get far enough with this and need some direction.

In this particular case, I'd start by visiting the DBpedia page corresponding to one of the things that you know you'd like in your result. A good instance is http://dbpedia.org/page/MySQL. On that page, you can start to look for properties that might help you find other related systems. One property listed on the page is
dbpedia-owl:genre dbpedia:Relational_database_management_system
Then I'd go the DBpedia SPARQL endpoint and try searching for other things that have this same property and value. In this case, I can run the following query:
SELECT * WHERE {
?rdbms dbpedia-owl:genre dbpedia:Relational_database_management_system .
}
SPARQL results
The results are pretty promising; there are 38 results. The first few are:
rdbms
http://dbpedia.org/resource/MySQL_Cluster
http://dbpedia.org/resource/MSQL
http://dbpedia.org/resource/OpenEdge_Advanced_Business_Language
http://dbpedia.org/resource/Adaptive_Server_Enterprise
…
In general, I find it much easier to locate an instance of the sort of thing that I'm looking for in Wikipedia, then visit the corresponding DBpedia page and look at the properties, and then formulate some exploratory queries based on that.

Related

How to keep SQL data and Elasticsearch in-sync, and which to search from?

I've seen two solutions mentioned, and was wondering what most people do.
Use logstash
Code your application to make writes to Elasticsearch alongside SQL. For example,
public saveRecord() {
saveToElasticsearch();
saveToSQL();
}
Another question is how to handle actually searching the entity? Do you ONLY use Elasticsearch?
If not, I would assume you fetch from Elasticsearch based on keywords and use the IDs returned to filter your SQL query. My question then, is how do you handle pagination? For example let's say you only want results 50 to 100. First you query Elasticsearch which returns 50-100. Then the SQL query reduces that to 20 results - the other 30 results are in what would've been the next Elasticsearch query (100 - 150 for example). Do you keep going back and forth?
As for your first question check here
As for the second question, if you plan to use elasticsearch as your search layer then better do it for all the searchable/filterable fields. As you've described, the alternative will get very messy very soon. Use elasticsearch for all your searches/filters and even aggregations if it suits your needs. Use the sql database as your point of truth and just get the full payload from there.
In general, if you will need to paginate then your search should better be in one place otherwise it will get ugly.

Getting instance - SPARQL query in Protege

I have an ontology in Protege and want to write some SPARQL queries.
The ontology is set up like this:
Thing > Beverages > Wine > Red_wine
Then I have added different red wines as instances. They also have some object properties like fromCountry, goesWellWith and priceRange
How do I create a SPARQL query for returning for example a red wine from France, that goes well with pork, and priceRange 100-199?
If you want to return certain resources in your knowledge base you need a SELECT query.
As the commenters have stated, it is not possible to answer your question exactly as you didn't provide enough information on how your data is modelled and you are expected to show your own efforts and where they failed.
You can find an detailed overview of SPARQL 1.1 at https://www.w3.org/TR/2013/REC-sparql11-overview-20130321/ or a more approachable tutorial at https://www.w3.org/2009/Talks/0615-qbe/.
As far as I can guess from your question, your query should look similar to this:
SELECT ?wine WHERE
{
?wine a :RedWine;
:goesWellWith :Pork;
:priceRange :PriceRange100To199.
}
Please adapt this query to your exact model and show us your results.
If you mean "just one result" with "a red wine", you can add "LIMIT 1".
You could also model the price range differently, for example by having a minPrice and maxPrice, as this would enable more precise queries using filters.
P.S.: I changed your :Red_wine to :RedWine as camel case is usually used for class URIs.

Display all fields in Wikidata Query Service

Wikidata provides query browser at https://query.wikidata.org
I want to display films all fields. I tried with using * but its not working. Does anybody know how to display all fields of the data for Films?
To work with SPARQL is necessary to understand some concepts, as #AKSW said in the comments of the question. If you don't understand the meaning of ?film ?p ?o. This is called triple¹ and is composed by subject-predicate-object. E. g., in the case of the films, it could be: x is a film. This is what you are querying in the Wikidata Query Service (WDQS) when you use ?film wdt:P31 wd:Q11424.
I think it isn't possible to display all the property-values of an item. In addition it probably could cause a timeout because there is many statements of many items.
If you want to check the property-values of all the films in Wikidata I think an option might be you write or find a script to extract the items with P31-Q11424 (instance of films). For that, the accessing data section could be useful (e. g. with pywikibot you could query and extract what you want).
If you are interested in SPARQL and WDQS I recommend you to read some help resources:
Wikidata Query Service Help, specifically the SPARQL tutorial.
Query examples (read another queries is how I began to learn).
SPARQL 1.1 Query Language specification.
RDF Dump Format (because read about the ontology of Wikidata could help to understand the concepts).
Edit
When I answer it I wrote triplestore and linked it to its respective page in the Wikipedia in English, but after the comment of #AKSW I consider I was wrong because the triplestore is the concept which is used to refer to the storage and retrieval of triple or semantic triple, "a set of three entities that codifies a statement about semantic data in the form of subject–predicate–object expressions" (from Semantic triple page in Wikipedia in English).

How to get information of a specific Wikidata ID using SPARQL query?

I know how to find all movies in Wikidata. So, I get a list of IDs like "Q18644475". If I try to get all film info for every film in the database in one query, I get a timeout. So, I decided to get the film info in individual queries.
Is there an SPARQL query to get the title and release year(optionally all possible info) for this ID?
I find the hoops you have to jump through to use an entity ID in SPARQL to be quite nuts.
If anyone else stumbles across this question then an alternative is to download the complete entity information in JSON format.
For example:
https://www.wikidata.org/wiki/Special:EntityData/Q18644475.json

Different SPARQL query engines give differing results for DESCRIBE Query

I tried one SPARQL query in two different engines:
Protege 4.3 - SPARQL query tab
Jena 2.11.0
While the query is the same the results returned by these two tools are different.
I tried a DESCRIBE query like the following:
DESCRIBE ?x
WHERE { ?x :someproperty "somevalue"}
Results from protege give me tuples that take ?x as subject/object; while the ones from jena are that take ?x as subject only.
My questions are:
Is the syntax of SPARQL uniform?
If I want DESCRIBE to work as in protege, what should I do in Jena?
To answer your first question yes the SPARQL syntax is uniform since you've used the same query in both tools. However what I think you are actually asking is should the results for the two tools be different or not? i.e. are the semantics of SPARQL uniform
In the case of DESCRIBE then yes the results are explicitly allowed to be different by the SPARQL specification i.e. no the semantics of SPARQL are not uniform but this is only in the case of DESCRIBE.
See Section 16.4 DESCRIBE (Informative) of the SPARQL Specification which states the following:
The query pattern is used to create a result set. The DESCRIBE form
takes each of the resources identified in a solution, together with
any resources directly named by IRI, and assembles a single RDF graph
by taking a "description" which can come from any information
available including the target RDF Dataset. The description is
determined by the query service
The important part of this is the last couple of sentences that say the description is determined by the query service. This means that both Protege's and Jena's answers are correct since they are allowed to choose how they form the description.
Changing Jena DESCRIBE handling
To answer the second part of your question you can change how Jena processes DESCRIBE queries by implementing a custom DescribeHandler and an associated DescribeHandlerFactory. You then need to register your factory like so:
DescribeHandlerRegistry.get().set(new YourDescribeHandlerFactory());