How to delete using sparql - sparql

I fear this is simple question . . . could someone tell me what the sparql is to delete all of a particular type of resource? Not just an individual triple but an entire set of resources.
thank you

Sounds like you want to delete all triples of resources having some class. Try this
delete {?s ?p ?o}
where {?s a :ClassToDelete; ?p ?o}

Related

SPARQL: Count how often an object property is used

I have an RDF graph and I want to count how many times the skos:broader object property is actually used. Is there a simple (and ideally efficient) SPARQL query I can use for that?
Trivially simple answer:
SELECT (COUNT(*) as ?cnt)
WHERE { ?s skos:broader ?o }

Is there a better way to do case insensitive queries? [duplicate]

I am using Jena ARQ to write a SPARQL query against a large ontology being read from Jena TDB in order to find the types associated with concepts based on rdfs label:
SELECT DISTINCT ?type WHERE {
?x <http://www.w3.org/2000/01/rdf-schema#label> "aspirin" .
?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type .
}
This works pretty well and is actually quite speedy (<1 second). Unfortunately, for some terms, I need to perform this query in a case-insensitive way. For instance, because the label "Tylenol" is in the ontology, but not "tylenol", the following query comes up empty:
SELECT DISTINCT ?type WHERE {
?x <http://www.w3.org/2000/01/rdf-schema#label> "tylenol" .
?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type .
}
I can write a case-insensitive version of this query using FILTER syntax like so:
SELECT DISTINCT ?type WHERE {
?x <http://www.w3.org/2000/01/rdf-schema#label> ?term .
?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type .
FILTER ( regex (str(?term), "tylenol", "i") )
}
But now the query takes over a minute to complete! Is there any way to write the case-insensitive query in a more efficient manner?
From all the the possible string operators that you can use in SPARQL, regex is probably the most expensive one. Your query might run faster if you avoid regex and you use UCASE or LCASE on both sides of the test instead. Something like:
SELECT DISTINCT ?type WHERE {
?x <http://www.w3.org/2000/01/rdf-schema#label> ?term .
?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type .
FILTER (lcase(str(?term)) = "tylenol")
}
This might be faster but in general do not expect great performance for text search with any triple store. Triple stores are very good at graph matching and not so good at string matching.
The reason the query with the FILTER query runs slower is because ?term is unbound it requires scanning the PSO or POS index to find all statements with the rdfs:label predicate and filter them against the regex. When it was bound to a concrete resource (in your first example), it could use a OPS or POS index to scan over only statements with the rdfs:label predicate and the specified object resource, which would have a much lower cardinality.
The common solution to this type of text searching problem is to use an external text index. In this case, Jena provides a free text index called LARQ, which uses Lucene to perform the search and joins the results with the rest of the query.

How to list all all graphs in Virtuoso?

When I go to http://localhost:8890/sparql/, there are two fields: Default Data Set Name (Graph IRI) and query. How can I list what all graphs (that go in the former field) are available in my DB? The field is not mandatory and I can just run a query against all namespaces. But I would like to know how to list the graphs available.
The only non-empty graph I was able to run was http://localhost:8890/sparql
For example, in a relational database environment, I believe this kind of info could be retrieved from system tables.
As noted in comments, this query will get you a list of all Named Graphs (which, as also noted, are not the same as "namespaces") in the targeted store --
SELECT DISTINCT ?g
WHERE { GRAPH ?g {?s ?p ?o} }
ORDER BY ?g
You can see live results (limited here to 100 graph names) on the DBpedia endpoint (a very short list, as you would expect) and on URIBurner (a much longer and more varied list).
I know this is an old question, but I was facing the same problem and thought someone else might benefit from the solution I found.
I was trying to execute this query to list all graphs and it was taking about 5 minutes to return a result:
SELECT DISTINCT ?g
WHERE {
GRAPH ?g {?s ?p ?o}
}
Instead, you should try the following query suggested in this documentation from Virtuoso:
SELECT DISTINCT ?g
WHERE {
GRAPH ?g {?s a ?o}
}
This query finished in less than 1 second and returned the list of graphs in the store. Of course, this query would only return graphs which have at least one triple with the predicate rdf:type, but it's still a big improvement on the one suggested by TallTed.

Sparql query dbpedia istance's property and values in a domain

I want to write a SPARQL query to retrieve all properties and values for an instance in a particular domain. For example, I want all properties and values of the singer "Sting" only in "MusicalArtist" domain. How can I do this?
If I understand you correctly, this is a pretty basic SPARQL question, and you'd be better off starting with a good tutorial on SPARQL, or sitting down with the SPARQL 1.1 Query Language document. Just skim through it, you don't need to memorize everything right away, but just get familiar with it. After all, Stack Overflow isn't for reproducing standard documentation; you're expected to already be familiar with that.
As you've shown in a previous question, you can ask for all properties of a given subject with a query like:
select ?p ?o where {
dbpedia:Barry_White ?p ?o
}
You can select all instances of a particular class, e.g., dbpedia-owl:MusicalWork with a query like
select ?o where {
?o a dbpedia-owl:MusicalWork
}
It sounds like you're simply asking how to combine the two. It's just
select ?p ?o where {
dbpedia:Barry_White ?p ?o .
?o a dbpedia-owl:MusicalWork
}
SPARQL results (0 results)
That query doesn't actually have any results, because Barry White isn't related to anything that's a MusicalWork, evidently. However, if you just ask for Works that he's associated with, you'll get a few results:
select ?p ?o where {
dbpedia:Barry_White ?p ?o .
?o a dbpedia-owl:Work
}
SPARQL results (3 results)

No result found on delete

I have the following delete statement:
delete {?s ?p ?o} {<http://www.example.org/test/unit213> ?p ?o.
?s ?p ?o}
The URI http://www.example.org/test/unit213 is valid, since I get on select, among others,
<http://www.example.org/test/unit213> rdf:type <http://www.example.org/test/unit>
Still, I get the following result:
No result
Can anyone tell me why?
That's probably correct, SPARQL Update operations aren't supposed to return any results.
By the way, I would discourage that style of Update without the WHERE keyword, it's very easy to misread.
- Steve