JavaScript Developer Guide
Step-by-step instructions on writing JavaScript applications with Kinetica
Step-by-step instructions on writing JavaScript applications with Kinetica
The following guide provides step-by-step instructions to get started writing JavaScript applications using Kinetica. This guide demonstrates only a small set of the available API. A detailed description of the complete interface is available under JavaScript API Reference.
The source code for the JavaScript API is available for download from the GitHub repository kineticadb/kinetica-api-javascript. Follow the instructions in the included README file to use the API library.
To connect to the database, instantiate an object of the GPUdb class, providing the connection URL, including host & port of the database server as well as an optional username & password for secure connections:
|
|
Before any data can be loaded into the system, a Type needs to be defined in the system. The type definition is a JSON string describing the fields (i.e. columns) of the type along with a name for the type. Each field consists of a name and a data type:
|
|
The returned object from the create_type() call contains a unique type identifier allocated by the system. This identifier can then be used in the request to create a new table as follows:
|
|
Once the table is created, data can be inserted as follows:
|
|
Once the table is populated with data, the data can be retrieved from the system by a call to get_records() as shown below:
|
|
For large tables, the data can be easily be retrieved in smaller blocks by using the offset and limit parameters. The returned response also contains the schema (or data type) of the results.
To filter a subset of the records, use the filter() method with an expression as follows:
|
|
To filter all the records matching a known list of values:
|
|
To retrieve a list of all the unique values for a column or a set of columns or expression:
|
|
Kinetica supports various group-by queries. To group by one or more columns and return the count of the unique values, use the aggregate_group_by() method as shown below. The next query shows how to get the count of records, as well as the sum and average of the values of column col1, within each group:
|
|
|
|
Kinetica supports grouping numerical data into a histogram. The input range is divided into equal sized bins and the count of objects in each bin are returned:
|
|
Included below is a complete example containing all the above requests, the data file, and output.
To run the complete sample, ensure the GPUdb.js
& tutorial.js
references in the tutorial.html
file point to the correct location of
the Kinetica Javascript API library & tutorial script files, respectively--(the
default assumes all three files are co-located in a browser-accessible
location). Then, open the tutorial.html
file in a browser, passing the
Kinetica URL, as well as optional username/password credentials for the
server as URL parameters:
|
|
Note
As this script creates a schema and several database objects within it, system admin permission is required to run it.