如何在 sparql 中检索列的值
执行查询后,我需要检索列的值。查询运行并在 Eclipse 控制台中给出正确答案。我只需要从行中检索属性值之一。 完整代码存储在SemanticSearch.java中。该函数是从 Admin.java 调用的。
public int searchForUser(String userName, String password)
{
String prolog="PREFIX kb:<"+VUSER.getURI()+">";
System.out.println("Search for user in semantic search.java");
String queryString1=prolog
+"\n" +"SELECT interest "
+WHERE {?x kb:Uname ?username. ?x kb:Password ?password. ?x kb:Interest ?interest. "
+"FILTER regex(?username, \"" +userName +"\" )}";
System.out.println(queryString1);
Query query=QueryFactory.create(queryString1);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results1 = qexec.execSelect();
//System.out.println(results1);
//ResultSetFormatter.out(results1);
ResultSetFormatter.out(System.out, results1,query);
if(results1.getRowNumber()>0)
{
QuerySolution soln=results1.nextSolution();
Literal RES=soln.getLiteral("Interest");
String RES=results1.
int res=results1.getRowNumber();
System.out.println(RES);
return res;
}
else
{
return 0;
}
}
输出是:
PREFIX kb:http://protege.stanford.edu/kb#
SELECT * WHERE {?x kb:Uname ?username. ?x kb:Password ?password. ?x kb:Interest ?interest. FILTER regex(?username, "anu" )}
| x | username | password | interest |
| kb:Anvika | "anu" | "anu" | "C language" |
出现此错误后:
Feb 10, 2011 10:50:56 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet Admin threw exception java.util.NoSuchElementException: QueryIteratorCloseable
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.nextBinding(QueryIteratorBase.java:93)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.nextBinding(ResultSetStream.java:74)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.nextSolution(ResultSetStream.java:91)
at semanticsearch.SemanticSearch.searchForUser(SemanticSearch.java:126)
at controller.Admin.doGet(Admin.java:84) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
我无法检索列中的值。 帮助。 问候。
Ineed to retrieve a column's value once the query is executed. The query runs and gives me the correct answer in the Eclipse console. I need to just retrieve one of the attribute value from the row.
The Complete code is stored in SemanticSearch.java. This function is called from Admin.java.
public int searchForUser(String userName, String password)
{
String prolog="PREFIX kb:<"+VUSER.getURI()+">";
System.out.println("Search for user in semantic search.java");
String queryString1=prolog
+"\n" +"SELECT interest "
+WHERE {?x kb:Uname ?username. ?x kb:Password ?password. ?x kb:Interest ?interest. "
+"FILTER regex(?username, \"" +userName +"\" )}";
System.out.println(queryString1);
Query query=QueryFactory.create(queryString1);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results1 = qexec.execSelect();
//System.out.println(results1);
//ResultSetFormatter.out(results1);
ResultSetFormatter.out(System.out, results1,query);
if(results1.getRowNumber()>0)
{
QuerySolution soln=results1.nextSolution();
Literal RES=soln.getLiteral("Interest");
String RES=results1.
int res=results1.getRowNumber();
System.out.println(RES);
return res;
}
else
{
return 0;
}
}
The Output is:
PREFIX kb:http://protege.stanford.edu/kb#
SELECT * WHERE {?x kb:Uname ?username. ?x kb:Password ?password. ?x kb:Interest ?interest. FILTER regex(?username, "anu" )}
| x | username | password | interest |
| kb:Anvika | "anu" | "anu" | "C language" |
After this error comes:
Feb 10, 2011 10:50:56 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet Admin threw exception java.util.NoSuchElementException: QueryIteratorCloseable
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.nextBinding(QueryIteratorBase.java:93)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.nextBinding(ResultSetStream.java:74)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.nextSolution(ResultSetStream.java:91)
at semanticsearch.SemanticSearch.searchForUser(SemanticSearch.java:126)
at controller.Admin.doGet(Admin.java:84) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
I am not able to retrieve the value at a column.
Help.
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,那么我的下一个怀疑是您尝试迭代超过迭代器的末尾。使用条件 results1.getRowNumber()>0 是一个非常糟糕的主意,因为它总是 > > 。据我所知是 0(尽管 ARQ 的 Javadoc 还不清楚)。
相反,我会用 results1.hasNext() 替换条件,看看是否可以修复它
已编辑的原始答案
我怀疑这只是变量名区分大小写的情况。
将行: 替换
为行:
并且我怀疑它会正常工作。
Ok so then my next suspicion would be that you've tried to iterate past the end of the iterator. Using the condition results1.getRowNumber()>0 is a very bad idea since it is always going to be > 0 as far as I can tell (though this isn't clear from the Javadoc for ARQ).
Instead I would replace the condition with results1.hasNext() and see if that fixes it
Redacted Original Answer
I suspect this is just a case of variable names being case sensitive.
Replace the line:
With the line:
And I suspect it will work fine.