开发手册 欢迎您!
软件开发者资料库

Q语言 - 功能

Q语言函数 - 使用这个初学者教程,简单易学地学习KDB +,包括从概述,架构,Q编程语言,类型转换,时态数据,列表,索引,字典,表格,动词和副词,联接开始的基本知识到高级知识,函数,内置函数,查询,进程间通信,消息处理程序(.Z库),属性,功能查询,表算法,磁盘上的表,维护功能。

函数类型

函数可以通过多种方式进行分类.在这里,我们根据它们采用的参数的数量和类型以及结果类型对它们进行了分类.功能可以是,

  • Atomic : 参数是原子的并产生原子结果

  • 聚合 : 列表中的原子

  • 统一(从列表中列出) : 扩展原子的概念,因为它们适用于列表.参数列表的计数等于结果列表的计数.

  • 其他 : 如果该函数不是来自上述类别.

数学中的二进制运算在q中被称为二元函数 ;例如,"+".类似地,一元操作被称为 monadic函数;例如,"abs"或"floor".

常用函数

q

abs

 q )abs -9.9/Absolute value,Negates -ve number&leaves non -ve number  9.9

all

q) all 4 5 0 -4 / Logical AND (numeric min), returns the minimum value0b

Max(&),Min(|)和Not(!)

q) /And, Or, and Logical Negationq) 1b & 1b        / And (Max)1bq) 1b|0b              / Or (Min)1bq) not 1b             /Logical Negate (Not)0b

asc

q)asc 1 3 5 7 -2 0 4    / Order list ascending, sorted list                        / in ascending order is returned`s#-2 0 1 3 4 5 7q)/attr - gives the attributes of data, which describe how it's sorted.`s denotes fully sorted, `u denotes unique and `p and `g are used to refer to lists with repetition, with `p standing for parted and `g for grouped

avg

q)avg 3 4 5 6 7           / Return average of a list of numeric values5fq)/Create on trade tableq)trade:([]time:3?(.z.Z-200);sym:3?(`ibm`msft`apple);price:3?99.0;size:3?100)

by

q)/ by - Groups rows in a table at given symq)select sum price by sym from trade    / find total price for each sym  sym  |   price------ | -------- apple | 140.2165  ibm  | 16.11385

cols

q)cols trade / Lists columns of a table`time`sym`price`size

count

q)count (til 9) / Count list, count the elements in a list and                / return a single int value 9

port

q)\p 9999 / assign port numberq)/csv - This command allows queries in a browser to be exported to   excel by prefixing the query, such as http://localhost:9999/.csv?select from trade where sym =`ibm

cut

q)/ cut - Allows a table or list to be cut at a certain pointq)(1 3 5) cut "abcdefghijkl"                            / the argument is split at 1st, 3rd and 5th letter."bc""de""fghijkl"q)5 cut "abcdefghijkl"      / cut the right arg. Into 5 letters part                            / until its end."abcde""fghij""kl"

删除

q)/delete - Delete rows/columns from a tableq)delete price from trade          time              sym   size---------------------------------------  2009.06.18T06:04:42.919  apple   36  2009.11.14T12:42:34.653   ibm    12  2009.12.27T17:02:11.518  apple   97

Distinct

q)/distinct - Returns the distinct element of a listq)distinct 1 2 3 2 3 4 5 2 1 3            / generate unique set of number1 2 3 4 5

enlist

q)/enlist - Creates one-item list.q)enlist 37,37q)type 37           / -ve type value-7hq)type enlist 37    / +ve type value7h

填充(^)

