简体   繁体   中英

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. Through the the Admin.java I am logging in and checking if the Username and Password are existing in my RDF file. The function of login in Admin.java calls SemanticSearch.java which runs a SPARQL Query. My Query is giving me the answer in Console of Eclipse and even onto another file. 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. With this if number of rows is 1 that means the username and password match and I can allow the user to login.

But I am not able to do so. I have tried using count(), Count() as CNT, even tried int res=results.next. But nothing seems to help.

I am pasting the code below:

Admin.java

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

In 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.

In SPARQL 1.1, which may be supported in the triplestore you're using, the syntax is:

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

You should get an integer returned in the ?count column, with the number of results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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