sparql-service的非结构化返回

时间:2017-09-28 12:05:36

标签: java sparql jena virtuoso

我从Apache Jena执行SPARQL查询时遇到问题。查询通过sparqlservice传递给LinkedGeoData。查询看起来像这样:

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程序中获得相同的格式? - 我不想为我需要的解决方案扫描那个奇怪的返回字符串。

提前致谢!

1 个答案:

答案 0 :(得分:3)

您可以使用ResultSet

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

这会将格式化结果打印到控制台,但您也可以使用ResultSet提供的方法根据需要解析结果

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

将打印名为&#34; s&#34;

的变量的所有结果

see here also