使用JENA的参数化SPARQL查询 [英] Parameterized SPARQL query with JENA

查看:105
本文介绍了使用JENA的参数化SPARQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jena框架,JSP和JAVA构建一个小的语义Web应用程序.我有一个远程SPARQL端点,并且我已经编写了一个简单的查询,该查询可以正常工作,但是现在我需要使用一些参数.到目前为止,这是我的代码:

I'm trying to build a small semantic web application using Jena framework, JSP and JAVA. I have a remote SPARQL endpoint and I've already written a simple query which works fine but now I need to use some parameters. Here is my code so far:

final static String serviceEndpoint = "http://fishdelish.cs.man.ac.uk/sparql/";

String comNameQuery = 
        "PREFIX fd: <http://fishdelish.cs.man.ac.uk/rdf/vocab/resource/> " +
        "SELECT ?name ?language ?type" +
        "WHERE { ?nameID fd:comnames_ComName ?name ;" +
        "fd:comnames_Language ?language ;" +
        "fd:comnames_NameType ?type ." +
        "}";

Query query = QueryFactory.create(comNameQuery);  
QueryExecution qe = QueryExecutionFactory.sparqlService(serviceEndpoint,query);

try {
    ResultSet rs = qe.execSelect();
    if ( rs.hasNext() ) {
        System.out.println(ResultSetFormatter.asText(rs));
    }
} 
catch(Exception e) { 
    System.out.println(e.getMessage());
}
finally {
    qe.close();
}

我要做的是参数化?name.我是Jena的新手,我不确定如何在SPARQL查询中使用参数.如果有人可以帮助我,我将不胜感激.

What I want to do is to parameterized ?name. I'm new to Jena and I'm not really sure how to use parameters in a SPARQL query. I would appreciate it if someone could help me with this.

推荐答案

如果只想将变量限制为本地查询具有特定值,则可以使用QueryFactory.create()方法的重载来实现,该方法需要QuerySolutionMap设置值限制.请注意,这不会改变您的查询,只是限制了最终结果,因此这并不是真正的参数化.

If you just want to restrict a variable to have a certain value for local queries you can do so with an overload of the QueryFactory.create() method which takes a QuerySolutionMap to set value restrictions. Note this doesn't alter your query just restricts the final results so this is not really parameterization.

如果您想真正拥有真正的参数化查询(即用变量替换常量),则有多种方法可以执行此操作,具体取决于您的ARQ版本.

If you want to actually have true parameterized queries (i.e. substitute variables for constants) then there are a couple of ways to do this depending on your version of ARQ.

使用任何当前版本(不超过2.9.0)的唯一方法是字符串连接,即在查询中不使用?name而是插入所需的值,例如鲍勃"

Using any current release (up to 2.9.0) the only way to do it is string concatenation i.e. instead of having ?name in your query you would just insert the value you want e.g. "Bob"

使用最新的中继线(2.9.1-SNAPSHOT起),有一个新的ParameterizedSparqlString类,例如,它使用户更加友好.

Using the latest trunk (2.9.1-SNAPSHOT onwards) there is a new ParameterizedSparqlString class which makes this much more user friendly e.g.

ParameterizedSparqlString queryStr = new ParameterizedSparqlString(comNameQuery);
queryStr.setLiteral("name", "Bob");

Query query = QueryFactory.create(queryStr.toString());

事实上,由于ParameterizedSparqlString具有StringBuffer样式的接口,可用于进一步简化代码,可用于一点一点地构建查询,并包括有用的功能,例如在查询之前添加前缀.

And in fact you can simplify your code further since ParameterizedSparqlString has a StringBuffer style interface and can be used to build your query bit by bit and includes useful functionality like prepending prefixes to your query.

此新方法的优点在于,它提供了执行参数化查询的更通用的方法,该方法也可以与更新一起使用,并且可用于准备现有方法未涵盖的远程查询.

The advantage of this new method is that it provides a more generic way of doing parameterized queries that can also be used with updates and is usable for preparing remote queries which the existing methods do not cover.

这篇关于使用JENA的参数化SPARQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