Next: , Previous: , Up: Top  


11 I/O and Interface

In order to support fast data analysis, k9 supports fast input and output (I/O). If you have the habit of making tea while a huge csv file loads, you might have to opt to take a few sips from a glass of water instead.

k9 is also able to interface with other languages so one doesn’t need to translate everything you’ve already written in those other languages to k9.

 I/O(*enterprise)
 0: r/w line
 1: r/w byte
*2: r/w data
*3: k-ipc set    
*4: https get
*5: ffi/py/js/..

`js?`js d
`csv?`csv t

11.1 Example of Data I/O

Let’s begin with a simple example to show how to read data from a csv file. We’ll generate a small table and save it with and without headers (wHeader.csv and woHeader.csv respectively). Then we’ll read it back in, both specifying types and with the csv reader. Types are specifed by one letter and the full list can be found in Atom Types.

 t:[[]a:3 2 1;b:1. 2. 4.;c:`x`y`z];t / generate table
a b  c
- -- -
3 1. x
2 2. y
1 4. z

 "wHeader.csv"1:`csv t               / save with column headers
wHeader.csv

 "woHeader.csv"1:1_`csv t             / save without col headers
woHeader.csv

 `csv?1:"wHeader.csv"                 / default reader
a b  c
- -- -
3 1. x
2 2. y
1 4. z

 (",";"ifn")1:"wHeader.csv"          / separator and types
a b  c
- -- -
3 1. x
2 2. y
1 4. z

 (`e`f`g;",";"ifn")0:"woHeader.csv"  / names, separator and types
e f  g
- -- -
3 1. x
2 2. y
1 4. z

11.2 read csv ⇒ `csv?x

Convert x from csv format. Works on lists and tables. You can also use `cs.

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

11.3 write csv ⇒ `csv x

Convert x to csv format. Works on lists and tables. You can also use `cs.

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

11.4 read json ⇒ `js?x

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

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

11.5 write json ⇒ `js x

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

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

11.6 read name value ⇒ `nv?x

Convert x, a list of name:value pairs, to a dictionary.

 `nv?("key1:value1";"key2:value2")
key1|value1
key2|value2

11.7 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 string then convert to list via list.

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

 "file.txt" 0: ("blue"; "red") / write to file, one line per element

11.8 read line ⇒ 0:x

Read from file x.

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

11.9 write byte ⇒ x 1:y

Output to file x the list of chars in y as bytes.

 "some.txt"1:"0123ABab"
"some.txt"

 1:"some.txt"
"0123ABab"

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

 1: "some.csv"
"a,b\n1,3.00\n2,4.00\n"

 `csv?1:"some.csv"
a b
- -
1 3
2 4

11.10 read byte ⇒ 1:x

Read from byte data from file x. See w byte for an example to write and then read byte data.

11.11 read data ⇒ 2: x

Enterprise Only

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 
..

11.12 write data ⇒ x 2: y

Enterprise only

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

This example saves 100 million 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 statistics from the file have impressive speeds (146 ms) given the file size (800 MB).

 n:_1e8
 r:+`a`b`c`d`e!5^n rand 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

11.13 k-ipc set ⇒ 3:

Enterprise only

11.14 https get ⇒ 4:

Enterprise only

11.15 ffi/py/js/.. ⇒ 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