Search code examples
kdb

How can I evaluate a Q script in a running KDB server?


I have a large table that I want to partition loaded in a kdb+ server. The partitioning code is in a Q script that I need to evaluate on the server, but I don't have I/O control over it (the server is running in the background, data in memory was loaded through the C API).

So far the only solution I've found is to open a handle and convert all my statements to strings (all those \" I have to use look quite messy):

if[4 <> count .z.x; 0N!"usage: part_md.q host port db_dir date (YYYY.MM.DD)"; exit[1]]

arg_host: .z.x 0
arg_port: .z.x 1
arg_db_dir: .z.x 2
arg_date: "D"$(.z.x 3)

/get server handle
h:hopen `$(":",arg_host,":",arg_port)
set_db: "db: \":",arg_db_dir, "/mydb/\""
set_sym_path: "sym_path: \":",arg_db_dir,"\""
h set_db
h set_sym_path

/select fields to partition
h "mydb_select: select [-10] A,B,C from mydb"

/enumerate symbols
h "md_select_enum: .Q.en[`$sym_path] md_select"
h "delete md_select from `."
...

How can I evaluate my script directly without using a handle to the server on the same machine?


Solution

  • You could use a kdb IDE like qStudio http://www.timestored.com/qstudio to send the query by entering commands exactly as you would as if at the console.

    Few other suggestions:

    1. Read up on the function form of inter process of communication, an alternative to always sending strings. More powerful as it allows using objects.

    2. Avoid underscores in variable names as they are also operators in kdb. (See here for q language guidelines: http://www.timestored.com/kdb-guides/q-coding-standards )

    3. Checkout the many variations of the sv function, particularly (` sv `:file,`name) it allows quickly forming file paths in a platform independent way.

    Regards, Ryan