Example5.java: JOIN_METHODを使用するSPARQL問合せ

この例では、問合せパターンに対して使用する結合方法を選択するときの結合方法(JOIN_METHOD={nl, hash})の選択など、追加機能を伴うSPARQL問合せについて説明します。RDFファイルexample.ntに含まれ、次のように表明されるクワッドをロードします。このファイルは、RDFグラフ機能のexamplesディレクトリに収録されています。

次に、グラフ<http://example.org/alice/foaf.rdf>で、他の人を知っている人の名前をすべて検索します。その際、ネステッド・ループ結合操作を使用して、SPARQL問合せの問合せパターン間で取得されるバインディングをマージします。

import com.hp.hpl.jena.query.*;
import org.openjena.riot.Lang;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import oracle.rdf.kv.client.jena.*;

public class Example5
{
  
public static void main(String[] args) throws Exception
  {
 
String szStoreName  = args[0];
String szHostName   = args[1];
String szHostPort   = args[2];
   
// Create connection    
OracleNoSqlConnection conn 
     = OracleNoSqlConnection.createInstance(szStoreName,
                                            szHostName, 
                                            szHostPort);

// Create the datasetgraph    
OracleGraphNoSql graph = new OracleGraphNoSql(conn);
DatasetGraphNoSql datasetGraph = DatasetGraphNoSql.createFrom(graph);
   
// Close graph, as it is no longer needed
graph.close();
    
// Clear dataset
datasetGraph.clearRepository();
    
Dataset ds = DatasetImpl.wrap(datasetGraph);
    
// Load data from file into the dataset    
DatasetGraphNoSql.load("example.nt", Lang.NQUADS, conn, 
                           "http://example.com"); //base URI

// change hint to hash to test hash join, or remove to use default
// join settings
String szQuery = 
" PREFIX ORACLE_SEM_FS_NS: "                         +
" <http://oracle.com/semtech#join_method=nl>"        +
" PREFIX foaf: <http://xmlns.com/foaf/0.1/>"         +
" SELECT ?name1 ?name2 "                             +
" WHERE { "                                          +
"   graph <http://example.org/alice/foaf.rdf> { "    +
"     ?person1 foaf:knows ?person2 . "               +
"     ?person1 foaf:name ?name1 . "                  +
"     ?person2 foaf:name ?name2 . "                  +
"   } "                                              +
" } ";
    
System.out.println("Execute query " + szQuery);
    
Query query = QueryFactory.create(szQuery);
QueryExecution qexec = QueryExecutionFactory.create(query, ds);
    
try {
      ResultSet results = qexec.execSelect();
      ResultSetFormatter.out(System.out, results, query);
    }
    
finally {
      qexec.close();
    }
    
ds.close();
conn.dispose();   
  }
}

この例をコンパイルして実行するコマンドと、想定されるJavaコマンドの出力は次のとおりです。

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example5.java

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example5 <store_name> \
<host_name> <host_port>
-------------------
| name1   | name2 |
===================
| "Alice" | "Bob" |
------------------- 

ハッシュ結合の選択は、コードで次の行を変更するとテストできます。このJavaクラスの出力は、前述の出力と同じになります。

PREFIX ORACLE_SEM_FS_NS: <http://oracle.com/semtech#join_method=hash>