3

I am totally new to SPARQL.

I would like to count the number of actors in this ontology : http://data.linkedmdb.org/directory/actor

I tried the following:

SELECT ?s (COUNT(*) AS ?count)
WHERE
   {
       ?a <http://data.linkedmdb.org/directory/actor> ?s}
 GROUP BY ?s

But i believe that's not the right syntax for it because it gives me 0 results.. Where I know there are several results in that data source! Could it be that the link is not the correct one?

Suvimo
  • 240
  • 1
  • 6
  • 18
  • Your question is ever so slightly incomplete, your last sentence implies that your query doesn't give you the answer you want but you haven't explained why? Does the SPARQL endpoint give an error (if so what error) or do you not get the results (if so what do you get and what did you expect). Editing your question to add these additional details will help you get an accurate answer – RobV Jun 11 '14 at 11:38
  • At a glance your query looks perfectly valid and correct. Without the additional details I've suggested you add its hard to answer your question because it is unclear what you are asking – RobV Jun 11 '14 at 11:40
  • Right now you're counting the number of actors in each film, and as RobV says, it looks like you're doing that correctly. Where are you running this query? What's wrong about the results? – Joshua Taylor Jun 11 '14 at 11:43
  • I am running this query in Protege software. The SPARQL endpoint shows 0 results. Not an error – Suvimo Jun 11 '14 at 13:05

1 Answers1

5

Counting actors per film

In the original formulation of the question, it appears that you're trying to count actors per film. The query is actually very close to that, but I'm not sure where you got the property URI from, but it's not right. E.g., if you look at http://data.linkedmdb.org/page/film/1 and right click on the movie:actor property, you can see that its URI is http://data.linkedmdb.org/resource/movie/actor. Thus, your query could be:

SELECT ?film (count(*) as ?nActors) WHERE {
  ?film <http://data.linkedmdb.org/resource/movie/actor> ?actor .
}
group by ?film
limit 10

Counting actors

Now, you could modify this query to count actors, by running the same triple pattern, but instead of grouping by the film and counting actors, just count distinct actors over all the films:

select(count(distinct ?actor) as ?nActors) where {
  [] <http://data.linkedmdb.org/resource/movie/actor> ?actor .
}

Now, that seems to provide an answer of 162, which seems rather low, but in other questions we've seen that LinkedMDB's endpoint has some strange limitations. It might be because of those, but this isn't the only way that we can count actors, either. You can note by looking at an actor's page, e.g., http://data.linkedmdb.org/page/actor/10, that each actor has the rdf:type http://data.linkedmdb.org/resource/movie/actor, which means that you can just ask for and count things with that type:

select(count(distinct ?actor) as ?nActors) where {
  ?actor a <http://data.linkedmdb.org/resource/movie/actor> .
}

That query returns 2500, which seems more appropriate (since it's probably hitting a limit of 2500 in the endpoint).

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks Joshua, but this seems to get the number of actors per film? I do not need to care about the films, I just want the number of actors in the whole ontology, as one number really, not a list. is that possible? – Suvimo Jun 11 '14 at 15:05
  • Sure, but then it's not clear why you were doing any sort of `group by` in the first place (or why there was no clarification after I pointed out in the comments that you're counting actors per film). I'll update my answer. – Joshua Taylor Jun 11 '14 at 15:13
  • Is there any way you could tell me what that "a" in the query means? – Suvimo Jun 12 '14 at 11:05
  • 1
    It's an abbreviation for the very commonly used rdf:type. Turtle has it too. 'x has type foo' is like 'x is a foo', so the abbreviation is 'a'. – Joshua Taylor Jun 12 '14 at 11:08