簡體   English   中英

在耶拿(Jena)中替換SPARQL查詢中的變量

[英]Replace variables within a SPARQL query in Jena

我需要在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());

在運行時,它只是打印出:
插入數據{:indx:locatedIn:placx;:onDate datx;:measures tempx。}
如何在查詢中成功插入值?

更新1
代碼中引入的更改如下:

  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()); 

我得到的輸出為:

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

我不知道我在哪里做錯了。 我需要這樣的輸出:

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

即,ind1應該不帶引號,尖括號和#和date應該不帶引號。 locatedIn是一個objecttypeProperty和onDate,而measures是Datatype屬性。 實際上,我需要逐行從CSV中讀取數據並插入到本體中。 為此,我想編寫腳本,在其中我將讀取CSV行並在SPARQL字符串中將其轉換。 在下一步中,我將使用創建的此字符串進行更新SPARQL查詢。

您需要在查詢中使用變量 ,而不是常量。 例如,您的字符串最初應為:

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

然后,您可以使用各種setXXX函數。 但是,請注意,不僅僅是setLiteral。 您不應該在這里使用setLiteral設置?indx的值,因為文字不能成為RDF中三元組的主題 對於這種情況,您需要使用setIri。 有關完整列表,請參閱docs的ParameterizedSparqlString

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM