DEV Community

Franz Wong
Franz Wong

Posted on • Updated on

KDB cookbook

I am still a KDB noob. This page will be kept updating whenever I have new tricks.

Open a file where file name is in variable

filePath:"/tmp/test.txt"
file:hopen hsym[`$filePath]

Delete a file

hdel `:/home/franz/test.txt

Check if a file exists

not () ~ key hsym `:/home/franz/test.txt

Delete a file only if it exists

if[not () ~ key hsym `:/home/franz/test.txt; hdel `:/home/franz/test.txt]

Define a table

table1:([] c1:1 2; c2:`abc`def; c3:123 456)

Define a single row table

table1:([] enlist c1:1; c2:`abc; c3:123)

Write a single string to file (Not append)

`:/home/franz/test.txt 0: "Hello world"

Export a table to csv

File name is same as the table name.

save `$"/home/franz/table1.csv"

Export a table to csv with a different file name

`:/home/franz/test.txt 0: csv 0: table1

Load csv file to table

"jsj" is the data type of each column in the csv. For example, j is long and s is symbol.

table3:("jsj";enlist",")0:`$"/home/franz/table1.csv"

try-catch

Execution Control

h:@[hopen;`::9999;{show "Unable to connect"; exit 1}]

Early return

Use : without anything on the left

:"Hello World"

Top comments (0)