Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run query on Apache Jackrabbit, explain with example

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.

like image 597
devsda Avatar asked May 22 '13 17:05

devsda


People also ask

How do I start Apache Jackrabbit?

Running the standalone server You need Java version 5 or higher to run the Jackrabbit standalone server. To start the server, double-click on the standalone jar file, or invoke it on the command line. Welcome to Apache Jackrabbit!

What is Apache Jackrabbit used for?

Apache Jackrabbit is an implementation of a new special kind of a database called a content repository. Defined in Java Specification Requests (JSRs) 170 and 283, a content repository is a hierarchically organized storage engine that combines features from advanced file systems and relational databases.

Is Apache Jackrabbit a database?

Apache Jackrabbit is an open source content repository for the Java platform. The Jackrabbit project was started on August 28, 2004, when Day Software licensed an initial implementation of the Java Content Repository API (JCR).

What is SQL2?

The JCR-SQL2 query language is defined by the JCR 2.0 specification as a way to express queries using strings that are similar to SQL. This query language is an improvement over the earlier JCR-SQL language, providing among other things far richer specifications of joins and criteria.


1 Answers

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'
like image 77
Lukas Eder Avatar answered Sep 29 '22 22:09

Lukas Eder