How to run a request on Apache Jackrabbit, explain an 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, ie, some Integer. Each child Node have some properties that I have used further. 

My task

I need to take the top 10 nodes whose keys (integer values) are minimal.

My thinking

To achieve the above goal, I make a request that sorts the keys of all the child nodes and selects the top 10. Then, using these keys, I get all the corresponding nodes, and after work I delete all the key / value pairs.

To do this, I searched a lot on the Internet how to run a query. Could you tell me how to run a request on apache jackrabit. Well, if you explain with an example.

Change 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 request below scenereo

enter image description here

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

Help me with this.

+4
source share
3 answers

You must specify your node type name in JCR-SQL2:

 select * from [nt:base] 

This is one of the main differences between JCR-SQL and JCR -SQL2 . In addition, name is a dynamic operand using a selector argument. Therefore, the best way to write your request is:

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

You have different ways to fulfill 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, not SQL, like string queries. You can take a look at the JBoss Modeshape documentation for examples, as this is another implementation of JCR 2.0.

+1
source

Hope this helps you complete 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; } 
0
source

Source: https://habr.com/ru/post/1482220/


All Articles