KDB+/Q Intro

rlwrap helps better console experience. You can install it via

 brew install rlwrap

With rlwrap we can have list of queries.

rlwrap ~/q/m64/q 

KDB+ 4.0 2023.08.11 Copyright (C) 1993-2023 Kx Systems

m64/ 8(24)core 32768MB mac-it-1 macbook-pro-73.local 127.0.0.1 EXPIRE 2024.11.07 hoho@gmail.com KOD #5016803

first line – means runs on version 4

second line – 8 cores on my machine, (24) number of licensed cores , available memory, machine name, ip address, my license is on trial, EXPIRE 2024.11.07,

Set alias q for q itself, and Q for rlwrap

alias Q='rlwrap $QHOME/m64/q'
alias q=$QHOME/m64/q 

We can exit via \\ or exit 2 commands

q)exit 2
(base) MacBook-Pro-73:q mac-it-1$ echo $?
2

We can save our program into a file and call it from the command line.

(base) MacBook-Pro-73:samples mac-it-1$ echo '"cik cik"' > helloworld.q
(base) MacBook-Pro-73:samples mac-it-1$ q helloworld.q 
KDB+ 4.0 2023.08.11 Copyright (C) 1993-2023 Kx Systems
m64/ 8(24)core 32768MB mac-it-1 macbook-pro-73.local 192.168.1.70 EXPIRE 2024.11.07 haha@gmail.com KOD #5016803

"cik cik"

File Handles

  • 0 STDIN (user input)
  • 1 STDOUT (user output)
  • 2 STDERR (error messages)

With adding semicolumn ; after element, execution stops after that action.

q)1 "cik cik\n"
cik cik
1
q)
q)
q)2 "cik cik\n"
cik cik
2
q)2 "cik cik\n";
cik cik
q)2 "cik cik\n";
cik cik
q)3 "cik cik\n";
'Cannot write to handle 3. OS reports: Invalid argument
  [0]  3 "cik cik\n";
       ^
q)1 "cik cik\n";
cik cik

Always use negative file handles. it will also help you append previous line.

q)-1 "cik cik\n";
cik cik

q)exit 2
(base) MacBook-Pro-73:samples mac-it-1$ echo '1 "cik cik ben geldim\n";' |q
cik cik ben geldim

Single Backslash

Single backslash help you move from q to k.

q)\
  +/1 2 3 4
10

Typing another single backslash returns session back to q.

if q has more than one parantheses, it means, it has entered the debugger. if we type another invalid statements, another layer is debugging is entered, and another level of parentheses is displayed at the prompt.

q))\
q)\
  \
q)

Single backslash can be used in a q source file to finish the comment line. It will start by forward slash, then comment, then backslash.

/
this is comment
\

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir