Skip to content

File system

Kdb+ communicates with the filesystem through

  • one-shot operations
  • handles to persistent connections

Handles are more efficient for multiple operations on a file.

File paths are displayed separated with forward slashes, regardless of the operating system.

One-shot operations

get set read/write or memory-map a data file¹ value read a data file¹

hcount file size hdel delete a file or folder hsym symbol/s to file symbol/s¹

0: File Text read/write chars¹ read0 read chars¹ 1: File Binary read/write bytes¹ read1 read bytes¹ 2: Dynamic Load load shared object

save load a variable rsave rload a splayed table dsave tables ? Enum Extend

¹ Has application beyond the file system.

Setting and getting

Keywords set and get let you treat files as variables that persist in the filesystem.

q)`:data/foo`:data/bar set'(42;"thin white duke")
`:data/foo`:data/bar
q)get `:data/foo
42
q)get `:data/bar
"thin white duke"

File utilities

hcount file size hdel delete a file or folder hsym symbol/s to file symbol/s

Writing and reading

Any file can be read or written as bytes (binary). Text-file primitives handle text files.

0 associates with text; 1 with bytes.

The File Text operator 0: can also represent a table as strings, and interpret key-value pairs.

Tables

save load a table rsave rload a splayed table dsave tables ? Enum Extend

Kdb+ uses files and directories to persist database tables. Partitioning a table divides its rows across multiple directories. Splaying a table stores each column as a separate file.

Connections

A persistent connection enables multiple operations on a file without repeatedly opening and closing it.

Opening a connection to a file returns a handle to the connection. The handle takes the form of an int that is also an applicable value.

System handles 0, 1, and 2 are to the console, stdout, and stderr. They are always open.

0 console hopen open a file¹ 1 stdout hclose close a file¹ 2 stderr

Opening a connection to a non-existent file creates it and any missing ancestor directories.

Applying the handle to data appends it to the file as bytes. Applying the neg of the handle to char data appends it as text. The result of a successful operation is the positive or negative handle.

Text

q)key `:foo/                            / does not exist
q)show h:hopen `:foo/bar.txt
12i
q)key `:foo/                            / file and dir created
,`bar.txt

q)neg[h] "hear the lark and hearken"
-12i
q)-12i "to the barking of the dog fox"
-12i
q)neg[h] "gone to ground"
-12i

q)hclose h
q)hcount `:foo/bar.txt
71
q)read0 `:foo/bar.txt
"hear the lark and hearken"
"to the barking of the dog fox"
"gone to ground"

q)read0 (`:foo/bar.txt;10;20)
"ark and hearken"
"to t"

Bytes

q)hopen ":foo/hello.dat"
7i
q)7i 0x68656c6c6f776f726c64
7i
q)hclose 7i
q)read1 `:foo/hello.dat
0x68656c6c6f776f726c64

Relative filepaths

Relative filepaths are sought in the following locations, in order.

  1. current directory
  2. QHOME
  3. QLIC

File compression
Q for Mortals §14 Introduction to kdb+