6.3 REST API を使用したトリプルのクエリ
6.3.3 POST リクエストでの SPARQL クエリの実行
POSTリクエストを使用して
REST API
でトリプルをクエリする場合は、次の2
つの手法 を使用できます。•
POSTのボディでSPARQL
クエリをファイルとして含める。• URL
にエンコードされたデータとしてSPARQL
クエリを含める。次の例は、米国議会法案
44
について調べるシンプルなSPARQL
のDESCRIBEクエリを示 したものです。このクエリは、bill44.sparqlという名前のファイルとして保存されてい ます。ファイルの内容は次のとおりです。
#file name bill44.sparql
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
PREFIX bill: <http://www.rdfabout.com/rdf/schema/usbill/>
DESCRIBE ?x WHERE { ?x rdf:type bill:HouseBill ; bill:number "44" . }
SPARQL
エンドポイントでは、パラメータまたはPOSTのボディとしてSPARQL
クエリを必要とします。次の例では、DESCRIBEクエリが記述されているbill44.sparqlファイ ルがPOSTリクエストのボディに渡されます。
# Windows???????????
curl --anyauth --user admin:password -i -X POST --data-binary @./bill44.sparql -H "Content-type: application/sparql-query"
-H "Accept: application/rdf+xml" \ http://localhost:8000/v1/graphs/sparql
注:
読みやすくするため、長いコマンドは
UNIX
の行継続文字「\
」で複数の 行に分割しています。また、改行を追加しています。コマンドをコピー&ペーストするときは、これらの継続文字や改行を取り除いてください。
このクエリは、法案
44
について説明する次のトリプルを返します。<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns">
<OfficialTitle rdf:ID="bnodebnode309771418819f878"
xmlns="http://www.rdfabout.com/rdf/schema/usbill/">
<rdf:typerdf:resource="http://www.rdfabout.com/rdf/schema/usbill /OfficialTitle"/>
<rdf:value rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
To amend the Internal Revenue Code of 1986 to provide reduced capital gain rates for qualified economic stimulus gain and to index the basis of assets of individuals for purposes of
determining gains and losses.</rdf:value></OfficialTitle>
<ShortTitle rdf:ID="bnodebnode30b47143b819db78"
xmlns="http://www.rdfabout.com/rdf/schema/usbill/">
<rdf:type rdf:resource="http://www.rdfabout.com/rdf/schema/usbill /ShortTitle"/>
<rdf:value rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
Investment Tax Incentive Act of 2003</rdf:value></ShortTitle>
<ShortTitle rdf:ID="bnodebnodee1860b72fb58b315"
xmlns="http://www.rdfabout.com/rdf/schema/usbill/">
<rdf:type rdf:resource="http://www.rdfabout.com/rdf/schema/usbill /ShortTitle"/>
<rdf:value rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
Investment Tax Incentive Act of 2003</rdf:value></ShortTitle>
<HouseBill rdf:about="http://www.rdfabout.com/rdf/usgov/congress/108 /bills/h44" xmlns="http://www.rdfabout.com/rdf/schema/usbill/">
<inCommittee rdf:resource="http://www.rdfabout.com/rdf/usgov /congress/committees/HouseWaysandMeans"/>
<cosponsor rdf:resource="http://www.rdfabout.com/rdf/usgov/congress /people/A000358"/>
<cosponsor rdf:resource="http://www.rdfabout.com/rdf/usgov/congress /people/B000208"/>
<cosponsor rdf:resource="http://www.rdfabout.com/rdf/usgov/congress /people/B000575"/>
<cosponsor ...
クエリのContent-type:には、application/sparql-queryが指定されています。また、コ ンテンツ応答タイプ(Accept:)では出力としてapplication/rdf+xmlが指定されていま す。出力は
XML
形式のトリプルとして返されます。詳細については、「応答の出力形 式」(139
ページ)を参照してください。注:
curlを使用してPUTまたはPOSTリクエストを実行し、ファイルからリク エストのボディを読み取る場合は、-dではなく--data-binaryを使用して 入力ファイルを指定してください。--data-binaryを使用すると、curlに よってファイルからリクエストのボディにデータがそのまま挿入されま す。-dを使用すると、curlによって入力から改行が取り除かれるため、ト リプルデータや
SPARQL
がシンタックスの面で無効になる可能性があり ます。POSTリクエストを使用する別の方法としては、『
REST Client API
』のセマンティックのド キュメントの説明に従って、フォームエンコードされた手法でURL
エンコードされた クエリを含める方法が挙げられます。次の
SPARQL
のSELECTクエリは、BioGuide ID
が「A000358
」である人物が共同提出者であった議院法案を調べます。
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>
PREFIX bill: <http://www.rdfabout.com/rdf/schema/usbill/>
PREFIX people: <http://www.rdfabout.com/rdf/usgov/congress/people/>
SELECT ?x WHERE { ?x rdf:type bill:HouseBill ; bill:cosponsor people:A000358. }
この例では、SELECTクエリが
URL
エンコードされています。これが、フォームエン コードされたデータとして送信されます。curl -X POST --anyauth --user admin:password \
-H "Accept:application/sparql-results+xml" --data-binary \
"query=PREFIX%20rdf%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F1999%2F02%2F22-r df-syntax-ns%23%3E%20%0APREFIX%20bill%3A%20%3Chttp%3A%2F%2Fwww.rdfabou t.com%2Frdf%2Fschema%2Fusbill%2F%3E%0APREFIX%20people%3A%20%3Chttp%3A%
2F%2Fwww.rdfabout.com%2Frdf%2Fusgov%2Fcongress%2Fpeople%2F%3E%0ASELECT
%20%3Fx%20WHERE%20%7B%20%3Fx%20rdf%3Atype%20bill%3AHouseBill%20%3B%20b ill%3Acosponsor%20%20people%3AA000358.%20%7D%0A"
-H "Content-type:application/x-www-form-urlencoded"
=>
<sparql xmlns="http://www.w3.org/2005/sparql-results/">
<head><variable name="x"/></head>
<results>
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1036
</uri></binding></result>
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1057
</uri></binding></result>
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1078
</uri></binding></result>
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h110
</uri></binding></result>
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1117
</uri></binding></result>
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1153
</uri></binding></result>
...
<result><binding name="x">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h975
</uri></binding></result>
</results>
</sparql>
注: