Cookbook/NamedPipes

From Kx Wiki
Jump to: navigation, search

Since kdb+ v3.4 it's possible to read FIFOs/named pipes on unix.

q)h:hopen`:fifo://file / Opens file as read-only. Note the fifo prefix 
q)read1 h              / Performs a single blocking read into a 64k byte buffer. Returns empty byte vector on eof
q)read1 (h;n)          / Alternatively, specify the buffer size n. At most, n bytes will be read, perhaps fewer
q)hclose h             / Close the file to clean up

.Q.fps is .Q.fs for pipes. The following example loads a csv via fifo, avoiding decompressing to disk:

q)system"rm -f fifo && mkfifo fifo"
q)system"unzip -p t.zip t.csv > fifo &"
q)trade:flip `sym`time`ex`cond`size`price!"STCCFF"$\:()
q).Q.fps[{`trade insert ("STCCFF";",")0:x}]`:fifo

A `:fifo:// handle is also useful for reading certain non-seekable or zero-length (therefore, not suitable for the regular read1) system files/devices, e.g.

q)a:hopen`:fifo:///dev/urandom
q)read1 (a;8)
0x8f172b7ea00b85e6
q)hclose a
Personal tools
Namespaces
Variants
Actions
Navigation
Print/export
Toolbox