On this page

Run sql (like) query for tables

To run sql (like) query for tables, streams etc. we can use the following API

Method : POST
URI : /db/<dbname>/table
Body :

{
"sql":"select * from ...",
"levk":"abcd",
"sec_buf": "<opaque buffer>",
"rev_buf": "<opaque buffer>",
"switch_done": 1,
}

Note: there are more params here apart from “sql“, but they are only used for recursive query (when more data is expected to come for the same query.
BangDB returns around 4MB of data in one shot (this is configurable, but still a limited data in one call), which means if there are more data in the table for the same query then I need to call again. But in next call I would like to start from right where it stopped. This becomes complicated when the query contains filter clauses for secondary or reverse indexes. BangDB abstracts all these details and helps users with just few key/value pairs that it returns along with response. And in subsequent call, we just need to copy paste those key/value pairs along with the “sql” query and call the API again to get the next set of data. We can keep doing this until data is exhausted in the db or we don’t wish to call again.
Let’s see all the attributes here in the json body

“sql” : put your sql query here
Note: In the first call, we just need to send “sql”

If in response we see “more_data_to_come” as 1 , then it means we have more data in the table for the query and we need to call it again. This time we will copy whatever other attribute is there in the request json doc as they are (no need to copy “more_data_to_come”) along with the original sql query

Note: checkout more details on sql query supported within BangDB

Example

curl -H "Content-Type: application/json" -d '{"sql": "select * from my_table_test"}' -X POST http://192.168.1.105:18080/db/mydb/query

Response

{
"rows": [
{
"k": "user1",
"v": "{\"name\":\"sachin sinha\", \"org\":\"BangDB\", \"likes\":[\"music\",\"cricket\",\"linux\",\"coding\"],\"_pk\":\"user1\",\"_v\":1}"
},
{
"k": "user2",
"v": "{\"name\":\"Andy B\", \"org\":\"BangDB\", \"likes\":[\"books\",\"mountains\",\"system designs\"],\"_pk\":\"user2\",\"_v\":1}"
}
],
"num_items": 2,
"more_data_to_come": 0,
"switch_done": 0
}

As you see here, the response has variable “more_data_to_come” which is set as 0, which means we have retrieved all the data for the query and no need to call the API again

Table APIs : All table APIs start with /db/<dbname>/<tablename>

Was this article helpful to you? Yes No