如何将字符串变量添加到 SPARQL 查询?
How to add a string variable to the SPARQL query?
我想将用户的输入整合到一个 SPARQL 查询中。这是我现有的代码,但我无法让它工作。
public void setName(String name) {
String formattedName = name.replace(" ", "-");
String query = "SELECT ?p ?o WHERE { <http://person/" + formattedName + "> ?p ?o }" ;
System.out.println(query);
QueryExecution qe = QueryExecutionFactory.sparqlService(
"http://localhost:3030/Date/query",
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " + query);
ResultSet results = qe.execSelect();
System.out.println(results);
qe.close();
}
然后我在控制台中得到以下输出:
SELECT ?p ?o WHERE { <http://person/Mark-John> ?p ?o }
com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX@28df8261
第一行是查询,第二行是结果,应该是收到的数据...
如果我尝试如下,它会起作用:
execSelectAndPrint(
"http://localhost:3030/Date/query",
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " + "SELECT ?p ?o WHERE { <http://person/Mark-John> ?p ?o }"
);
打印:
| <http://purl.org/dc/elements/1.1/name> | "Mark John" |
| <http://purl.org/dc/elements/1.1/nationality> | "American" |
如何更改第一个代码以显示第二个代码的结果?
你的问题标题与你在文中描述的问题无关...
你问的更多"How to print the Apache Jena API ResultSet
?"
然后令人困惑的是,您已经有一种名为 execSelectAndPrint
的方法可以执行您想要的操作。所以问题是为什么你不能(重新)使用该方法或至少该方法中的相应代码行?
Apache Jena API 文档,特别是可以找到 SPARQL 的部分 here
ResultSet
是一个迭代器,因此迭代它:
ResultSet results = qexec.execSelect();
for ( ; results.hasNext() ; ) {
QuerySolution qs = results.next() ;
... // extract the variables
}
或使用convenience methods之一:
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(results);
小评论:
你的方法叫做setName
,看起来有点奇怪w.r.t。到你在这个方法中所做的事情。
我想将用户的输入整合到一个 SPARQL 查询中。这是我现有的代码,但我无法让它工作。
public void setName(String name) {
String formattedName = name.replace(" ", "-");
String query = "SELECT ?p ?o WHERE { <http://person/" + formattedName + "> ?p ?o }" ;
System.out.println(query);
QueryExecution qe = QueryExecutionFactory.sparqlService(
"http://localhost:3030/Date/query",
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " + query);
ResultSet results = qe.execSelect();
System.out.println(results);
qe.close();
}
然后我在控制台中得到以下输出:
SELECT ?p ?o WHERE { <http://person/Mark-John> ?p ?o }
com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX@28df8261
第一行是查询,第二行是结果,应该是收到的数据...
如果我尝试如下,它会起作用:
execSelectAndPrint(
"http://localhost:3030/Date/query",
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " + "SELECT ?p ?o WHERE { <http://person/Mark-John> ?p ?o }"
);
打印:
| <http://purl.org/dc/elements/1.1/name> | "Mark John" |
| <http://purl.org/dc/elements/1.1/nationality> | "American" |
如何更改第一个代码以显示第二个代码的结果?
你的问题标题与你在文中描述的问题无关...
你问的更多"How to print the Apache Jena API ResultSet
?"
然后令人困惑的是,您已经有一种名为 execSelectAndPrint
的方法可以执行您想要的操作。所以问题是为什么你不能(重新)使用该方法或至少该方法中的相应代码行?
Apache Jena API 文档,特别是可以找到 SPARQL 的部分 here
ResultSet
是一个迭代器,因此迭代它:
ResultSet results = qexec.execSelect();
for ( ; results.hasNext() ; ) {
QuerySolution qs = results.next() ;
... // extract the variables
}
或使用convenience methods之一:
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(results);
小评论:
你的方法叫做setName
,看起来有点奇怪w.r.t。到你在这个方法中所做的事情。