Java Developer Manual¶
The following guide provides step-by-step instructions to get started writing Java applications using Kinetica. This guide demonstrates only a small set of the available API. A detailed description of the complete interface is available under Java API Reference.
- Prerequisites
- Data File
- API Download
- Maven Download
- Manual Download
- File Hierarchy
- Connecting to the Database
- Creating a Type
- Creating a Table
- Inserting Data
- Key/Value Record
- In-Line Record
- CSV
- Retrieving Data
- Updating and Removing Records
- Alter Table
- Indexes
- Compression
- Dictionary Encoding
- Filters
- Filter Example 1
- Filter Example 2
- Filter Example 3
- Filter Example 4
- Aggregates
- Aggregate Example 1
- Aggregate Example 2
- Aggregate Example 3
- Aggregate Example 4
- Joins
- Projections
- Union, Intersect, and Except
- Deleting Records
- Download & Run
Prerequisites¶
Data File¶
The tutorial java file makes reference to a data file in the current directory. This path can be updated to point to a valid path on the host where the file will be located, or the script can be run with the data file in the current directory.
API Download¶
Maven Download¶
We suggest using Maven as the build tool for your Java project. To use the Kinetica Java API, you must add our Nexus repository and the Kinetica Java API dependency that matches that of the targeted Kinetica database, as shown below:
Important
The pom.xml file used for the tutorial can be found below
Manual Download¶
The source code for the Java API is also available for download from the GitHub repository kineticadb/kinetica-api-java. Follow the instructions in the included README file to build the API library.
File Hierarchy¶
The tutorial was setup like the below diagram:
Connecting to the Database¶
To connect to the database, instantiate an object of the GPUdb class, providing the connection URL, including host & port of the database server:
Creating a Type¶
Before any data can be loaded into the system, a Type needs to be defined in the system. The type is a class, extended from RecordObject, using annotations to describe which class instance variables are fields [i.e. columns], what type they are, and any special handling they should receive. Each field consists of a name and a data type:
Note
Although a constructor is not required, if the class does have any constructors, it must have a constructor with no parameters. Other constructors can be added, as necessary
Next, the types need to be created:
Creating a Table¶
The returned object from the createType[] call contains a unique type identifier allocated by the system. This identifier can then be used in the request to create a new table. The examples below outline creating a table with either an options map [Vendor] or options object [Payment]:
Inserting Data¶
Once the table is created, data can be inserted into it. There is a convenience class called BulkInserter, which facilitates inserting records into a table in batches, documented under Multi-Head Ingest. For this tutorial, only the native Java API call insertRecords[] will be shown.
Key/Value Record¶
In-Line Record¶
Important
Additional records are inserted at this point, which can be found in the full Tutorial.java file below
CSV¶
This example requires the util and io libraries but allows for importing a large amount of records with ease. After setting up a Scanner and File instance, you can loop over all values in a .csv file, append the values to lists of a list, then insert the list.
Retrieving Data¶
Once the table is populated with data, the data can be retrieved from the system by a call to getRecords[tableName, offset, limit, options] using in-line parameter-passing.
One can also invoke getRecords[request] using the GetRecordsRequest request class. This object contains all the parameters for the method call, and can be reused in successive calls, avoiding re-specifying query parameters.
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.
Also, note that all query related methods have the above two versions--with the request object and with the parameters passed directly to the method.
Updating and Removing Records¶
Use updateRecords[] to update matching key values for all records in a table.
Use deleteRecords[] to delete records from a table. A list can be used to specify which records delete based on matching expressions.
Alter Table¶
Some properties can be altered or added after table creation, including indexes, dictionary encoding, and compression.
Indexes¶
Using the alterTable method, you can create indexes on columns using the create_index action paired with a column name.
Compression¶
Applying column compression works similarly: using the alter_table method but with a set_column_compression action paired with a column name and compression type option.
Important
Column compression is applied at a fixed interval, so be sure to verify later that the compression has been added. Column usage should decrease by roughly 23% [~1989 bytes]
Dictionary Encoding¶
Applying dictionary encoding via alter_table involves adding a new property to a column.
Important
To add a new property, all existing column properties must be listed along with any new property
Filters¶
Filters are an easy way to reduce larger table into more concise views using expressions.
Filter Example 1¶
Filter Example 2¶
Filter Example 3¶
Filter Example 4¶
Aggregates¶
Kinetica supports various aggregate and group-by queries, which group and aggregate your data to return counts and useful statistics.