Going Deeper with the Free Version of kdb+
We are using a 32-bit CentOS 6.5 machine for the free version. If you are using a 64-bit machine, make sure you install the ia32-libs package (yum install ia32-libs). Installation is very simple. First, download the package from kx.com. Then, choose an installation directory$ mkdir kdb $ mv ~/linux.zip kdb $ cd kdb $ unzip linux.zip
You will see a single directory, q, with the following
l32 q.k q.q q.sh README.txt s.k sp.q trade.q
The README.txt file can be consulted for other installation scenarios. Next, set the QHOME variable to the location of the q directory, move to the q directory, and start q:
$ export QHOME=/home/deadline/kdb/q $ cd q $ l32/q
If everything extracted correctly, you should see the following:
KDB+ 3.1 2014.08.22 Copyright (C) 1993-2014 Kx Systems l32/ 2()core 1884MB deadline gromit 255.255.255.255 NONEXPIRE Welcome to kdb+ 32bit edition For support please see http://groups.google.com/d/forum/personal-kdbplus Tutorials can be found at http://code.kx.com/wiki/Tutorials To exit, type \\ To remove this startup msg, edit q.q q)
You can begin using kdb+ from the q) prompt. To exit exit kdb+ enter \\. Before we look at any examples, let's add the ability to scroll through our q commands by using the rlwrap package. If rlwrap is not installed, it can be installed using yum (as root):
# yum install rlwrap
Note: the rlwrap package is in the epel repository. Next, create a file in your q directory called q.sh with the following contents:
#!/bin/bash cd ~/q rlwrap l32/q "$@"
Finally, make the file executable:
chmod u+x q.sh
If we use this script to start q, then we can scroll back through previous commands we entered from the q) prompt.
We are now ready to try a few simple examples. To get a full understanding of using kdb+, please consult the links below. This tutorial is designed to provide you some basic working knowlege of kdb+. Start kdn+ and from the q) prompt type:
q)"Hello world!" "Hello world!" q)\ls *.q "q.q" "sp.q" "trade.q" q)5+7 12 q)5 + 5 7 12 10 12 17
In the above examples. we printed "Hello World!", listed the files that end in "q" in the current directory, summed two numbers and then incremented a list of numbers by 5.
Next, let's define the factorial function:
factorial:{prd 1+til x}
As you can see, q has a very terse expression. If we now use the factorial function
we get the following:
q)factorial[7] 5040
Next, we will create a table with 1 million rows filled with random time-series data. Again, you can investigate the syntax later. For now, the examples illustrate the ease with which data can generate and be processed using kdb+. Our table is created as follows (note the ; at the end of each command):
q)n:1000000; q)item:`apple`banana`orange`pear; q)city:`beijing`chicago`london`paris; q)table:([]time:asc n?0D0;n?item;amount:n?100;n?city);
Now that the data is in memory, we can ask a simple query selecting all rows from the table where the item sold is a banana:
q) select from table where item=`banana time item amount city ------------------------------------------ 0D00:00:00.048360228 banana 62 beijing 0D00:00:00.159745663 banana 27 london 0D00:00:00.480262935 banana 40 london 0D00:00:00.548035651 banana 32 chicago 0D00:00:00.705146044 banana 22 paris 0D00:00:00.712388008 banana 98 london 0D00:00:00.958473980 banana 48 paris 0D00:00:01.071770489 banana 40 london 0D00:00:04.457911849 banana 65 paris ..
Only a subset of answers is printed. We can also generate an aggregate query that calculates the sum of the amounts sold of all items by each city:
q)select sum amount by city from table city | amount -------| -------- beijing| 12418161 chicago| 12342736 london | 12367712 paris | 12383797
Even in these simple examples the real power of kdb+/q is easily seen. To learn more about using kdb+ and q, please consult the following resources:
