7.14 JavaScript Object Notation (JSON)形式のサポート
JavaScript Object Notation (JSON)形式は、SPARQL問合せのレスポンスのためにサポートされます。JSONデータ形式は単純、コンパクトで、JavaScriptプログラムに適しています。
たとえば、次のJavaコード・スニペット(ResultSetFormatter.outputAsJSON
メソッドをコールする)があるとします。
Oracle oracle = new Oracle(jdbcUrl, user, password);
GraphOracleSem graph = new GraphOracleSem(oracle, modelName);
ModelOracleSem model = new ModelOracleSem(graph);
graph.add(new Triple(
Node.createURI("http://ds1"),
Node.createURI("http://dp1"),
Node.createURI("http://do1")
)
);
graph.add(new Triple(
Node.createURI("http://ds2"),
Node.createURI("http://dp2"),
Node.createURI("http://do2")
)
);
graph.commitTransaction();
Query q = QueryFactory.create("select ?s ?p ?o where {?s ?p ?o}",
Syntax.syntaxARQ);
QueryExecution qexec = QueryExecutionFactory.create(q, model);
ResultSet results = qexec.execSelect();
ResultSetFormatter.outputAsJSON(System.out, results);
JSON出力は次のとおりです。
{ "head": { "vars": [ "s" , "p" , "o" ] } , "results": { "bindings": [ { "s": { "type": "uri" , "value": "http://ds1" } , "p": { "type": "uri" , "value": "http://dp1" } , "o": { "type": "uri" , "value": "http://do1" } } , { "s": { "type": "uri" , "value": "http://ds2" } , "p": { "type": "uri" , "value": "http://dp2" } , "o": { "type": "uri" , "value": "http://do2" } } ] } }
前述の例を、Oracle Databaseに対して直接ではなく、リモートのSPARQLエンドポイントを問い合せるようにするには、次のように変更します。(リモートのSPARQLエンドポイントがファイアウォールの外側にある場合、HTTPプロキシを設定する必要があります。)
Query q = QueryFactory.create("select ?s ?p ?o where {?s ?p ?o}", Syntax.syntaxARQ); QueryExecution qe = QueryExecutionFactory.sparqlService(sparqlURL, q); ResultSet results = qexec.execSelect(); ResultSetFormatter.outputAsJSON(System.out, results);
この項の最初の例を名前付きグラフに拡張するため、次のコード・スニペットでは、同じOracleモデルに2つのクワッドを追加し、名前付きグラフベースのSPARQL問合せを実行して、問合せ出力をJSON形式にシリアライズします。
DatasetGraphOracleSem dsgos = DatasetGraphOracleSem.createFrom(graph);
graph.close();
dsgos.add(new Quad(Node.createURI("http://g1"),
Node.createURI("http://s1"),
Node.createURI("http://p1"),
Node.createURI("http://o1")
)
);
dsgos.add(new Quad(Node.createURI("http://g2"),
Node.createURI("http://s2"),
Node.createURI("http://p2"),
Node.createURI("http://o2")
)
);
Query q1 = QueryFactory.create(
"select ?g ?s ?p ?o where { GRAPH ?g {?s ?p ?o} }");
QueryExecution qexec1 = QueryExecutionFactory.create(q1,
DatasetImpl.wrap(dsgos));
ResultSet results1 = qexec1.execSelect();
ResultSetFormatter.outputAsJSON(System.out, results1);
dsgos.close();
oracle.dispose();
JSON出力は次のとおりです。
{ "head": { "vars": [ "g" , "s" , "p" , "o" ] } , "results": { "bindings": [ { "g": { "type": "uri" , "value": "http://g1" } , "s": { "type": "uri" , "value": "http://s1" } , "p": { "type": "uri" , "value": "http://p1" } , "o": { "type": "uri" , "value": "http://o1" } } , { "g": { "type": "uri" , "value": "http://g2" } , "s": { "type": "uri" , "value": "http://s2" } , "p": { "type": "uri" , "value": "http://p2" } , "o": { "type": "uri" , "value": "http://o2" } } ] } }
また、次の例のように、FusekiベースのSPARQLエンドポイントに対してHTTPを通してJSONレスポンスを取得することもできます。通常、SPARQL Webサービス・エンドポイントに対してSPARQL問合せを実行する場合は、Accept request-head
フィールドはapplication/sparql-results+xml
に設定されます。JSON出力形式の場合は、Accept request-head
フィールドをapplication/sparql-results+json
に置き換えます。
http://hostname:7001/fuseki/oracle?query=<URL_ENCODED_SPARQL_QUERY>&output=json