Skip to content

Using the kdb Insights Python interface

The Python interface allows you to publish data to RT in your own Python application.

The Python langauge interface is a Python wrapper of the C interface to RT

This section details how to get started with the Python interface.

Downloading the Python interface

You can download the Python interface from the kdb Insights Nexus registry.

Supported Operating Systems

The Python interface is supported on the following operating systems:

  • CentOS 8 or later
  • Red Hat Enterprise Linux (RHEL) 8 or later
  • Ubuntu 18.04 or later
  • Windows

Note

The interface is only supported on x86 architectures. We currently do not support macOS.

Prerequisites

The Python interface requires the following system packages:

  • libssl
  • libcurl

Installation

Install the Python interface via pip.

  1. Ensure you have the latest version of pip:

    pip install --upgrade pip
    
  2. Install the package:

    $ pip install --extra-index-url https://nexus.dl.kx.com/repository/kxi-pypi-public/simple/ kxi-rtpy
    

    The interface uses PyKX

    The pip install command will also install PyKX if it is not already present.

Using the Python interface in your own application

To publish data to kdb Insights Enterprise using the Python interface as part of your own application, use the rtpy module. This module contains the functionality to connect to kdb Insights Enterprise and publish the data into the system.

Authentication and Connectivity

kdb Insights Enterprise provides an Information Service which returns a client URL (KXI_CONFIG_URL). This URL is used to authenticate to the kdb Insights Enterprise instance. If you are not using kdb Insights Enterprise you can populate a local JSON file with the connectivity details. Further information on this is available in the getting started guide.

Initializing

  1. Start the Python interpreter and import the rtpy module:

    from rtpy import Publisher, RTParams
    
  2. Create an RTParams configuration object:

    params = RTParams(configUrl='file:///tmp/rt_config.json', rtDir='/tmp/tmprt')
    
    See the Parameters section for the full list of configuration parameters.

    configUrl

    The configUrl can be either a kdb Insights Enterprise client URL to a local JSON file (using the file URI scheme) containing the RT configuration.

Parameters

The RTParams class contains the following RT connection configuration parameters.

parameter required description
configUrl Yes The URL/file that is called/read to get the RT endpoint(s)
rtDir Yes RT directory
queryTimeout No Milliseconds to wait for the connection to be established
fetchConfigSleep No Time in ms to sleep between reads of the configuration details
configMaxAge No Maximum age of configuration in milliseconds
localPersistencePeriod No Local persistence period in milliseconds
logLevel No Log level: info, warn, err, or off
consoleLogLevel No Console log level: info, warn, err, or off
CAInfoFile No Path to Certificate Authority certificate bundle
dedupId No ID to dedup multiple publishers

See the C interface documentation for the list of default values.

Publishing data

The Publisher class implements the Python context manager protocol. The functions to connect and disconnect from RT are called implicitly when entering and exiting the with block below.

  1. Create some data and publish it to RT:
    from rtpy import Publisher, RTParams
    import pykx as kx
    from datetime import *
    import uuid
    
    params = RTParams(configUrl='file:///tmp/client.json', rtDir='/tmp/tmprt')
    
    with Publisher(params) as pub:
    
        now = datetime.utcnow()
        trade_id = uuid.uuid4()
    
        # Send a Python list
        data = ["hello", 1234, 3.14, now]
        pub("test", data)
    
        # A kdb+ license is required to use the PyKX Table object
        data = kx.Table([[now, "VOD", "LSE", "buy", 75.90, 100, trade_id]], columns=['time','sym','exch','side','price','size','tradeID'])
    
        pub("trade", data)
    

Next Steps

Learn how to use our sample Python program. to publish data.