简体   繁体   English

如何计算Sparql Query中的行数

[英]How to Count the number of rows in Sparql Query

I am working in Eclipse and am using 2 Java Files: Admin.java and SemanticSearch.java. 我在Eclipse中工作并使用2个Java文件:Admin.java和SemanticSearch.java。 Through the the Admin.java I am logging in and checking if the Username and Password are existing in my RDF file. 通过Admin.java我登录并检查我的RDF文件中是否存在用户名和密码。 The function of login in Admin.java calls SemanticSearch.java which runs a SPARQL Query. Admin.java中的登录功能调用运行SPARQL查询的SemanticSearch.java。 My Query is giving me the answer in Console of Eclipse and even onto another file. 我的查询在Eclipse的Console中给我答案,甚至在另一个文件上。 Now my job is to give back the answer to Admin.java either by returning the value or by counting rows and sending that value to Admin.java. 现在我的工作是通过返回值或通过计算行并将该值发送到Admin.java来回复Admin.java的答案。 With this if number of rows is 1 that means the username and password match and I can allow the user to login. 如果行数为1表示用户名和密码匹配,我可以允许用户登录。

But I am not able to do so. 但我无法这样做。 I have tried using count(), Count() as CNT, even tried int res=results.next. 我已经尝试使用count(),Count()作为CNT,甚至尝试过int res = results.next。 But nothing seems to help. 但似乎没有任何帮助。

I am pasting the code below: 我粘贴下面的代码:

Admin.java Admin.java

SemanticSearch semsearch = new SemanticSearch(request.getSession());
semsearch.loadData(REALPATH + RDFDATASOURCEFILE1);
semsearch.searchForUser(response.getOutputStream(),null, userName, password);

In SemanticSearch.java 在SemanticSearch.java中

public void searchForUser(OutputStream out, String xslfile1, 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 * " +"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(); --> here the two rows are printed
    int res=results1.getRowNumber();
    System.out.println(results1); -->here answer is 0
    ResultSetFormatter.out(results1);
    ResultSetFormatter.out(out, results1);
    System.out.println(res);
    try {
        if (xslfile1 != null)
        {
            out.write(ResultSetFormatter.asXMLString(results1, xslfile1).getBytes("UTF-8"));
                System.out.println(results1);
                System.out.println(xslfile1);
            System.out.println("I am in if");
        }
        else
        {
           out.write(ResultSetFormatter.asXMLString(results1).getBytes(
                        "UTF-8"));
           System.out.println("I am in else");
        }

    } catch (IOException e) {
            e.printStackTrace();
    }
}   

Please Help, I am struggling to get this from a week, Regards, Archana. 请帮助,我很难在一周内得到这个,问候,Archana。

In SPARQL 1.1, which may be supported in the triplestore you're using, the syntax is: 在您正在使用的三元组中可能支持的SPARQL 1.1中,语法为:

SELECT (COUNT(*) AS ?count)
WHERE {
  ...
}

You should get an integer returned in the ?count column, with the number of results. 您应该在?count列中返回一个返回的整数,其中包含结果数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM