SPARQL endpointへの問い合わせ
2008-12-25


DBPediaなど公開されているSPARQL endpointが増えている.
これらのendpointに対して問い合わせ処理をJenaで行う場合はQueryExecutionFactoryクラスのsparqlServiceメソッドを利用する
サンプルコードは以下の通り.

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;

public class UsingSparqlEndpointSample {

    public static void main(String[] args) {
        new UsingSparqlEndpointSample ();
    }

    public UsingSparqlEndpointSample () {
        // query
        String sparql = "select * where {?rc  ?x .} limit 10";
        // sparql endpoint's url
        String service = "http://dbpedia.org/sparql";
        
        Query query = QueryFactory.create(sparql, "",
                Syntax.syntaxSPARQL);
        
        QueryExecution queryExe = QueryExecutionFactory
                .sparqlService(service, query);
        
        ResultSet res =   queryExe.execSelect();
        
        while(res.hasNext()) {
            QuerySolution sol = res.nextSolution();
            System.out.println(sol.get("rc"));
            System.out.println(sol.get("x"));
        }
    }
}
ちなみに関係データベースを使った場合は以下のようになる.
import java.sql.SQLException;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;

public class UsingRDBSample {

    public static void main(String[] args) {
        new UsingRDBSample();
    }

    public UsingRDBSample() {
        try {
            String sparql = "select * where {?rc  ?x .} limit 10";

            Class.forName("com.mysql.jdbc.Driver");
           
            DBConnection conn = new DBConnection(
"jdbc:mysql://localhost/test?useUnicode=true&
characterEncoding=UTF-8",
 "user", "password", "MySQL");
            ModelMaker maker = ModelFactory.createModelRDBMaker(conn);
            Model model = maker.openModel("modelName");
            
            Query query = QueryFactory.create(sparql, "",
                    Syntax.syntaxSPARQL);
           
            DataSource ds = DatasetFactory.create(model);
            QueryExecution queryExe = QueryExecutionFactory
                    .create(query, ds);
            
            ResultSet res =   queryExe.execSelect();
            
            while(res.hasNext()) {
                QuerySolution sol = res.nextSolution();
                System.out.println(sol.get("rc"));
                System.out.println(sol.get("x"));
            }
            
            conn.close();
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
使う機会はこっちのほうが多いかも.
[java]
[jena]
[semanticweb]
[sparql]

コメント(全49件)
※コメントの受付件数を超えているため、この記事にコメントすることができません。


記事を書く
powered by ASAHIネット