第6章 RDFグラフの更新

RDFグラフ機能は、SPARQL Update (http://www.w3.org/TR/sparql11-update/)をサポートしています。これはSPARULとも呼ばれています。これに関係する主なプログラミングAPIは、Apache JenaクラスUpdateAction (com.hp.hpl.jena.updateパッケージ内)と、RDFグラフ機能クラスOracleGraphNoSqlおよびDatasetGraphNoSqlです。次の例は、データベースに格納されている関連モデルから<http://example/graph>という名前付きグラフにあるすべてのトリプルを削除するSPARQL Update操作を示しています。

OracleGraphNoSql oracleGraph = .... ;
DatasetGraphNoSql dsgos = DatasetGraphNoSql.createFrom(oracleGraph); 

// SPARQL Update operation
String szUpdateAction = "DROP GRAPH <http://example/graph>"; 
// Execute the Update against a DatasetGraph instance 
// (can be a Jena Model as well)
UpdateAction.parseExecute(szUpdateAction, dsgos); 

Oracle NoSQL Databaseは空の名前付きグラフに関する情報を保持しません。つまり、このグラフにトリプルを追加せずにCREATE GRAPH <graph_name>を呼び出した場合、トリプルは作成されないということです。Oracle NoSQL Databaseを使用すると、次の例で示しているように、CREATE GRAPHのステップを安全にスキップできます。

PREFIX example: <http://example/>
INSERT DATA { 
    GRAPH <http://example/graph> { 
		example:anne example:age 30 . 
		example:peter example:birthyear 1982  
	}
} ;
DELETE DATA { 
     GRAPH <http://example/graph> { example:anne example:age 30 . }
}

次の例は、複数の挿入および削除操作を伴うSPARQL Update操作(ARQ 2.9.2)を示しています。

PREFIX : <http://example/>
CREATE GRAPH <http://example/graph>;
INSERT DATA { example:anne example:age 30 };
INSERT DATA { example:peter example:birthyear 1982 };
DELETE DATA { example:peter example:birthyear 1982 };
INSERT DATA { 
    GRAPH <http://example/graph> { 
		example:anne example:age 30 . 
		example:peter example:birthyear 1982  
	}
};
DELETE DATA { 
     GRAPH <http://example/graph> { example:anne example:age 30 }
} 

上の例で空のDatasetGraphNoSqlに対して更新操作を実行した後で、SPARQL問合せSELECT ?s ?p ?o WHERE {?s ?p ?o}を実行すると、次の応答が生成されます。

-----------------------------------------------------
| s                     | p                    | o  |
=====================================================
| <http://example/anne> | <http://example/age> | 30 |
-----------------------------------------------------

同じデータを使用してSPARQL問合せSELECT ?s ?p ?o ?g where {GRAPH ?g {?s ?p ?o}}を実行すると、次の応答が生成されます。

-----------------------------------------------------------------
| s                      | p                          | o    |
=================================================================
| <http://example/peter> | <http://example/birthyear> | 1982 |
-----------------------------------------------------------------
--------------------------------
|             g                |
================================
| <http://example/graph> |
--------------------------------

joseki-config.ttlファイルで各行の先頭のコメント文字(##)を削除すると、SPARQL Update操作のJava APIを使用するかわりに、SPARQL Update操作を受け入れるようにApache Jena Josekiを構成できます。

## <#serviceUpdate>
## rdf:type joseki:Service ;
## rdfs:label "SPARQL/Update" ;
## joseki:serviceRef "update/service" ;
## # dataset part
## joseki:dataset <#oracle>;
## # Service part. 
## # This processor will not allow either the protocol,
## # nor the query, to specify the dataset.
## joseki:processor joseki:ProcessorSPARQLUpdate
## .
## 
## <#serviceRead>
## rdf:type joseki:Service ;
## rdfs:label "SPARQL" ;
## joseki:serviceRef "sparql/read" ;
## # dataset part
## joseki:dataset <#oracle> ; ## Same dataset
## # Service part. 
## # This processor will not allow either the protocol,
## # nor the query, to specify the dataset.
## joseki:processor joseki:ProcessorSPARQL_FixedDS ;
## .

joseki-config.ttlファイルの編集後は、Apache Jena Joseki Webアプリケーションを再起動する必要があります。これで、次のように単純な更新操作を試すことができます。

  1. ブラウザで、http://<hostname>:7001/joseki/update.htmlに移動します。

  2. テキスト・ボックスに次のように入力するか、貼り付けます。

    PREFIX example: <http://example/>
    INSERT DATA { 
    GRAPH <http://example/g1> { example:peter example:birthyear 1970  }
    }
    
  3. 「Perform SPARQL Update」をクリックします。

更新操作が正常に実行されたことを確認するために、http://<hostname>:7001/josekiに移動して次の問合せを入力します。

SELECT *
WHERE 
{ GRAPH <http://example/g1>}{?s ?p ?o}}

応答には、次のトリプルが含まれているはずです。

<http://example/peter> <http://example/birthyear> "1970"

SPARQL Updateは、HTTP POST操作を使用してhttp://<hostname>:7001/joseki/update/serviceに送信することもできます。たとえば、cURL (http://en.wikipedia.org/wiki/CURL)を使用してHTTP POSTリクエストを送信し、更新操作を実行できます。

curl --data "request=PREFIX%20%3A%20%3Chttp%3A%2F%2Fexample \
        %2F%3E%20%0AINSERT%20DATA%20%7B%0A%20%20GRAPH%20%3 \
        Chttp%3A%2F%2Fexample%2Fg1%3E%20%7B%20%3Ar%20%3Ap%20 \
        888%20%7D%0A%7D%0A" \
        http://hostname:7001/joseki/update/service

上の例で、URLエンコードされている文字列は名前付きグラフへの単純なINSERT操作です。デコードすると、次のように表示されます。

PREFIX : <http://example/>
INSERT DATA { 
GRAPH <http://example/g1> { :r :p 888 }