q)/fill - used with nulls. There are three functions for processing null values.The dyadic function named fill replaces null values in the right argument with the atomic left argument.q)100 ^ 3 4 0N 0N -53 4 100 100 -5q)`Hello^`jack`herry``john``jack`herry`Hello`john`Hello

填写

q)/fills - fills in nulls with the previous not null value.q)fills 1 0N 2 0N 0N 2 3 0N -5 0N1 1 2 2 2 2 3 3 -5 -5

第一个

q)/first - returns the first atom of a listq)first 1 3 34 5 31

翻转

q)/flip - Monadic primitive that applies to lists and associations. It interchange the top two levels of its argument.q)trade       time                   sym      price   size------------------------------------------------------  2009.06.18T06:04:42.919    apple   72.05742   36  2009.11.14T12:42:34.653    ibm     16.11385   12  2009.12.27T17:02:11.518    apple   68.15909   97q)flip tradetime | 2009.06.18T06:04:42.919 2009.11.14T12:42:34.6532009.12.27T17:02:11.518sym   |  apple         ibm         appleprice | 72.05742     16.11385    68.15909size  | 36 12 97

iasc

 q)/iasc  - 索引递增,返回相对于输入列表的升序排序列表的索引. q)iasc 5 4 0 3 4 9  2 3 1 4 0 5

Idesc

 q)/idesc  - 索引降序,返回相对于输入列表的下降排序列表q)idesc 0 1 3 4  3 2 1 0

in

 q)/in  - 在列表中,二元函数用于查询列表(右侧)关于其内容的列表. q)(2 4)in 1 2 3  10b

insert

q)/insert - Insert statement, upload new data into a table.q)insert[`trade;((.z.Z);`samsung;48.35;99)],3q)trade      time                  sym       price     size------------------------------------------------------ 2009.06.18T06:04:42.919   apple    72.05742     36 2009.11.14T12:42:34.653    ibm     16.11385     12 2009.12.27T17:02:11.518   apple    68.15909     97 2015.04.06T10:03:36.738   samsung  48.35        99

key

 q)/key  - 三个不同的函数,即生成+ ve整数,给出目录的内容或表/字典的键. q)key 9  0 1 2 3 4 5 6 7 8 q)key`:c:` $ RECYCLE. BIN`Config.Msi`Documents and Settings`Drivers`Geojit`hiberfil.sys`I ..

lower

 q)/lower  - 转换为小写和floor q)lower("JoHn";`HERRY`SYM)"john"`herry`sym

最大和最小(即|和&)

q)/Max and Min / a|b and a&bq)9|79q)9&55

null

 q)/null  - 如果是atom是一个null else参数列表中的0b q)null 1 3 3 0N $ b $ b 0001b

Peach

q)/peach - Parallel each, allows process across slavesq)foo peach list1       / function foo applied across the slaves named in list1'list1q)foo:{x+27}q)list1:(0 1 2 3 4)q)foo peach list1       / function foo applied across the slaves named in list127 28 29 30 31

Prev

q)/prev - returns the previous element i.e. pushes list forwardsq)prev 0 1 3 4 5 70N 0 1 3 4 5

随机(?)

q)/random - syntax - n?list, gives random sequences of ints and floatsq)9?50 0 4 0 3 2 2 0 1q)3?9.90.2426823 1.674133 3.901671

Raze

q)/raze - Flattn a list of lists, removes a layer of indexing from a list of lists. for instance:q)raze (( 12 3 4; 30 0);("hello";7 8); 1 3 4)12 3 430 0"hello"7 8134

read0

q)/read0 - Read in a text fileq)read0 `:c:/q/README.txt    / gives the contents of *.txt file

read1

q)/read1 - Read in a q data fileq)read1 `:c:/q/t10xff016200630b000500000073796d0074696d6500707269636…

反转

q)/reverse - Reverse a listq)reverse 2 30 29 1 3 44 3 1 29 30 2q)reverse "HelloWorld""dlroWolleH"

set

q)/set - set value of a variableq)`x set 9`xq)x9q)`:c:/q/test12 set trade`:c:/q/test12q)get `:c:/q/test12       time                   sym      price     size---------------------------------------------------------  2009.06.18T06:04:42.919    apple    72.05742    36  2009.11.14T12:42:34.653     ibm     16.11385    12  2009.12.27T17:02:11.518    apple    68.15909    97  2015.04.06T10:03:36.738    samsung  48.35       99  2015.04.06T10:03:47.540    samsung  48.35       99  2015.04.06T10:04:44.844    samsung  48.35       99

ssr

q)/ssr - String search and replace, syntax - ssr["string";searchstring;replaced-with]q)ssr["HelloWorld";"o";"O"]"HellOWOrld"

string

q)/string - converts to string, converts all types to a string format.q)string (1 2 3; `abc;"XYZ";0b)(,"1";,"2";,"3")"abc"(,"X";,"Y";,"Z"),"0"

SV

q)/sv - Scalar from vector, performs different tasks dependent on its arguments.It evaluates the base representation of numbers, which allows us to calculate the number of seconds in a month or convert a length from feet and inches to centimeters.q)24 60 60 sv 11 30 4941449   / number of seconds elapsed in a day at 11:30:49

system

q)/system - allows a system command to be sent,q)system "dir *.py"" Volume in drive C is New Volume"" Volume Serial Number is 8CD2-05B2"""" Directory of C:\\Users\\myaccount-raj""""09/14/2014    06:32 PM     22 hello1.py""                1 File(s)    22 bytes"

q)/tables - list all tablesq)tables ``s#`tab1`tab2`trade

Til

q)/til - Enumerateq)til 50 1 2 3 4

trim

q)/trim - Eliminate string spacesq)trim " John ""John"

v s

q)/vs - Vector from scaler , produces a vector quantity from a scaler quantityq)"|" vs "20150204|msft|20.45""20150204""msft""20.45"

xasc

