kx

Connect to Kx Systems, Inc. kdb+ databases

Description

The kx function creates a kx object. The kx object represents a Kx Systems®, Inc. kdb+ database connection.

After you create a kx object, you can use the object functions to run Kx Systems, Inc. kdb+ commands, retrieve data from the Kx Systems, Inc. kdb+ database, and write data back to the database.

Before you connect to the database, add the Kx Systems, Inc. file jdbc.jar to the MATLAB® Java® class path by using the javaaddpath command. The following code adds the JAR file to the MATLAB Java class path. This code assumes that the full path of the JAR file is c:\q\java\jdbc.jar.

javaaddpath c:\q\java\jdbc.jar

Alternatively, add the JAR file to the static Java class path. For details about dynamic and static class paths, see Java Class Path.

Note

Earlier versions of the Kx Systems, Inc. kdb+ database provide this JAR file with the name kx.jar. If you are running an earlier version of the database, rename kx.jar as jdbc.jar to add this file to the MATLAB Java class path.

Creation

Description

example

c = kx(ipaddress,port) connects to a Kx Systems, Inc. kdb+ database and sets the ipaddress and port properties.

example

c = kx(ipaddress,port,customerid) uses a customer identifier for the database connection.

Input Arguments

expand all

Customer identifier, specified as a character vector or string scalar. The customer identifier consists of a user name and password separated by a colon, such as 'username:password'.

Data Types: char | string

Properties

expand all

Handle, specified as a Kx Systems, Inc. kdb+ database handle object. For details, contact Kx Systems, Inc.

Example: [1×1 c]

Data Types: double

IP address of the machine where the kdb+ database is located, specified as a character vector or string scalar.

Example: 'localhost'

Data Types: char | string

Port number of the machine where the kdb+ database is located, specified as a numeric scalar.

Example: 5001

Data Types: double

Object Functions

isconnectionDetermine if connections to Kx Systems, Inc. kdb+ databases are valid
closeClose connections to Kx Systems, Inc. kdb+ databases
getRetrieve Kx Systems, Inc. kdb+ connection object properties
execRun Kx Systems, Inc. kdb+ commands
fetchRequest data from Kx Systems, Inc. kdb+ databases
tablesRetrieve table names from Kx Systems, Inc. kdb+ databases
insertWrite data to Kx Systems, Inc. kdb+ databases

Examples

collapse all

Create a kdb+ database connection. Then, retrieve data from the database.

Run this command at the DOS command prompt.

q tradedata.q -p 5001

Connect to a kdb+ database using an IP address and port number. c is a kx object.

ipaddress = 'localhost';
port = 5001;
c = kx(ipaddress,port)
c = 

  kx with properties:

       handle: [1×1 c]
    ipaddress: 'localhost'
         port: 5001

Retrieve data from the kdb+ database.

ksql = 'select from trade';
d = fetch(c,ksql)
d =           
         sec: {5000x1 cell}
       price: [5000x1 double]
      volume: [5000x1 int32]
    exchange: [5000x1 double]
        date: [5000x1 double]

d is a structure that contains these fields:

  • Security

  • Price

  • Volume

  • Exchange

  • Date

Close the kdb+ database connection.

close(c)

Create a kdb+ database connection using the customer identifier. Then, retrieve data from the database.

Run this command at the DOS command prompt.

q tradedata.q -p 5001

Connect to a kdb+ database using an IP address, port number, and customer identifier. The customer identifier consists of a character vector that contains the user name and password separated by a colon. c is a kx object.

ipaddress = 'localhost';
port = 5001;
customerid = 'username:password';
c = kx(ipaddress,port,customerid)
c = 

  kx with properties:

       handle: [1×1 c]
    ipaddress: 'localhost'
         port: 5001

Retrieve data from the kdb+ database.

ksql = 'select from trade';
d = fetch(c,ksql)
d =           
         sec: {5000x1 cell}
       price: [5000x1 double]
      volume: [5000x1 int32]
    exchange: [5000x1 double]
        date: [5000x1 double]

d is a structure that contains these fields:

  • Security

  • Price

  • Volume

  • Exchange

  • Date

Close the kdb+ database connection.

close(c)
Introduced in R2007a