# A mountain tour of kdb+ and q¶

This is a mountain tour of q, the programming language built into kdb+. It takes the form of a one-page ‘ridge walk’ along the tops, with optional side descents to see more detail.

Use it as a very fast start with kdb+, or for a quick overview of what it is like to work in q.

Before you start

Download, install and launch q. Experiment with the expressions as you read.

Follow links to deeper treatments of topics.

## The q session¶

The q session is a REPL. It evaluates a q expression and prints the result. You can use it as a calculator.

$q KDB+ 3.7t 2020.03.05 Copyright (C) 1993-2020 Kx Systems m64/ 4()core 8192MB sjt ... q)2+2 3 4 4 5 6 q)acos -1 3.141593 End your session with the Terminate system command. q)\\$

## Databases¶

### Tables¶

Tables are first-class objects in q.

Load the Suppliers and Parts database.

q)\l sp.q
p  | p$p1 qty| 300 Joining a dictionary to a table appends a tuple. q)sp,spqty!(s5;p3;159) s p qty --------- s1 p1 300 s1 p2 200 s1 p3 400 s1 p4 200 s4 p5 100 s1 p6 100 s2 p1 300 s2 p2 400 s3 p2 200 s4 p2 200 s4 p4 300 s1 p5 400 s5 p3 159 ### Functions¶ Function notation is simple. A function can have up to eight arguments. Unless explicitly named, the first three arguments are assumed to be x, y, and z. q){x*x}2 -1.5 17 4 2.25 289 q)el:{[e;a;v;c]"<",e," ",a,"=\"",v,"\">",c,"</",e,">"} q)el["a";"href";"https://example.com/";"link text"] "<a href=\"https://example.com/\">link text</a>" ### Iteration¶ Control structures such as do and while are rarely used for iteration. Much iteration is implicit in the operators. q)2 3 4 + 10 12 13 14 q)2 3 4 + 10 100 1000 12 103 1004 Most other iteration is handled by keywords, and special operators called iterators. q)count each ("quick";"brown";"fox") 5 5 3 q).h.htc[p;"The quick brown fox"] / mark up "<p>The quick brown fox</p>" q)"The quick brown fox" {.h.htc[y;x]}/ pbodyhtml "<html><body><p>The quick brown fox</p></body></html>" q)8 {x,sum -2#x}\1 1 / 10 Fibonacci numbers 1 1 1 1 2 1 1 2 3 1 1 2 3 5 1 1 2 3 5 8 1 1 2 3 5 8 13 1 1 2 3 5 8 13 21 1 1 2 3 5 8 13 21 34 1 1 2 3 5 8 13 21 34 55 ## Communication¶ Interprocess communication is ‘baked in’ to q. It requires no library code and is easy to set up. Watch two kdb+ processes communicating through TCP/IP. ### Client/server¶ Use two command shells for this. On the left, we have the server task; on the right, the client. KDB+ 3.7t 2020.01.22 … | KDB+ 3.7t 2020.01.22 m64/ 4()core 8192MB … | m64/ 4()core 8192MB … | q)\p 5432 | | q)h:hopen ::5432 | q)h"2+2" | 4 | | q)h "system\"l /Users/sjt/q/sp.q\"" q)+pcity!(p$p1p2p3p4p5  |
(s#+(,color)!,s#bluegreen  |
+spqty!(s$s1s1s1s2s3 | | q)h "select from sp where s in s2s3" | s p qty | --------- | s2 p1 300 | s2 p2 400 | s3 p2 200 | q) On the left, the server task started listening on port 5432. The client task opened a socket to port 5432, getting a handle, which it dubbed h. The client task sent to the server the expression 2+2 to be evaluated, and received the value 4 in return. The client task told the server to load the Suppliers and Parts script. The server task session showed that script loaded. The client sent the server a qSQL query and got a table as a result. Asynchronous calls are only slightly more complicated. A production system requires code in the callbacks to secure communications but you can see from the above that the basics are very simple. The baked-in interprocess communications make it simple to implement systems as tasks distributed over multiple machines. ### Webserver¶ A q session can listen for HTTP requests and act as a webserver. The default callback composes a page for browsing tables in the session. q)tables[] / Suppliers & Parts pssp q)\p 8090 / listen to port 8090 Browse to http://localhost:8090. ## Development¶ ### Scripts¶ Write and load scripts to define an application. Scripts are text files. The sp.q script defines the Suppliers & Parts database and runs some queries on it. s:([s:s1s2s3s4s5] name:smithjonesblakeclarkadams; status:20 10 30 20 30; city:londonparisparislondonathens) p:([p:p1p2p3p4p5p6] name:nutboltscrewscrewcamcog; color:redgreenblueredbluered; weight:12 17 17 14 12 19; city:londonparisromelondonparislondon) sp:([] s:s$s1s1s1s1s4s1s2s2s3s4s4s1; / fkey
p:p\$p1p2p3p4p5p6p1p2p2p2p4p5; / fkey
qty:300 200 400 200 100 100 300 400 200 200 300 400)

select distinct p,s.city from sp

select sum qty by p.color from sp

select from sp where s.city=p.city`

In scripts, q expressions can be written across multiple lines.

### IDE¶

KX Developer is a free interactive development environment (IDE) for q.