如何从dbpedia获得单个记录查询sparql的电影片名,3个艺术家和1个导演?

时间:2015-11-25 12:01:28

标签: sparql dbpedia movie opendata

我试图使用此查询来获取电影。

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
SELECT ?movie ?movieTitle ?directorName ?DistributorName ?ArtistName ?Country (CONCAT(STR(MONTH(?ReleaseDate)), 
                 "/", 
                 STR(DAY(?ReleaseDate)), 
                 "/", 
                STR(YEAR(?ReleaseDate))) as ?displayDate)
WHERE{ 

  ?movie rdf:type <http://dbpedia.org/ontology/Film> .
  ?movie foaf:name ?movieTitle .
  ?movie dbpedia2:director ?directorName .
  ?movie dbpedia2:distributor ?DistributorName .
  ?movie dbpedia2:starring ?ArtistName .
  ?movie dbpedia2:country ?Country .
  ?movie <http://dbpedia.org/ontology/releaseDate> ?ReleaseDate  .

  FILTER(!isLiteral(?movieTitle) || langMatches(lang(?movieTitle), "EN"))
  FILTER ((?ReleaseDate >= "2001-01-01"^^xsd:date) && (?ReleaseDate < "2016-01-01"^^xsd:date))

}
ORDER BY DESC(?ReleaseDate)
LIMIT 500 OFFSET 0

但是,结果如下:

movieA - directorA - distributorA - ArtistA - Indonesia - 2012/06/10
movieA - directorA - distributorA - ArtistB - Indonesia - 2012/06/10
movieA - directorA - distributorA - ArtistC - Indonesia - 2012/06/10
movieB - directorD - distributorA - ArtistA - Malaysia - 2013/06/10
movieC - directorA - distributorS - ArtistD - France - 2013/03/10

我希望以这种格式获取数据:

movieA - directorA - distributorA - ArtistA,ArtistB,ArtistC - Indonesia - 2012/06/10
movieB - directorD - distributorA - ArtistA - Malaysia - 2013/06/10
movieC - directorA - distributorS - ArtistD - France - 2013/03/10

1 个答案:

答案 0 :(得分:1)

您可以使用GroupConcat执行此操作。因此,您可以修改查询以包含(GROUP_CONCAT(?ArtistName; separator =“,”)作为?艺术家)。当我运行查询时,我注意到导演(列出多个导演的电影)等也会发生同样的事情。所以如果你想把每部电影都放在一行,你可能也需要将它们分组。

示例查询。

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
SELECT ?movie ?movieTitle ?directorName ?DistributorName(GROUP_CONCAT(?ArtistName ; separator=",") as ?artists) ?Country (CONCAT(STR(MONTH(?ReleaseDate)), 
                 "/", 
                 STR(DAY(?ReleaseDate)), 
                 "/", 
                STR(YEAR(?ReleaseDate))) as ?displayDate)
WHERE{ 

  ?movie rdf:type <http://dbpedia.org/ontology/Film> .
  ?movie foaf:name ?movieTitle .
  ?movie dbp:director ?directorName .
  ?movie dbp:distributor ?DistributorName .
  ?movie dbp:starring ?ArtistName .
  ?movie dbp:country ?Country .
  ?movie <http://dbpedia.org/ontology/releaseDate> ?ReleaseDate  .

  FILTER(!isLiteral(?movieTitle) || langMatches(lang(?movieTitle), "EN"))
  FILTER ((?ReleaseDate >= "2001-01-01"^^xsd:date) && (?ReleaseDate < "2016-01-01"^^xsd:date))

}
ORDER BY DESC(?ReleaseDate)
LIMIT 500 OFFSET 0