q)/xasc - Order table ascending, allows a table (right-hand argument) to be sorted such that (left-hand argument) is in ascending orderq)`price xasc trade          time                 sym      price      size----------------------------------------------------------   2009.11.14T12:42:34.653     ibm     16.11385     12   2015.04.06T10:03:36.738   samsung   48.35        99   2015.04.06T10:03:47.540   samsung   48.35        99   2015.04.06T10:04:44.844   samsung   48.35        99   2009.12.27T17:02:11.518    apple    68.15909     97   2009.06.18T06:04:42.919    apple    72.05742     36

xcol

q)/xcol - Renames columns of a tableq)`timeNew`symNew xcol trade        timeNew                 symNew    price      size-------------------------------------------------------------   2009.06.18T06:04:42.919      apple    72.05742     36   2009.11.14T12:42:34.653       ibm     16.11385     12   2009.12.27T17:02:11.518      apple    68.15909     97   2015.04.06T10:03:36.738     samsung   48.35        99   2015.04.06T10:03:47.540     samsung   48.35        99   2015.04.06T10:04:44.844     samsung   48.35        99

xcols

q)/xcols - Reorders the columns of a table,q)`size`price xcols trade  size    price           time                   sym-----------------------------------------------------------   36   72.05742   2009.06.18T06:04:42.919      apple    12   16.11385   2009.11.14T12:42:34.653       ibm   97   68.15909   2009.12.27T17:02:11.518      apple   99   48.35      2015.04.06T10:03:36.738     samsung    99   48.35      2015.04.06T10:03:47.540     samsung   99   48.35      2015.04.06T10:04:44.844     samsung

xdesc

q)/xdesc - Order table descending, allows tables to be sorted such that the left-hand argument is in descending order.q)`price xdesc trade        time                   sym       price      size-----------------------------------------------------------   2009.06.18T06:04:42.919    apple    72.05742      36   2009.12.27T17:02:11.518    apple    68.15909      97   2015.04.06T10:03:36.738   samsung   48.35         99   2015.04.06T10:03:47.540   samsung   48.35         99   2015.04.06T10:04:44.844   samsung   48.35         99   2009.11.14T12:42:34.653     ibm     16.11385      12

xgroup

q)/xgroup - Creates nested tableq)`x xgroup ([]x:9 18 9 18 27 9 9;y:10 20 10 20 30 40)'lengthq)`x xgroup ([]x:9 18 9 18 27 9 9;y:10 20 10 20 30 40 10)  x  |    y---- | -----------  9  | 10 10 40 10  18 | 20 20  27 | ,30

xkey

q)/xkey - Set key on tableq)`sym xkey trade    sym   |      time                    price     size--------- | -----------------------------------------------   apple  | 2009.06.18T06:04:42.919    72.05742     36    ibm   | 2009.11.14T12:42:34.653    16.11385     12   apple  | 2009.12.27T17:02:11.518    68.15909     97  samsung | 2015.04.06T10:03:36.738    48.35        99  samsung | 2015.04.06T10:03:47.540    48.35        99  samsung | 2015.04.06T10:04:44.844    48.35        99

系统命令

系统命令控制 q 环境.它们具有以下形式 :

  \ cmd [p]     其中p可以是可选的

下面讨论了一些流行的系统命令 :

\ a [namespace]  - 列出给定命名空间中的表

q)/Tables in default namespaceq)\a,`tradeq)\a .o         / table in .o namespace.,`TI

\b  - 查看依赖关系

q)/ views/dependenciesq)a:: x+y      / global assingmentq)b:: x+1q)\b`s#`a`b

\B  - 待定视图/依赖项

q)/ Pending views/dependenciesq)a::x+1     / a depends on xq)\B         / the dependency is pending' / the dependency is pendingq)\B`s#`a`bq)\b`s#`a`bq)b29q)a29q)\B`symbol$()

\ cd  - 更改目录

q)/change directory, \cd [name]q)\cd"C:\\Users\\myaccount-raj"q)\cd ../new-accountq)\cd"C:\\Users\\new-account"

\d  - 设置当前命名空间

q)/ sets current namespace \d [namespace]q)\d             /default namespace'q)\d .o          /change to .oq.o)\d`.oq.o)\d .         / return to defaultq)key `          /lists namespaces other than .z`q`Q`h`j`oq)\d .john       /change to non-existent namespaceq.john)\d`.johnq.john)\d .q)\d`.

\l  - 从db加载文件或目录

q)/ Load file or directory, \lq)\l test2.q / loading test2.q which is stored in current path.   ric      |     date      ex      openP    closeP    MCap----------- | ------------------------------------------------- JPMORGAN   | 2008.05.23  SENSEX  18.30185  17.16319  17876   HSBC     | 2002.05.21  NIFTY   2.696749  16.58846  26559 JPMORGAN   | 2006.09.07  NIFTY   14.15219  20.05624  14557   HSBC     | 2010.10.11  SENSEX  7.394497  25.45859  29366 JPMORGAN   | 2007.10.02  SENSEX  1.558085  25.61478  20390    ric     |    date       ex      openP     closeP    MCap---------- | ------------------------------------------------  INFOSYS  | 2003.10.30    DOW    21.2342   7.565652   2375 RELIANCE  | 2004.08.12    DOW    12.34132  17.68381   4201   SBIN    | 2008.02.14    DOW    1.830857  9.006485   15465  INFOSYS  | 2009.06.11  HENSENG  19.47664  12.05208   11143   SBIN    | 2010.07.05    DOW    18.55637  10.54082   15873

\p  - 端口号

q)/ assign port number, \pq)\p5001iq)\p 8888q)\p8888i

\\  - 从q控制台退出

\\ - exitExit form q.