Search code examples
databasekdb

How to load a csv into a table in Q?


Very new to Q and I am having some issues loading my data into a table following the examples on the documentation.

I am running the following code:

table1: get `:pathname.csv

While it doesn't throw an error, when I run the following command nothing comes up:

select * from table1

Or when selecting a specific column:

select col1 from table1

If anyone could guide me in the right direction, that would be great!

Edit: This seems to work and retain all my columns:

table1: (9#"S";enlist csv) 0: `:data.CSV

Solution

  • You're going to need to use 0: https://code.kx.com/q/ref/filenumbers/#load-csv

    The exact usage will depend on your csv, as you need to define the datatypes to load each column as.

    As an example, here I have a CSV with a long, char & float column:

    (kdb) chronos@localhost ~/Downloads $ more example.csv 
    abc,def,ghi
    1,a,3.4
    2,b,7.5
    3,c,88
    (kdb) chronos@localhost ~/Downloads $ q
    KDB+ 3.6 2018.10.23 Copyright (C) 1993-2018 Kx Systems
    l64/ 4()core 3894MB chronos localhost 127.0.0.1 EXPIRE 2019.06.15 [email protected] KOD #5000078
    
    q)("JCF";enlist",")0:`:example.csv
    abc def ghi
    -----------
    1   a   3.4
    2   b   7.5
    3   c   88 
    q)meta ("JCF";enlist",")0:`:example.csv
    c  | t f a
    ---| -----
    abc| j    
    def| c    
    ghi| f    
    q)
    

    I use the chars "JCF" to define the datatypes long, character & float respectively.

    I enlist the delimiter (",") to indicate that the first row of the CSV contains the headers for the columns. (Otherwise, these can be supplied in your code & the table constructed)

    On a side note, note that in q-sql, the * is not necessary as in standard SQL; you can simply do select from table1 to query all columns