My database has one table: Country {Name, Capital, Population, Area}, and I have around 200 entries.
How can i create a GET request which return 20 (or some other constant) random countries?
I can figure out how to return random sets.
Thanks.
My database has one table: Country {Name, Capital, Population, Area}, and I have around 200 entries.
How can i create a GET request which return 20 (or some other constant) random countries?
I can figure out how to return random sets.
Thanks.
Hello, Daniel!
There is no straight way to get a random set of data objects for now. You can implement this function through custom code. For example: hold somewhere total number of objects in target table. When you need to retrieve random row - make GET request with pagesize 0 and offset in range from 1 to totalObjects -1.
Also you can retrieve collection of objects with pagesize 100 ( maximum allowed pagesize ) and choose randomly as many objects as you need. In first case you’ll have to make as much requests as many objects you need. In the second - you’ll be limited by 100 objects with random offset.
Here is one more way - more complicated. You can create a column ‘identifier’ with integer type hold there a number for each row, and dynamically change numeration while working with the table. The purpose is to keep all objects numerated one by one all the time. After this you’ll be able to retrieve as many random objects as you need using where clause like ‘identifier in (1, 5, 23, …)’, where numbers are generated randomly.
best regards,
Alex
Im not sure if I understood what do you propose.
How would URL look like?
https://api.backendless.com/v1/data/Countries?offset=1 (??)
Lets assume i know exactly ho many values do I have on my table, lets assume I do have 80 entries, and i need a random set with 20 values.
Can you detail a little more your idea? Thank you
Daniel,
With the first approach proposed by Alex, you’d be getting a block of records, say X starting at a random offset. So in that case, the only random element is the offset from where to load X records. The request would look exactly as you showed:
https://api.backendless.com/v1/data/Countries?offset=<randomnumber>
The second approach would get you closer to where you want to be. Suppose you have an INT column in your table which contains a value you increment every time a new object is saved (this could actually be accomplished with custom business logic which injects the column value). Suppose the column name is “recordNumber”. In order to retrieve X records, you identify X random numbers between 0 and “the number of objects minus 1”, say they are 3, 7, 9, 15, 33 and run the following query (make sure the query is URL encoded):
https://api.backendless.com/v1/data/Countries?where="recordNumber in (3,7,9,15,33)"
Hope this helps.
Regards,
Mark
Daniel, I’m sorry - I’ve made a mistake while describing my first approach - I meant making request with pagesize 1 but not 0. The request would look like this:
GET http://api.backendless.com/v1/data/Countries?pagesize=1&offset=<random_number[0…total-1]>
In response you’ll get collection of “Countries” with one object inside.
About second approach: the ‘recordNumber’ column values should be updated if any object is deleted. For example, if you have 3 objects in table with numbers 1, 2, 3, and you delete object with number 2 - you should change recordNumber for object with number 3 to 2. This way after deletion you’ll have two object numbered 1 and 2. This algorithm should be implemented for all deletion cases excluding the last object deletion - in this case object just should be removed without any additional logic.
best regards,
Alex