filefieldkdb+bin

kdb: How to read the data of the specified column name from the flat table


Suppose I have a table t locally, stored in the path `d:/t, the table has two columns, the form is as follows:

a b   
------
0 "ab"
1 "bc"
2 "cd"

I use “set” to save locally.

t:([]a:til 3;b:("ab";"bc";"cd"))
`:d:/t set data

My question is: How can I read only column b from the binary file "d:/t". The expected result is as follows:

b   
----
"ab"
"bc"
"cd"

It seems that the function "get" can only get all the data, and cannot read the specified column.what should I do? Thank you for your answer.


Solution

  • As Thomas said you can't load single columns from a binary file without loading the whole contents of that file.

    If your flat file is small enough however, and you'd rather not splay it, then you can still specify specific columns when querying the table - as if you had access to those columns individually:

    q)t:([]a:til 3;b:("ab";"bc";"cd"))
    q)`:test set t
    q)select b from `:test
    b
    ----
    "ab"
    "bc"
    "cd"
    

    Don't be fooled, this is effectively the same operation as select b from get`:t, i.e. we still have to load the whole flat file for the duration of the query. But this avoids having to litter your code with get's