2017-09-28 92 views
0

我在Apache Jena中執行的SPARQL查詢有問題。查詢通過sparqlservice傳遞給LinkedGeoData。查詢看起來像這樣:從sparql-service返回非結構化

Prefix lgdo: <http://linkedgeodata.org/ontology/> 
Prefix geom: <http://geovocab.org/geometry#> 
Prefix ogc:<http://www.opengis.net/ont/geosparql#> 
Select ?s ?l ?g 
From <http://linkedgeodata.org> { 
    ?s 
    a lgdo:Amenity ; 
    rdfs:label ?l ;  
    geom:geometry [ 
     ogc:asWKT ?g 
    ] . 

    Filter(<bif:st_intersects> (?g, <bif:st_point> (12.372966, 51.310228), 0.1)) . 
} 

我用<bif:st_intersects><bif:st_point>,爲前綴bif由端點http://linkedgeodata.org/sparql定義,但沒有在我的Java代碼:

String queryString2 = 
         "Prefix lgdo: <http://linkedgeodata.org/ontology/> "+ 
         "Prefix geom: <http://geovocab.org/geometry#> "+ 
         "Prefix ogc:<http://www.opengis.net/ont/geosparql#> "+ 
         "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+ 
         "Select ?s ?l ?g From <http://linkedgeodata.org> { ?s a lgdo:Amenity ; rdfs:label ?l ;  geom:geometry [  ogc:asWKT ?g ] . Filter (<bif:st_intersects> (?g, <bif:st_point> (12.372966, 51.310228), 0.1)) .}"; 
    QueryExecution qexec2 = QueryExecutionFactory.sparqlService("http://linkedgeodata.org/sparql", query2); 

Iterator<Triple> triples2 = qexec2.execConstructTriples(); 

while(triples2.hasNext()) 
       { 

        Triple triple = triples2.next(); 

        Node s = triple.getSubject(); 
        Node p = triple.getPredicate(); 
        Node o = triple.getObject(); 

        System.out.println(s.toString()); 
        System.out.println(p.toString()); 
        System.out.println(o.toString()); 
        System.out.println("\n"); 
       } 

這裏是我的輸出的摘錄:

b80d5f650b2e98240baf560415cdef40 
http://www.w3.org/2005/sparql-results#value 
"Kita EinSteinchen" 


559c608a13e5bc6e4f98ed9ea24ee97d 
http://www.w3.org/2005/sparql-results#binding 
b80d5f650b2e98240baf560415cdef40 


0759546664f20899fa5455abd7dbcc74 
http://www.w3.org/2005/sparql-results#variable 
"g" 


0759546664f20899fa5455abd7dbcc74 
http://www.w3.org/2005/sparql-results#value 
"LINESTRING(12.3802344 51.3325989,12.380315 51.3327169,12.3803773 51.3327029,12.3804007 51.3327403,12.3799895 51.3328375,12.3800728 51.332975,12.3800728 51.332975,12.3800992 51.3330203,12.3799822 51.3330486,12.3799632 51.3330555,12.3799248 51.3330256,12.379842 51.3330456,12.3798641 51.3331146,12.3799149 51.3331622,12.3795303 51.333342,12.379223 51.3328586,12.3792961 51.3328139,12.3802344 51.3325989)"^^http://www.openlinksw.com/schemas/virtrdf#Geometry 

但是,一旦我直接在SPARQL端點運行相同的查詢(Query on LinkedGeoData SPARQL-service)我得到了一個結構良好的解決方案,即獲得每個變量(?s ?l ?g)的值。我如何在我的Java程序中獲得相同的格式? - 我不想爲我需要的解決方案掃描那個奇怪的返回字符串。

在此先感謝!

回答

3

可以使用ResultSet

ResultSet rs = qexec2.execSelect(); 
ResultSetFormatter.out(rs); 

這種打印格式的結果安慰,但你也可以使用ResultSet所提供的方法解析結果如你所願

rs.forEachRemaining(r -> System.out.println(r.get("s"))); 

將打印所有來自變量「s」的結果

see here also

+0

非常感謝!有效 :) –