6.3 REST API を使用したトリプルのクエリ
6.3.6 応答の出力形式
REST API(
GET:/v1/graphs/sparqlまたはPOST:/v1/graphs/sparql)でSPARQL
エンドポ イントをクエリすると、結果の出力形式が変化することがあります。応答タイプの形式 は、クエリのタイプと、RESTリクエストのAccept
ヘッダタイプで使用されるパラ メータによって異なります。Accept
ヘッダタイプでは、応答出力で予期されるMIME
タイプ()を設定します。次の表は、さまざまなSPARQL
クエリに対するAccept
ヘッダタイプおよび応答出力の形式を説明したものです。動詞 説明 権限
GET
URL
エンコードされたSPARQL
に基づいてクエリを実 行します。名前付きグラフを取得します。rest-reader
POST パラメータとしての
SPARQL
クエリ、またはPOSTのボ ディの一部としてURL
エンコードされたSPARQL
クエ リに基づいてクエリを実行します。SPARQL Update
を使用すると、パラメータとして使用する場合、または
URL
エンコードされたSPARQL Update
としてPOSTのボディで使用する場合は、POSTによってト リプルが名前付きグラフにマージされます。rest-writer
PUT
SPARQL Update
を使用すると、名前付きグラフ内のトリプルが置換されます。空のグラフで
PUT
を使用すると、グラフが削除されます。
rest-writer
DELETE
SPARQL Update
を使用すると、名前付きグラフ内のトリプルが削除されます。
rest-writer
HEAD コンテンツなしで、名前付きグラフを取得します。
rest-reader
SPARQL
のSELECTクエリは結果をXML、JSON、または HTML
として返すことができますが、
SPARQL
のCONSTRUCTクエリはサポートされている任意のトリプル形式のトリプルとして結果を返します。SPARQLのDESCRIBEクエリは、クエリによって見つかっ たトリプルを記述するトリプルを
XML
形式で返します。SPARQLのASKクエリを使用 した場合は、ブール型の値(trueまたはfalse)を返します。次の例では、この
SPARQL
のSELECTクエリを使用して、Robert Andrews(「A000210」) が提出者になっていた米国議会法案を調べます。このクエリは として保存クエリ
タイプ
Accept
ヘッダタイプ 応答出力SELECT application/sparql-results+xml
W3C
規格の定義に従って、結果をXML
で返します。「例:XML
として 結果を返す」(141ページ)を参照し てください。SELECT application/sparql-results+json
W3C
規格の定義に従って、結果をJSON
で返します。「例:JSONとして 結果を返す」(142
ページ)を参照し てください。SELECT text/html 結果を
HTML
で返します。「例:HTML
として結果を返す」(143ペー ジ)を参照してください。CONSTRUCT application/sparql-results+xml または
application/sparql-results+json
サポートされている任意の形式のトリ プルとして結果を返します。「サポー トされている
RDF
トリプルのシリア ライゼーション」(36ページ)を参照 してください。DESCRIBE application/rdf+xml 結果をトリプルとして
XML
形式で返します。「GETリクエストでの
SPARQL DESCRIBE
クエリ」(
138
ページ)を参照してください。ASK application/sparql-results+xml?
??
application/sparql-results+json
ブール型の値(trueまたはfalse)を
XML
として返します。「例:XMLま たはJSON
としてブール型の値を返 す」(144
ページ)を参照してくださ い。ファイルの内容は次のとおりです。
#filename bills.sparql
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/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?bill ?title
WHERE { ?bill rdf:type bill:HouseBill ; dc:title ?title ;
bill:sponsor people:A000210 . }
LIMIT 5
6.3.6.1
例:XML
として結果を返す次の例では、
SPARQL
のSELECTクエリがXML
形式で応答を返します。curl --anyauth --user admin:password -i -X POST \ -–data-binary @./bills.sparql
-H "Content-type: application/sparql-query" \ -H "Accept: application/sparql-results+xml" \ http://localhost:8050/v1/graphs/sparql
=>
HTTP/1.1 200 OK
Content-type: application/sparql-results+xml Server: MarkLogic
Content-Length: 1528 Connection: Keep-Alive Keep-Alive: timeout=5
<sparql xmlns="http://www.w3.org/2005/sparql-results/">
<head><variable name="bill"/>
<variable name="title"/>
</head>
<results>
<result>
<binding name="bill">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1171 /uri>
</binding><binding name="title">
<literal datatype="http://www.w3.org/2001/XMLSchema#string">
H.R. 108/1171: Iris Scan Security Act of 2003
</literal>
</binding>
</result>
<result>
<bindingname="bill">
<uri>http://www.rdfabout.com/rdf/usgov/congress/108/bills/h1314/
</uri></binding><binding name="title">
<literal datatype="http://www.w3.org/2001/XMLSchema#string">
H.R. 108/1314: Screening Mammography Act of 2003</literal>
</binding>
</result>
<result>
<binding name="bill"><uri>http://www.rdfabout.com/rdf/usgov /congress/108/bills/h1384/</uri>
</binding>
...
</result>
</results>
</sparql>
6.3.6.2
例:JSON
として結果を返す次の例では、SPARQLのSELECTクエリが
JSON
形式で応答を返します。curl --anyauth --user admin:password -i -X POST \ --data-binary @./bills.sparql \
-H "Content-type: application/sparql-query" \ -H "Accept: application/sparql-results+json" \ http://localhost:8050/v1/graphs/sparql
=>
HTTP/1.1 200 OK
Content-type: application/sparql-results+json Server: MarkLogic
Content-Length: 1354 Connection: Keep-Alive Keep-Alive: timeout=5
{"head":{"vars":["bill","title"]},
"results":{"bindings":[
{"bill":{"type":"uri",
"value":"http:\/\/www.rdfabout.com\/rdf\/usgov\/congress\/108\/
bills\/h1171"},
"title":{"type":"literal", "value":"H.R. 108\/1171: Iris Scan Security Act of 2003",
"datatype":"http:\/\/www.w3.org\/2001\/XMLSchema#string"}}, {"bill":{"type":"uri",
"value":"http:\/\/www.rdfabout.com\/rdf\/usgov\/congress\/108\/
bills\/h1314"},
"title":{"type":"literal", "value":"H.R. 108\/1314: Screening Mammography Act of 2003",
"datatype":"http:\/\/www.w3.org\/2001\/XMLSchema#string"}}, {"bill":{"type":"uri",
"value":"http:\/\/www.rdfabout.com\/rdf\/usgov\/congress\/108\/
bills\/h1384"},
"datatype":"http:\/\/www.w3.org\/2001\/XMLSchema#string"}}, {"bill":{"type":"uri",
"value":"http:\/\/www.rdfabout.com\/rdf\/usgov\/congress\/108\/
bills\/h1418"},
"title":{"type":"literal", "value":"H.R. 108\/1418: Veterans' Housing Equity Act",
"datatype":"http:\/\/www.w3.org\/2001\/XMLSchema#string"}}, ...
}]}}
6.3.6.3
例:HTML
として結果を返す次の例では、SPARQLのSELECTクエリが
HTML
形式で応答を返します。curl --anyauth --user admin:password -i -X POST \ --data-binary @./bills.sparql \
-H "Content-type: application/sparql-query" \
-H "Accept: text/html" http://localhost:8050/v1/graphs/sparql"
=>
HTTP/1.1 200 OK
Content-type: text/html; charset=UTF-8 Server: MarkLogic
Content-Length: 1448 Connection: Keep-Alive Keep-Alive: timeout=5
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SPARQL results</title>
</head>
<body><table border="1"><tr>
<th>bill</th>
<th>title</th>
</tr><tr>
<td><ahref="/v1/graphs/things?iri=http%3a//www.rdfabout.com/
rdf/usgov/congress/108/bills/h1171">http://www.rdfabout.com/
rdf/usgov/congress/108/bills/h1171</a>
</td>
<td>H.R. 108/1171: Iris Scan Security Act of 2003</td>
</tr><tr>
<td><ahref="/v1/graphs/things?iri=http%3a//www.rdfabout.com/
rdf/usgov/congress/108/bills/h1314">http://www.rdfabout.com/
rdf/usgov/congress/108/bills/h1314</a>
</td>
<td>H.R. 108/1314: Screening Mammography Act of 2003</td>
</tr><tr>
<td><ahref="/v1/graphs/things?iri=http%3a//www.rdfabout.com/
rdf/usgov/congress/108/bills/h1384">http://www.rdfabout.com/
rdf/usgov/congress/108/bills/h1384</a>
</td>
<td>H.R. 108/1384: To amend the Railroad Retirement Act of
1974 to eliminate a limitation on benefits.
</td>
</tr>
...
</table>
</body>
</html>
6.3.6.4
例:XML
またはJSON
としてブール型の値を返す次の例では、SPARQLのASKクエリが、(以前の例の)Carolynが
Eunice
よりも後に誕 生したかどうかを判断するために使用されています。ファイルの内容は次のとおりです。
#file: ask-sparql.sparql
PREFIX db: <http://dbpedia.org/resource/>
PREFIX onto: <http://dbpedia.org/ontology/>
ASK {
db:Carolyn_Bessette-Kennedy onto:birthDate ?by . db:Eunice_Kennedy_Shriver onto:birthDate ?bd . FILTER (?by>?bd).
}
SPARQL
のASKクエリが、ブール型の結果をXML
として返します。curl --anyauth --user user:password -i -X POST --data-binary @./ask-sparql.sparql
-H "Content-type: application/sparql-query"
-H "Accept: application/sparql-results+xml"
http://localhost:8050/v1/graphs/sparql
=>
HTTP/1.1 200 OK
Content-type: application/sparql-results+xml Server: MarkLogic
Content-Length: 89 Connection: Keep-Alive
Keep-Alive: timeout=5<sparql
<xmlns="http://www.w3.org/2005/sparql-results/">
<boolean>true</boolean>
</sparql>
同じ
SPARQL
のASKクエリで、ブール型の結果がJSON
として返されるようにする場合 は、次の例のようになります。curl --anyauth --user user:password -i -X POST --data-binary @./ask-sparql.sparql
-H "Content-type: application/sparql-query"
-H "Accept: application/sparql-results+json"
http://localhost:8050/v1/graphs/sparql
=>
HTTP/1.1 200 OK
Content-type: application/sparql-results+json Server: MarkLogic
Content-Length: 17 Connection: Keep-Alive Keep-Alive: timeout=5 {"boolean":true}
6.3.6.5
サポートされていないシリアライゼーションサポートされていないシリアライゼーションの応答に対してGETリクエストを実行する と、「406 Not Acceptable」エラーが発生します。また、SPARQLペイロードのパースで 障害が発生すると、応答によって「400 Bad Request」エラーが発生します。
次に例を示します。
<rapi:error xmlns:rapi="http://marklogic.com/rest-api">
<rapi:status-code>400</rapi:status-code>
<rapi:status>Bad Request</rapi:status>
<rapi:message-code>RESTAPI-INVALIDCONTENT</rapi:message-code>
<rapi:message>RESTAPI-INVALIDCONTENT: (err:FOER0000) Invalid content:
Unexpected Payload: c:\space\example.ttl</rapi:message>
</rapi:error>