Next: , Previous: , Up: Top  


12 I/O and Interface

Given k9 is useful for analyzing data it won’t be a surprise that input and output (I/O) are supported. k9 has been optimized to read in data quickly so if you have a workflow of making a tea while the huge csv file loads you might have an issue.

k9 is also able to interface with other languages so one doesn’t need to rewrite everything already written into k9.

 I/O         
 0:  r/w line
 1:  r/w char
*2:  r/w data
*3:  kipc set
*4:  http get

 Interface
`csv?`csv t    / read/write csv
`json?`json t  / read/write json
*ffi: "./a.so"5:`f!"ii" /I f(I i){return 2+i;}       //cblas ..
*c/k: "./b.so"5:`f!1    /K f(K x){return ki(2+xi);}  //feeds ..

12.1 Input format values to table

This section shows you the syntax for reading in data into a table with the correct type.

 d:,(`date`time`int`float`char`symbol)            / headers 
 d,:,(2020.04.20;12:34:56.789;37;12.3;"hi";`bye)) /data
 d                                          
date       time         int float char  symbol
2020.04.20 12:34:56.789 37  12.3  hi    bye   

 `csv'd                                           / to csv
date,time,int,float,char,symbol            
2020.04.20,12:34:56.789,37,12.3,"hi",bye

 "some.csv"0:`csv'd                               / write to some.csv
 0:"some.csv"                                     / read from some.csv
date,time,int,float,char,symbol            
2020.04.20,12:34:56.789,37,12.3,"hi",bye

 ("Dtifs*";,",")0:"some.csv"                      / read into table
date       time         int float char    symbol
---------- ------------ --- ----- ------- ------
2020.04.20 12:34:56.789  37 12.3  "hi"    bye   

12.2 read CSV ⇒ ‘csv?x

Convert x from CSV format. Works on lists and tables.

 `csv?("a,b";"1,3.";"2,4.")
a b 
- --
1 3.
2 4.

12.3 write csv ⇒ ‘csv x

Convert x to CSV format. Works on lists and tables.

 `csv [[]a:1 2;b:3. 4.]
a,b 
1,3.
2,4.

12.4 read json ⇒ ‘json?x

Convert x from json format. Works on lists and tables.

 `json?("{a:1,b:3.}";"{a:2,b:4.}")
a b 
- --
1 3.
2 4.

12.5 write json ⇒ ‘json x

Convert x to json format. Works on lists and tables.

 `json [[]a:1 2;b:3. 4.]
{a:1,b:3.}
{a:2,b:4.}

12.6 write line ⇒ x 0:y

Output to x the list of strings in y. y must be a list of strings. If y is a single stream then convert to list via enlist.

 ""0:("blue";"red")      / "" represents stdout
blue
red

 "some.csv" 0:`csv [[]a:1 2;b:3. 4.]
"some.csv"

12.7 read line ⇒ 0:x

Read from file x.

 0:"some.csv"
a,b 
1,3.
2,4.

12.8 write char ⇒ x 1:y

Output to x the list of chars in y. y must be a list of chars. If y is a single char then convert to list via enlist.

 "some.txt"1:"hello here\nis some text\n"
 1:"some.txt"
"hello here\nis some text\n"
  t:+`a`b!(1 2;3 4);t
a b
- -
1 3
2 4
 "some.k"1:`k t      / write table to file in k format

12.9 read char ⇒ 1:x

Read from file x.

 "some.txt"0:,`csv 3 1 2 / first write a file to some.txt
 1:"some.txt"            / now read it back
"3,1,2\n"
  t:+`a`b!(1 2;3 4);t
a b
- -
1 3
2 4
 t:`k?1:"some.k";t   / read file stored in k format (as shown above)
a b
- -
1 3
2 4

12.10 read data ⇒ 2: x

Load file, eg. csv or from a (x 2: y) save. For the latter one can find a “save then load” example in the next section.

 2:`t.csv
s    t        e p  z   
---- -------- - -- ----
AABL 09:30:00 D 11 4379
AABL 09:30:00 B 40 3950

 2:`r                / read from file
a          b          c          d          e         
---------- ---------- ---------- ---------- ----------
0.5366064  0.8250996  0.8978589  0.4895149  0.6811532 
0.1653467  0.05017282 0.4831432  0.4657975  0.4434603 
0.08842649 0.8885677  0.23108    0.3336785  0.6270692 
..

12.11 write data ⇒ x 2: y

Save to file x non-atomic data y (eg. lists, dictionaries, or tables).

This example saves 100mio 8-byte doubles to file. The session is then closed and a fresh session reads in the file. Both the write (420 ms) and compute statistc from file (146 ms) have impressive speeds given the file size (800 MB).

 n:_1e8
 r:+`a`b`c`d`e!5^n?1.;r
 `r 2:r              / write to file 

Start new session.

 \t r:2:`r;select avg a,sum b, max c, min d, sum e from r
148

12.12 conn/set ⇒ 3:

Enterprise only.

12.13 http/get ⇒ 4:

Enterprise only.

12.14 lib ⇒ 5:

Enterprise only.

Load shared library.

Contents of file ’a.c’

int add1(int x){return 1+x;}
int add2(int x){return 2+x;}
int indx(int x[],int y){return x[y];}

Compile into a shared library (done on macos here)

% clang -dynamiclib -o a.so a.c

Load the shared library into the session.

 f:"./dev/a.so"5:{add1:"i";add2:"i";indx:"Ii"}
 f[`add1] 12
13
 f[`indx][12 13 14;2]
14

Next: , Previous: , Up: Top