RESTful API

Representational State Transfer (REST) is a Software Architecture style consisting of guidelines and best practices for creating scalable web services.

Here are 4 methods, that are defined in HTTP protocol correspond to CRUD:

HTTP Method Description
POST Create
GET Select
PUT Update
DELETE Delete

Machbase is not standard REST API. Rather it is RESTful API that processes CRUD by using POST and GET methods. In other words, it uses POST method for data insertion and GET method for rest of operations do the all the work.

Table of Contents

Sample Test

A few sample codes are provided in sample directory and you can check how it works.

File Description

File Name Description
curl1_create.sh create table and get column information.
curl2_insert.sh insert JSON data into the Machbase.
curl3_select.sh retrieve data from the Machbase.
curl4_tables.sh get the list of tables.
curl5_columns.sh get a column information with specified table.

Go to rest sample directory, run each sample.

# go to rest sample directory.
[mach@localhost]$ cd $MACHBASE_HOME/sample/rest

# run curl1_create.sh
[mach@localhost]$ sh curl1_create.sh

# run curl2_insert.sh
[mach@localhost]$ sh curl2_insert.sh

# run curl3_select.sh
[mach@localhost]$ sh curl3_select.sh

# run curl4_tables.sh
[mach@localhost]$ sh curl4_tables.sh

# run curl5_columns.sh
[mach@localhost]$ sh curl5_columns.sh

Setting Up Environments

Execute machbase web analytics by using "$MACHBASE_HOME/bin/MWAserver start" first, and then it allows you to access Machbase server by using RESTful API .
Please refer to machadmin for how to set and execute on the server.

[mach@localhost]$ cd $MACHBASE_HOME/bin
[mach@localhost]$ MWAserver start

Use Machbase

Machbase RESTful API is interlocked with the server configuration of machbase web analytics. Hence users can use the same value if there is a value of server being used. If you are using GET method, it easily changes the values.

Basic URL

The basic URL of RESTful API is as follows.
http://hostname:port/machbase
default port value is 5001.

For localhost, you can access the server with the address below when install it for the first time.
http://127.0.0.1:5001/machbase

Data Extraction

It brings data by using HTTP GET method. The return value is JSON type. Set the parameter name as "q", and make the query string as value.

The query below brings the list of tables.

curl -G "http://127.0.0.1:5001/machbase" --data-urlencode 'q=select * from m$sys_tables'

Data Entry

Send the input value of JSON type as parameter by using HTTP GET method.
The following is an example of inputting data to a test_table with three columns.

curl -X GET "http://127.0.0.1:5001/machbase" --data-urlencode 'q=create table test_table(c1 short, c2 integer, c3 varchar(20))'

To input data, execute "curl" command on the linux command prompt by using POST method.

curl -X POST -H "Content-Type: application/json" "http://127.0.0.1:5001/machbase" -d '{"name":"test_table", "values":[[1,2,"aaa"],[3,4,"bbb"]]}'

When you input datetime type, it also needs to specify datetime format.

The pattern of Machbase should be used for datetime format. The default format of Machbase, "YYYY-MM-DD HH24:MI:SS mmm:uuu:nnn" is used if none is specified. If datetime format is not matched, it may not return the results properly.

Be noted that it is case sensitive.

curl -X GET "http://127.0.0.1:5001/machbase" --data-urlencode 'q=create table test_date(c1 datetime)'

curl -X POST -H "Content-Type: application/json" "http://127.0.0.1:5001/machbase" -d '{"name":"test_date",  "date_format":"YYYY-MM-DD HH24:MI:SS", "values":[["2015-02-01 12:13:14"],["2015-02-11 07:08:09"]]}'

Data Deletion

The standard REST API has to use HTTP DELETE method, but Machbase uses GET method to execute query for deletion. To delete the test_table:

curl -G "http://127.0.0.1:5001/machbase" --data-urlencode 'q=delete from test_table except 1 rows'

Creation, Modification, and Deletion of Table

Machbase can execute any query by using the HTTP GET method. Therefore, CREATE, ALTER and DROP commands are also available.

curl -G "http://127.0.0.1:5001/machbase" --data-urlencode 'q=drop table test_table'

results matching ""

    No results matching ""