如何在Apache Jackrabbit上运行查询,举例说明

时间:2013-05-22 17:33:43

标签: java database jackrabbit jcr jcr-sql2

我使用Apache Jackrabbit作为数据库。

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.

我的任务

我必须选择其键(整数值)最小的前10个节点。

我的想法

为了实现上述目标,我创建了一个查询,对所有子节点的键进行排序,并选择前10个。然后通过使用该键,我获得所有相应的节点,并在工作后删除所有键/值对。

为此我在互联网上搜索了很多如何运行查询。你能告诉我如何在apache jackrabit上运行查询吗?如果你用例子解释,这很好。

编辑否。 1

公共课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();
    }
}

}

异常

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)

我想写下一个关于scenereo

的查询

enter image description here

这里具有整数值的节点具有一些属性。我想按整数值对这些节点进行排序,并提取前50个节点以供进一步处理。

帮助我。

3 个答案:

答案 0 :(得分:3)

您应该在JCR-SQL2中引用您的节点类型名称:

select * from [nt:base]

这是JCR-SQLJCR-SQL2之间的主要区别之一。此外,name是一个dynamic operand采用选择器参数。因此,编写查询的更好方法是:

select * from [nt:base] as b where name(b) = '12345'

答案 1 :(得分:1)

根据您要使用的query language,您可以采用不同的方式执行查询。

使用this code查看仅使用API​​的简单查询,而不是像字符串查询那样的SQL。 您也可以查看JBoss Modeshape documentation示例,因为它是另一个JCR 2.0实现。

答案 2 :(得分:0)

我希望这可以帮助您执行查询:

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;
}