Create a table
Table creation requires to define the properties of table hence we need to send a json doc specifying the details. Please see more about the specific of table that could be defined in this documentation.
Method : POST
URI : /db/<db_name>/table
Body:
{
"table_name": "< table name >",
"table_type": <table_type>, // WIDE_TABLE (1), NORMAL_TABLE (0) etc...
"key_type": <key_type>, // string or long (for key type)
"key_sz": <key_size_bytes>, // if key_type is string then size of key, dfault is 24 in this case
"allow_duplicate": <0 or 1>,
"allow_rev_idx": <0 or 1>,
"version_type": <0 or 1>
}
Here is some information regarding these parameters.
You don't need to provide all these info unless you are sure, otherwise default is good.
The required (must) attributes are:
- table name : alphanumeric and "_"(underscore) is allowed. Should be less than 64 bytes (ideally).
- table type :
- key type:
- allow duplicate : set it as 1 if you wish to allow duplicate PKs.
- allow reverse index : set it as 1 if you wish to enable reverse indexing.
There are more attributes that can be set, please see table-env documentation.
Example
- Let's create WIDE_TABLE , with key size 24 (key type as NORMAL_KEY) etc.
- Let's create NORMAL_TABLE now, with long key type
- There is another way, which is unique for all table types and it's using sql like statement.
curl -H "Content-Type: application/json" -d '{"table_name":"my_table_test","table_type":1,"key_type":1,"key_sz":24,"allow_duplicate":1,"allow_rev_idx":0,"version_type":1}' -X POST http://192.168.1.105:18080/db/mydb/table
Response
{
"msg": "successfully created the table"
}
To insert data into WIDE_TABLE we can use the following API:
Method : POST
URI : /db/<db_name>/<table_name>/doc
Body:
{"doc":{"name":"john"}, "_pk":"p1"} // _pk outside doc, DB will take this as _pk
{"doc":{"name":"john", "_pk":"p1"}} // _pk inside doc, DB will take this as _pk
{"doc":{"name":"john"}} // _pk is missing, DB will create one (timestamp in microsec)
curl -H "Content-Type: application/json" -d '{"table_name":"my_table_test2","table_type":0,"key_type":10,"allow_duplicate":1,"allow_rev_idx":0,"version_type":1}' -X POST http://192.168.1.105:18080/db/mydb/table
To insert data into NORMAL_TABLE we can use following:
Method : POST
URI : /db/<db_name>/<table_name>/doc
Body:
{"doc":"this is a random opaque value", "_pk":1}
Method : POST
URI : /db/<db_name>/query
Body:
{"sql":"insert statement ..."}
To insert into WIDE_TABLE, we can use following as body
{"sql" : "insert into my_table_test values "p1" {"name":"john"}"}
To insert into NORMAL_TABLE
{"sql" : "insert into my_table_test2 values 1 " this is a random opaque value ""}
Response
{
"retval": 0
}
This returns 0 or positive number for success else negative value for error.
Method : POST
URI : /db/<db_name>/query
Body:
{"sql":"insert statement ..."}
Example
{"sql" : "select * from my_table_test"}
{"sql" : "select * from my_table_test2"}
etc.