Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chdb-io/chdb-go
Go bindings and cli for chDB, an in-process SQL OLAP Engine powered by ClickHouse
https://github.com/chdb-io/chdb-go
chdb cli clickhouse golang
Last synced: about 1 month ago
JSON representation
Go bindings and cli for chDB, an in-process SQL OLAP Engine powered by ClickHouse
- Host: GitHub
- URL: https://github.com/chdb-io/chdb-go
- Owner: chdb-io
- License: apache-2.0
- Created: 2023-12-23T07:51:05.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-08-27T03:04:30.000Z (4 months ago)
- Last Synced: 2024-10-21T22:20:22.966Z (about 2 months ago)
- Topics: chdb, cli, clickhouse, golang
- Language: Go
- Homepage: https://chdb.io
- Size: 91.8 KB
- Stars: 78
- Watchers: 2
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-clickhouse - chdb-io/chdb-go - chdb-go is a Go bindings library and command-line interface for chDB, an OLAP SQL engine powered by ClickHouse. (Language bindings / Golang)
README
[![chDB-go](https://github.com/chdb-io/chdb-go/actions/workflows/chdb.yml/badge.svg)](https://github.com/chdb-io/chdb-go/actions/workflows/chdb.yml)
# chdb-go
[chDB](https://github.com/chdb-io/chdb) go bindings and chDB cli.## Install
### Install libchdb.so
1. Install [`libchdb`](https://github.com/chdb-io/chdb/releases)
- curl -sL https://lib.chdb.io | bash### Install chdb-go
1. Install `chdb-go`
- `go install github.com/chdb-io/chdb-go@latest`
2. Run `chdb-go` with or without persistent `--path`
- run `$GOPATH/bin/chdb-go`### or Build from source
1. Build `chdb-go`
- run `make build`
2. Run `chdb-go` with or without persistent `--path`
- run `./chdb-go`## chdb-go CLI
1. Simple mode
```bash
./chdb-go "SELECT 123"
./chdb-go "SELECT 123" JSON
```
2. Interactive mode
```bash
./chdb-go # enter interactive mode, but data will be lost after exit
./chdb-go --path /tmp/chdb # interactive persistent mode
``````bash
chdb-io/chdb-go [main] » ./chdb-go
Enter your SQL commands; type 'exit' to quit.
:) CREATE DATABASE IF NOT EXISTS testdb;```
#### Go lib Example
```go
package mainimport (
"fmt"
"os"
"path/filepath""github.com/chdb-io/chdb-go/chdb"
)func main() {
// Stateless Query (ephemeral)
result, err := chdb.Query("SELECT version()", "CSV")
if err != nil {
fmt.Println(err)
}
fmt.Println(result)tmp_path := filepath.Join(os.TempDir(), "chdb_test")
defer os.RemoveAll(tmp_path)
// Stateful Query (persistent)
session, _ := chdb.NewSession(tmp_path)
defer session.Cleanup()_, err = session.Query("CREATE DATABASE IF NOT EXISTS testdb; " +
"CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;")
if err != nil {
fmt.Println(err)
return
}_, err = session.Query("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);")
if err != nil {
fmt.Println(err)
return
}ret, err := session.Query("SELECT * FROM testtable;")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(ret)
}
}
```#### Go SQL driver for chDB
```go
package mainimport (
"database/sql"
"log"_ "github.com/chdb-io/chdb-go/chdb/driver"
)func main() {
db, err := sql.Open("chdb", "")
if err != nil {
log.Fatal(err)
}
rows, err := db.Query(`select COUNT(*) from url('https://datasets.clickhouse.com/hits_compatible/athena_partitioned/hits_0.parquet')`)
if err != nil {
log.Fatalf("select fail, err: %s", err)
}
cols, err := rows.Columns()
if err != nil {
log.Fatalf("get result columns fail, err: %s", err)
}
log.Printf("result columns: %v", cols)
defer rows.Close()
var count int
for rows.Next() {
err := rows.Scan(&count)
if err != nil {
log.Fatalf("scan fail, err: %s", err)
}
log.Printf("count: %d", count)
}
}
```### Golang API docs
- See [lowApi.md](lowApi.md) for the low level APIs.
- See [chdb.md](chdb.md) for high level APIs.