Geek is a kdb+/q interface for Go (Golang). It comes with a kdb+/q load balancer ConnPool, which uses a buffered channel to handle the connections pool.
- ConnPool.Handle
- doesn't serialize or deserialize IPC message.
- before passing on IPC messages, define .geek.user and .geek.ip on the q process
- ConnPool.Sync
- need a known go type pointer as an parameter
- Install Geek
go get -u github.com/jshinonome/geek
- Import it in your code
import "github.com/jshinonome/geek"
- Create an example.go file as below
package main
import "github.com/jshinonome/geek"
func main() {
// connect to a q process @ 1800
q1 := geek.QProcess{Port: 1800}
q1.Dial()
// connect to a q process @ 1801
q2 := geek.QProcess{Port: 1801}
q2.Dial()
qConnPool := geek.NewConnPool()
qConnPool.Put(&q1)
qConnPool.Put(&q2)
qConnPool.Serving()
qEngine := geek.DefaultEngine(qConnPool)
qEngine.Run() // listen and server on :8101
}
- Start two q processes
q -p 1800
q -p 1801
- Run the q Engine
go run example.go
- Start a new q process and run queries(geek.Engine doesn't keep client connection, so don't try to open a handle)
`::8101 ("system";"p") //1800
`::8101 ("system";"p") //1801
- Create an example.go file as below
package main
import (
"fmt"
"time"
"github.com/jshinonome/geek"
)
func main() {
// connect to a q process @ 1800
q := geek.QProcess{Port: 1800}
q.Dial()
sym := "a"
f := struct {
Api string
Sym string
}{
"getTrade", sym,
}
r := make([]trade, 0)
err := q.Sync(&r, f)
if err != nil {
fmt.Println(err)
}
for _, t := range r {
fmt.Printf("%+v\n", t)
}
}
type trade struct {
Time time.Time `k:"time"`
Sym string `k:"sym"`
Price float64 `k:"price"`
Qty int64 `k:"qty"`
}
- Start a q process
q -p 1800
trade:([]time:"p"$.z.D-til 10;sym:10?`a`b;price:10?10.0;qty:10?10);
getTrade:{select from trade where sym=x};
.z.pg:{[x]0N!(`zpg;x);value x};
- Run go program
go run example.go
Intend to support only these 6 types
kdb | go |
---|---|
long | int64 |
float | float64 |
char | byte |
symbol | string |
timestamp | time.Time |
boolean | bool |
go struct without k tags -> k mixed list
struct {
Func string
Param1 string
Param2 string
}
go map -> k dictionary
map[string]bool
go struct with k tags(keys' name) -> k dictionary
struct {
Key1 string `k:"sym"`
Key2 time.Time `k:"time"`
Key3 float64 `k:"qty"`
}
go list of a struct with k tags(column names) -> k table
[]struct {
Sym string `k:"sym"`
Time time.Time `k:"time"`
Qty float64 `k:"qty"`
}