How to run query on Apache Jackrabbit, explain with example
Asked Answered
E

3

5

I am using Apache Jackrabbit as a database.

In my case, root node has numbers of child nodes(only at depth 1).
All child node has unique name, i.e., some Integer.
Each child Node have some properties that I have used further.

My task

I have to take top 10 nodes whose keys(integer values) are minimum.

My thinking

To achieve above goal, I make a query that sorts the keys of all child nodes, and pick top 10. Then by using that keys, I get all corresponding nodes, and after working, delete all that key/value pairs.

For that I searched a lot on the internet how to run the query. Can you please tell me how to run query on apache jackrabit. It is good, if you explain with example.

Edit no. 1

public class JackRabbit {

public static void main(String[] args) throws Exception {

    try {

        Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");
        javax.jcr.Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));

        Node root = session.getRootNode();


        // Obtain the query manager for the session via the workspace ...
        javax.jcr.query.QueryManager queryManager = session.getWorkspace().getQueryManager();

        // Create a query object ...
        String expression = "select * from nt:base where name= '12345' ";
        javax.jcr.query.Query query = queryManager.createQuery(expression, javax.jcr.query.Query.JCR_SQL2);

        // Execute the query and get the results ...
        javax.jcr.query.QueryResult result = query.execute();


        session.logout();

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

}

Exception

javax.jcr.query.InvalidQueryException: Query:
select * from nt:(*)base where name= '12345'; expected: <end>
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.apache.jackrabbit.spi2dav.ExceptionConverter.generate(ExceptionConverter.java:69)
    at org.apache.jackrabbit.spi2dav.ExceptionConverter.generate(ExceptionConverter.java:51)
    at org.apache.jackrabbit.spi2dav.ExceptionConverter.generate(ExceptionConverter.java:45)
    at org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.executeQuery(RepositoryServiceImpl.java:2004)
    at org.apache.jackrabbit.jcr2spi.WorkspaceManager.executeQuery(WorkspaceManager.java:349)
    at org.apache.jackrabbit.jcr2spi.query.QueryImpl.execute(QueryImpl.java:149)
    at jackrabbit.JackRabbit.main(JackRabbit.java:36)

I want to write a query of below scenereo

enter image description here

Here nodes having integer value have some properties. I want to sort these nodes by their integer values, and extract top 50 nodes for further processing.

Help me in that.

Equidistant answered 22/5, 2013 at 17:33 Comment(0)
B
5

You should quote your node type name in JCR-SQL2:

select * from [nt:base]

This is one of the main differences between JCR-SQL and JCR-SQL2. Besides, name is a dynamic operand taking a selector argument. So a better way to write your query would be this:

select * from [nt:base] as b where name(b) = '12345'
Barrybarrymore answered 24/5, 2013 at 7:29 Comment(2)
For me - using jackrabbit - name(b) will throw javax.jcr.UnsupportedRepositoryOperationException ... but using localname(b) works fine.Kiyohara
@kevinjansz: Huh, could be. It's been a while and I haven't really touched JCR, since... so I'm afraid I won't be of any helpBarrybarrymore
S
1

You have different ways of executing your queries depending on the query language you want to use.

Take a look at this code for some simple query using only the API and not SQL like string queries. You can take a look at JBoss Modeshape documentation for examples too since it is another JCR 2.0 implementation.

Sandy answered 23/5, 2013 at 6:5 Comment(1)
I made one code, but it throws one error, that says Query is wrong. Please help me in making it correct. See edit no. 1Equidistant
H
1

I hope this will help you to execute the query:

public FolderListReturn listFolder(String parentNode, String userid,String password) {
    System.out.println("getting folders and files from = "+parentNode+" of user : "+userid);


    SessionWrapper sessions =JcrRepositoryUtils.login(userid, password);
    Session jcrsession = sessions.getSession();

    Assert.notNull(name);
    FolderListReturn folderList1 = new FolderListReturn();
    ArrayOfFolders folders = new ArrayOfFolders();
    try {
                javax.jcr.query.QueryManager queryManager;

                queryManager = jcrsession.getWorkspace().getQueryManager();
                String expression = "select * from [nt:folder] AS s WHERE ISCHILDNODE(s,'"+name+"')and CONTAINS(s.[edms:owner],'*"+userid+"*')  ORDER BY s.["+Config.EDMS_Sorting_Parameter+"] ASC";

                javax.jcr.query.Query query = queryManager.createQuery(expression, javax.jcr.query.Query.JCR_SQL2);
                javax.jcr.query.QueryResult result = query.execute();
                        for (NodeIterator nit = result.getNodes(); nit.hasNext();) {
                Node node = nit.nextNode();
                Folder folder = new Folder();
                folder=setProperties(node,folder,userid,password,jcrsession,name);
                folders.getFolderList().add(folder);
                }
        folderList1.setFolderListResult(folders);
        folderList1.setSuccess(true);

    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        //JcrRepositoryUtils.logout(sessionId);
    }
    return folderList1;
}
Hubblebubble answered 14/9, 2015 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.