Created
October 25, 2013 04:08
-
-
Save tsunagun/7149348 to your computer and use it in GitHub Desktop.
SPARQLスニペット集
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GROUP BYで集約した結果を数える | |
# 例)BibResourceクラスのインスタンスそれぞれが | |
# どのようなプロパティをいくつ持っているかを数えて表示する | |
SELECT ?resource ?property (COUNT(?property) AS ?property_count) | |
FROM <http://purl.org/net/aozora/resources> | |
WHERE { | |
?resource rdf:type <http://purl.org/net/aozora/BibResource> . | |
?resource ?property ?value . | |
} | |
GROUP BY ?resource ?property | |
ORDER BY ?resource |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 値をカンマ区切りで一つにする | |
# 例)書籍が複数のキーワードを持っている場合, | |
# それらをカンマ区切りで取得する | |
SELECT ?book ?title ?keywords | |
WHERE { | |
{ | |
SELECT ?book (group_concat(distinct ?keyword ; separator = ", ") AS ?keywords) | |
WHERE { | |
?book dc:subject ?keyword . | |
} | |
GROUP BY ?book | |
} | |
?book dc:title ?title | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 結果に特定のレコードを含まない | |
# 例)書籍が持つプロパティのうち,rdf:type以外を取得する | |
SELECT ?book ?property | |
WHERE { | |
?book ?property ?value | |
?book !rdf:type ?value | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 結果に含まれる文字列の一部を別の文字列に置き換える | |
SELECT ?book | |
WHERE { | |
?book dc:title ?title; | |
FILTER REPLACE(str(?title),"×","x") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 構造化された値を取得する | |
# 例)書籍の著者が | |
# ・リテラル | |
# ・リテラルを持ったリソース | |
# のいずれかであるとき,その両方を取得する | |
SELECT ?book ?author_name | |
WHERE { | |
{ | |
?book dc:creator ?author_name . | |
} | |
UNION | |
{ | |
?book dc:creator ?author . | |
?author foaf:name ?author_name . | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment