Get-by-key queries
Run a simple get-by-key query
The get-by-key query retrieves the record corresponding to a key.For example, given a table
trades where rows are indexed by their trade id, the following retrieves the trader that
made the trade 1023:
year, month, and day), key values can be passed either as variadic
arguments or as a list:
IDataTable#getKeyFieldNames(). Key values should be passed
to withKey() in this exact order.
Compile once, run twice
If a query will be run several times, it is better to compile it once:Use a get-by-key query
The get-by-key query returns the result as anIRecordReader.
For example, the following service retrieves the trader name for a trade id:
Get-by-key and DirectQuery
While Get-by-key queries performed on the datastore are very fast, they can take a long time on external databases, solely due to the networking time. As running too many get-by-key queries through DirectQuery can be a performance issue, there is a setting to control whether this is allowed. The possible behaviors are denying, warning, or allowing the query to run, and the default is to warn. For standard size tables, it is recommended to load this table into an in-memory datastore table, as mentioned in the composite database documentation. Get-by-key queries would then be directed to the in-memory table, instead of the external database. Configure this behavior in the database settings when creating the application.- BigQuery
- ClickHouse
- Databricks
- Generic JDBC
- MS SQL
- Redshift
- Snowflake
- Synapse
List queries
Minimal list query
The minimal list query requires a table name and retrieves all records:Add a condition
The list query can be filtered using a condition builder. The following retrieves the trades made by John:Dynamic conditions
The condition can be parameterized at query compilation. The parameter values are specified at query execution. The following retrieves the trades made by Paul with that mechanism:Select fields
If not all fields are useful, the fields returned by the query can be selected. There are three ways of doing so.Aliased fields
This is the generic approach. Fields returned by the query can be aliased, like in SQL queries:All fields belong to the queried table
If all the fields come from the queried table, there is a shorter method that will use the field name as an alias:Fields without an alias
If only the field position in the results matters, and not the alias, the following is also possible:Add a timeout or a result limit
For performance reasons, the query can be restricted by adding a result limit and/or a timeout. The timeout will cancel the query if reached and an exception will be thrown. The result limit sets the maximum number of rows that will be returned. Note that the rows returned when using a result limit are not deterministic: successive executions of the same query may return different rows, even if no transaction occurred in the database.Add query tags
Each query can have a map of tags to help monitor the application. For example, the following adds the name of the operation:Use a list query
The list query returns anICursor that can be used to manipulate every record.
It is important to read through the ICursor within a try-with-resources statement to ensure that all related resources
are freed at the end of the manipulation.
For example, the following retrieves the sum of “value” by date:
Run with an acceptor
By default, calling.run() blocks the calling thread until the full result is available as a single ICursor.
An acceptor provides a way to process results as they are produced, partition by partition, without waiting for the
entire result set.
This is useful when:
- The result processing should happen in parallel.
- Fine-grained control over how partition results are merged or consumed is needed.
IPartitionedResultAcceptor. Its onResult(int partitionId, ICursor result) method is called
once per partition as soon as that partition’s results are ready.
run() method returns an ICompletionSync that can be used to wait for the query to finish.
Distinct queries
The distinct query retrieves all distinct values for a field or a field combination from a record list. The record list can be filtered with specified conditions. Its characteristics are very similar to list query ones. It returns anICursor, and provides the following options:
- Field aliasing
- Limit
- Timeout
- Query tags