如何在 sparql 中检索列的值

发布于 2024-10-16 21:20:14 字数 3210 浏览 2 评论 0原文

执行查询后,我需要检索列的值。查询运行并在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2024-10-23 21:20:14

好吧,那么我的下一个怀疑是您尝试迭代超过迭代器的末尾。使用条件 results1.getRowNumber()>0 是一个非常糟糕的主意,因为它总是 > > 。据我所知是 0(尽管 ARQ 的 Javadoc 还不清楚)。

相反,我会用 results1.hasNext() 替换条件,看看是否可以修复它

已编辑的原始答案


我怀疑这只是变量名区分大小写的情况。

将行: 替换

Literal RES=soln.getLiteral("Interest");

为行:

Literal RES=soln.getLiteral("interest");

并且我怀疑它会正常工作。

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:

Literal RES=soln.getLiteral("Interest");

With the line:

Literal RES=soln.getLiteral("interest");

And I suspect it will work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文