Python Querying API.¶
The following documentation defines a Python API to be used when querying the kdb Insights and kdb Insights Enterprise Offerings.
Classes:
- Query – Python API for querying kdb Insights and kdb Insights Enterprise.
Query¶
Bases: ApiClient
Python API for querying kdb Insights and kdb Insights Enterprise.
See Connection Parameters for shared constructor arguments.
Attributes:
- udas (
'UdaNamespace') – A namespace of UDAs (User-Defined Analytics) available on the connected instance. See: UDA (User-Defined Analytic) object model for kdb Insights for details.
Functions:
- fetch_custom_apis – Query the database for custom APIs and attach them as additional attributes.
- get_data – Run a getData.
- get_meta – Run a getMeta.
- get_table_schemas – Get table schemas in the format of {"table": {"col": pykxtype}}.
- ping – Run a ping.
- preview – Run a preview.
- qsql – Run a qSQL.
- refresh_udas – Re-fetch UDAs from getMeta and return the updated namespace.
- sql – Run an SQL.
fetch_custom_apis¶
fetch_custom_apis()
Query the database for custom APIs and attach them as additional attributes.
get_data¶
get_data(table, start_time=lambda: datetime.now(tz=(timezone.utc)) - timedelta(minutes=15), end_time=lambda: datetime.now(tz=(timezone.utc)), *, input_timezone=None, output_timezone=None, filter=None, group_by=None, aggregations=None, fill=None, temporality=None, slice=None, sort_columns=None, labels=None, **kwargs)
Run a getData.
API call on kdb Insights Enterprise.
Parameters:
- table (
str) – The name of the table from which to retrieve data - start_time (
str | datetime | None) – The earliest time data is to be queried from, by default this is 15 minutes prior to the time that the get_data call is made. - end_time (
str | datetime | None) – The latest time data is to be queried from, by default this is the time that the get_data function is called. - input_timezone (
Optional[str]) – The timezones of start_time and end_time, default is UTC if not specified. - output_timezone (
Optional[str]) – The timezone of output timestamp columns, default is UTC if not specified. - filter (
Optional[List[list]]) – A list of triadic Python lists defining filter conditions in the format[['function', 'column name', 'parameter'], ... ] - group_by (
Optional[str]) – A list of Pythonstritems defining the columns to group aggregations by - aggregations (
Optional[List[list]]) – A list of Pythonstritems or a list of lists ofstritems defining. The columns to be returned from the table across DAPs if defined as a list ofstr. If provided as a list of triadic Python lists as follows[['assignName', 'agg', 'column'], ...]thenassignNamedefines the name to be assigned to the return of the aggregation,aggdefines the aggregation function to be used andcolumndefines the column on which that aggregation is applied. - fill (
Optional[str]) – This defines how to handle the presence of null values. This should be supplied as a singlestritem with valueforwardto define the use of the last non-null value in a column orzeroto replace all null values with the 0 value of that column. - temporality (
Optional[str]) – This determines the way that thestart_timeandend_timearguments are interpreted. The two support input types are'slice'and'snapshot'. - slice (
Optional[str]) – This should be defined as a Python list of 'times' which denote the time range to grab between each date in thestart_timeandend_timerange when a temporality of'slice'is used. - sort_columns (
Optional[List[str]]) – This should be a Python list ofstrtype objects denoting the columns which are to be sorted on. - labels (
Optional[Dict[str, str]]) – labels defined in the assembly file of the DAPs can be used to only select data from DAPs in assemblies matching that label. - *kwargs* – Other possible arguments.
Returns:
Table– Apykx.Tableorpykx.KeyedTablereturned from a query to kdb Insights Enterprise
Examples:
Connect to a running instance of kdb Insights and query using get_data with
default values
import kxi.query
conn = kxi.query.Query()
conn.get_data('my_table')
Query the instance providing arguments to various get_data calls.
conn.get_data('my_table', group_by = ['sensorID', 'qual'])
conn.get_data('my_table', filter = [['within', 'qual', [0, 2]]])
conn.get_data('my_table', start_time='2000.05.26',end_time='2000.05.27')
get_meta¶
get_meta()
Run a getMeta.
API call on kdb Insights Enterprise.
Returns:
Dictionary– Apykx.Dictionarycontaining metadata information about the various processes and schemas which describe a database.
Examples:
Connect to a running instance of kdb Insights and retrieve metadata information about the database, schemas and assemblies
import kxi.query
conn = kxi.query.Query()
conn.get_meta()
get_table_schemas¶
get_table_schemas(assembly=None)
Get table schemas in the format of {"table": {"col": pykxtype}}.
Parameters:
- assembly (
str | None) – Optional assembly name to filter for.
Returns:
Example
query.get_table_schemas()
{
"tableName": {
"col1": kxi.util.QType(-12, pykx.TimestampAtom, "timestamp", "p")
}
}
ping¶
ping(labels=None)
Run a ping.
This API is intended to run on a data access process and returns true if it reaches a target
Parameters:
- labels (
Optional[dict]) – labels defined in the assembly file of the DAPs can be used to only select data from DAPs in assemblies matching that label.
Returns:
BooleanVector– A list of booleans.
preview¶
preview(table, start_time=None, end_time=None, limit=None)
Run a preview.
The preview API is a lightweight data retrieval API which is able to fetch small samples of a table using minimal time and resources, for you to review or to use in testing software compatibility with a schema.
Parameters:
- table (
str) – The name of the table from which to retrieve data - start_time (
str | datetime | None) – The earliest time data is to be queried from, by default this is 15 minutes prior to the time that the get_data call is made. - end_time (
str | datetime | None) – The latest time data is to be queried from, by default this is the time that the get_data function is called. - limit (
int | None) – The maximum number of rows to return.
qsql¶
qsql(query, assembly, target, use_qe=False)
Run a qSQL.
API call on kdb Insights Enterprise.
Parameters:
- query (
str) – The SQL query which is to be run against the query environment. - assembly (
str) – The Assembly name to query, must support query environment. - target (
str) – The DAP instance to query. - use_qe (
bool) – Indicate whether to use the QE or PROD environment.
Returns:
K– Apykx.Kreturned from a query to kdb Insights Enterprise
Examples:
Connect to a running instance of kdb Insights and query using qsql
import kxi.query
conn = kxi.query.Query()
conn.qsql('my_table', "assembly-qe", "rdb")
refresh_udas¶
refresh_udas()
Re-fetch UDAs from getMeta and return the updated namespace.
Call this if the deployment has changed since the last access.
Returns:
'UdaNamespace'– The refreshedUdaNamespace.
Example
conn.refresh_udas()
conn.udas.example.daAPI(table="trade", column="price")
sql¶
sql(query, use_qe=True, **kwargs)
Run an SQL.
API call on kdb Insights Enterprise.
Parameters:
- query (
str) – The SQL query which is to be run against the query environment - use_qe (
bool) – Indicate whether to use the QE or PROD environment. - *kwargs* – Additional parameters.
Returns:
Table– Apykx.Tableorpykx.KeyedTablereturned from a query to kdb Insights Enterprise
Examples:
Connect to a running instance of kdb Insights and query using sql
import kxi.query
conn = kxi.query.Query()
conn.sql('SELECT * FROM my_table')
udas¶
udas: 'UdaNamespace'
Lazy UDA namespace — tab-complete across q namespaces.