简体   繁体   English

在耶拿(Jena)中替换SPARQL查询中的变量

[英]Replace variables within a SPARQL query in Jena

I need to replace some variables(indx, placx, datx, tempx) within a SPARQL query and for the same I am using following code: 我需要在SPARQL查询中替换一些变量(indx,placx,datx,tempx),对于相同的情况,我正在使用以下代码:

ParameterizedSparqlString ps = new ParameterizedSparqlString();
ps.setCommandText("INSERT DATA" + 
            "{" + 
             ":indx :locatedIn :placx ;" + ":onDate datx ;" + ":measures tempx ." +              
            "}");
ps.setIri(":","http://www.example.com/tempsensor#");
ps.setLiteral("indx",ind);
ps.setLiteral("placx",plac);
ps.setLiteral("datx",dat);
ps.setLiteral("tempx",temp);

System.out.println(ps.toString()); 的System.out.println(ps.toString());

On running, it simply prints out: 在运行时,它只是打印出:
INSERT DATA{:indx :locatedIn :placx ;:onDate datx ;:measures tempx .} 插入数据{:indx:locatedIn:placx;:onDate datx;:measures tempx。}
How can I insert the values successfully within query? 如何在查询中成功插入值?

UPDATE #1 更新1
Changes introduced in code are as: 代码中引入的更改如下:

  ps.setBaseUri("http://www.example.com/tempsensor#"); ps.setCommandText("PREFIX : <http://www.example.com/tempsensor#>\\n"+ "INSERT DATA\\n" + "{\\n" + " ?indx :locatedIn ?placx ;" + ":onDate ?datx ;" + " :measures ?tempx .\\n" + "}"); ps.setIri("?indx", ps.getBaseUri()+ind); ps.setIri("placx",ps.getBaseUri()+plac); ps.setLiteral("datx",dat); ps.setLiteral("tempx",temp); System.out.println(ps.toString()); 

I am getting output as: 我得到的输出为:

PREFIX : <http://www.example.com/tempsensor#>
INSERT DATA
{
 <#ind1>  :locatedIn <#Delhi> ;:onDate "2015-02-01" ; :measures 13 .
}

I don't know where I am doing wrong. 我不知道我在哪里做错了。 I need an output like: 我需要这样的输出:

 PREFIX : <http://www.example.com/tempsensor#>
    INSERT DATA
    {
     :ind1  :locatedIn :Delhi ; :onDate 2015-02-01 ; :measures 13 .
    }

ie, ind1 should be without quotes,angle brackets,and # and date should be without quotes. 即,ind1应该不带引号,尖括号和#和date应该不带引号。 locatedIn is an objecttypeProperty and onDate and measures are Datatype properties. locatedIn是一个objecttypeProperty和onDate,而measures是Datatype属性。 Actually, I need to read data from CSV row by row and insert into an ontology. 实际上,我需要逐行从CSV中读取数据并插入到本体中。 For this I want to write the script where I will read row of CSV and convert the same in SPARQL string. 为此,我想编写脚本,在其中我将读取CSV行并在SPARQL字符串中将其转换。 In the next step, I will make a update SPARQL query with this string created. 在下一步中,我将使用创建的此字符串进行更新SPARQL查询。

You need to use variables , not constants in the query. 您需要在查询中使用变量 ,而不是常量。 Eg, your string should originally be: 例如,您的字符串最初应为:

insert data { ?indx :locatedIn ?placx ... }

Then you can use the various setXXX functions. 然后,您可以使用各种setXXX函数。 However, note that there's more than just setLiteral. 但是,请注意,不仅仅是setLiteral。 You shouldn't use setLiteral to set the value of ?indx here, because literals cannot be the subjects of triples in RDF. 您不应该在这里使用setLiteral设置?indx的值,因为文字不能成为RDF中三元组的主题 For that case, you'd want to use setIri. 对于这种情况,您需要使用setIri。 See the docs for ParameterizedSparqlString for the full list. 有关完整列表,请参阅docs的ParameterizedSparqlString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM