来自 sparql-service 的非结构化 return

Unstructured return from sparql-service

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

提前致谢!

你可以使用ResultSet

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

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

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

将打印名为 "s"

的变量的所有结果

see here also