0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SPARQL】独自の型の値をFilterで絞り込みしたい

Posted at

SPARQLについて

SPARQLは現状(2021年10月)、Wikipedia(DBpedia)や自治体関係のデータのAPI取得などで度々用いられるクエリです。
概要についてはSPARQL入門を参照ください。

今回のトピック

法人情報のデータを取得できるAPI、gBizINFOにおいてSPARQLを使用できます。gBizINFOの概要についてはgBizINFOから法人情報APIを取得してみるの記事などを参照ください。

このgBizINFOで、例えば財務データの勘定科目を取得する際に、以下のような独自の型で設定された項目がレスポンスで来たりします。

"shihyou": {
  "datatype": "http://imi.go.jp/ns/core/rdf#コード型",
  "type": "literal",
  "value": "http://hojin-info.go.jp/code/財務情報/資本金"
},

これを資本金だけに絞り込みたい場合、以下のようなFilterの設定では上手くいきません。
FILTER(?shihyou = "http://hojin-info.go.jp/code/財務情報/資本金")

対応

リテラルと任意のデータ型とのマッチングの記事にある通り、独自の型でマッチングさせたい場合は^^型名のような感じで明示的に型を指定してあげる必要があります。
なお、xsd:stringxsd:integerなどの一般的(?)な型であれば、あえて型名を指定する必要がなく、SQLと同じような感じでFilterに条件を書くことができます。

クエリサンプル

gBizINFOのAPIで、資本金の項目でデータを取得したい場合のクエリを例にします。

PREFIX hj: <http://hojin-info.go.jp/ns/domain/biz/1#>
PREFIX ic: <http://imi.go.jp/ns/core/rdf#>

SELECT ?cID ?name ?value ?hyouki ?shihyou
FROM <http://hojin-info.go.jp/graph/zaimu>
WHERE {
  ?s hj:法人活動情報 ?o .
  ?o ic:名称/ic:表記 ?name .
  ?o ic:ID/ic:識別値 ?cID .
  ?o hj:数量コレクション/hj:数量 ?c .
  ?c ic:数値 ?value .
  ?c ic:単位表記 ?hyouki .
  ?c hj:指標 ?shihyou
  FILTER(?shihyou = "http://hojin-info.go.jp/code/財務情報/資本金"^^ic:コード型)
}
limit 100

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?