基本的なSPARQLクエリ
すべてトリプルを取得
select *
where {
?s ?p ?o .
}
LIMIT 100
「東京都を主語(Subject)に含む」トリプルの述語(?p)と目的語(?o)を取得する
select distinct ?p ?o
where {
<http://ja.dbpedia.org/resource/東京都> ?p ?o .
}
LIMIT 100
「ラベルに“大阪”を含む」トリプルの主語(?s)
select distinct ?s where {
?s <http://www.w3.org/2000/01/rdf-schema#label>? "大阪"@ja .
}LIMIT 100
「ラベルが“大阪”と一致する」トリプルの主語(?s)につながっている述語(?p)と目的語(?o)
select distinct ?s where {
?s <http://www.w3.org/2000/01/rdf-schema#label> ?o
FILTER(regex(str(?o), "大阪" )) .
}LIMIT 100
PREFIXの利用
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?p ?o where {
?s rdfs:label "大阪"@ja .
}LIMIT 100
OFFSETの利用
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct ?p ?o
where {
dbpedia-ja:東京都 ?p ?o .
}
LIMIT 10
OFFSET 10
主語が同じ時の省略表現
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?p ?o where {
?s rdfs:label "大阪"@ja ;
?p ?o.
}LIMIT 100
いろいろな組み合わせを試す(DBpedia日本語版編)
大阪府を主語とするトリプル一覧
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct * where {
dbpedia-ja:大阪府 ?p ?o.
}
大阪府が持つプロパティ一覧
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct ?p where {
dbpedia-ja:大阪府 ?p ?o.
}
大阪府の隣接都道府県
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct * where {
dbpedia-ja:大阪府 prop-ja:隣接都道府県 ?o.
}
すべての隣接都道府県の組
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct * where {
?s prop-ja:隣接都道府県 ?o.
}
大阪府の隣接都道府県の数
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct count(?o) where {
dbpedia-ja:大阪府 prop-ja:隣接都道府県 ?o.
}
大阪府の隣接都道府県の数(結果を変数に代入)
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct count(?o) AS ?count where {
dbpedia-ja:大阪府 prop-ja:隣接都道府県 ?o.
}
不要なプロパティをFILTERで除外
PREFIX prop-ja: <http://ja.dbpedia.org/property/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
select distinct * where {
dbpedia-ja:大阪府 ?p ?o.
FILTER(?p != dbpedia-owl:wikiPageWikiLink) .
}
タイプ一覧の取得
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select distinct ?o where {
?s rdf:type ?o.
}LIMIT 100
都道府県の一覧を取得
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?s where {
?s rdf:type dbpedia-owl:Place.
?s dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県.
}
郵便番号でソート(昇順)
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?s ?o where {
?s rdf:type dbpedia-owl:Place;
dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県;
dbpedia-owl:postalCode?? ?o.
}ORDER BY ?o
郵便番号でソート(降順)
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?s ?o where {
?s rdf:type dbpedia-owl:Place;
dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県;
dbpedia-owl:postalCode ?o.
}ORDER BY DESC(?o)
日本の都道府県を「隣接自治体の数」でソート
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?s count(?o) AS ?c where {
?s rdf:type dbpedia-owl:Place;
dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県;
prop-ja:隣接都道府県 ?o.
}ORDER BY ?c
各都道府県で生まれた政治家
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?pref ?s where {
?pref rdf:type dbpedia-owl:Place.
?pref dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県.
?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace ?pref.
}ORDER BY ?pref
各都道府県で生まれた政治家の数
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?pref (count(?s) AS ?c) where {
?pref rdf:type dbpedia-owl:Place.
?pref dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県.
?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace ?pref.
}GROUP BY ?pref
ORDER BY ?c
「出生地が大阪府」または「出生地が東京都」の政治家
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?s ?pref where {
{?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace dbpedia-ja:大阪府.}
UNION{?s rdf:type dbpedia-owl:Politician;
dbpedia-owl:birthPlace dbpedia-ja:東京都.}
?s dbpedia-owl:birthPlace ?pref.
}ORDER BY ?pref
第36回SWO研究会のハンズオン成果
都道府県ごとの、日本のアイドルの人数
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
select distinct ?pref (count(?s) AS ?c) where {
?pref rdf:type dbpedia-owl:Place.
?pref dbpedia-owl:wikiPageWikiLink category-ja:日本の都道府県.
?s rdf:type dbpedia-owl:Person;
dbpedia-owl:wikiPageWikiLink category-ja:日本のアイドル;
dbpedia-owl:birthPlace ?pref.
}GROUP BY ?pref
ORDER BY ?c
ウィキペディアに掲載された日本人(nationalityがJapan)の生誕年ごとの人数集計
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia-ja: <http://ja.dbpedia.org/resource/>
SELECT ?year COUNT(?s) WHERE {
?s dbpedia-owl:nationality dbpedia-ja:Japan ;
dbpedia-owl:birthDate ?date.
}
GROUP BY (year(?date) AS ?year)
ORDER BY ?year
生誕年の新しい順
昔の人が2020年生まれになっている
select distinct ?s ?date where {
?s dbpedia-owl:birthDate ?date.
}
ORDER BY desc(year(?date))
LIMIT 100
日本百名山の標高順
select *
where
{
?uri dbpedia-owl:wikiPageWikiLink category-ja:日本百名山.
?uri rdf:type schema:Mountain.
?uri foaf:name ?name.
?uri foaf:depiction ?image.
?uri prop-ja:標高 ?altitude.
}
order by ?altitude
戦国時代・安土桃山時代の戦に参加した武将(回数順)
PREFIX category-ja: <http://ja.dbpedia.org/resource/Category:>
SELECT DISTINCT ?person count(?war) AS ?c WHERE {
{?war dbpedia-owl:wikiPageWikiLink category-ja:日本の戦国時代の戦い}
UNION
{?war dbpedia-owl:wikiPageWikiLink category-ja:安土桃山時代の戦い}
?war dbpedia-owl:opponents ?person .
}
GROUP BY ?person
ORDER BY DESC(?c)
「学会」を含む組織の創設年度順列挙
産総研 西村さん作
国立国会図書館典拠データ検索・提供サービスで利用可能なクエリ
http://id.ndl.go.jp/auth/ndla/?query=
select distinct * where{
?id <http://xmlns.com/foaf/0.1/name> ?name.
?id a <http://xmlns.com/foaf/0.1/Organization>;
<http://RDVocab.info/ElementsGr2/dateOfEstablishment> ?date;
<http://RDVocab.info/ElementsGr2/corporateHistory> ?history.
filter (regex(str(?name), "学会"))
}
order by ?date
SPARQLチュートリアル@第36回SWO研究会-DBpediaシンポジウム より引用