Skip to content

Go bindings and cli for chDB, an in-process SQL OLAP Engine powered by ClickHouse

License

Notifications You must be signed in to change notification settings

chdb-io/chdb-go

 
 

Repository files navigation

chDB-go

chdb-go

chDB go bindings and chDB cli.

Install

  1. Download and install libchdb
  • run make update_libchdb to download and extract libchdb.so. or
  • run make install to install libchdb.so
  1. Build chdb-go
  • run make build
  1. Run chdb-go with or without persistent --path
  • run ./chdb-go

chdb-go CLI

  1. Simple mode
./chdb-go "SELECT 123"
./chdb-go "SELECT 123" JSON
  1. Interactive mode
./chdb-go # enter interactive mode, but data will be lost after exit
./chdb-go --path /tmp/chdb # interactive persistent mode
  1. Shortcuts
  • \l to list databases;
  • \dt dbname to list tables in database;
chdb-io/chdb-go [main] » ./chdb-go 
Enter your SQL commands; type 'exit' to quit.
 :) CREATE DATABASE IF NOT EXISTS testdb;

 :) \l
┏━━━━━━━━━━━━━━━━━━━━┓
┃ name               ┃
┡━━━━━━━━━━━━━━━━━━━━┩
│ INFORMATION_SCHEMA │
├────────────────────┤
│ _local             │
├────────────────────┤
│ information_schema │
├────────────────────┤
│ system             │
├────────────────────┤
│ testdb             │
└────────────────────┘

 :) CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree()
:-] ORDER BY id;

 :) \dt testdb
┏━━━━━━━━━━━┓
┃ name      ┃
┡━━━━━━━━━━━┩
│ testtable │
└───────────┘

Go lib Example

package main

import (
    "fmt"
    "github.com/chdb-io/chdb-go/chdb"
)

func main() {
    // Stateless Query (ephemeral)
    result := chdb.Query("SELECT version()", "CSV")
    fmt.Println(result)

    // Stateful Query (persistent)
    session, _ := NewSession(path)
    defer session.Cleanup()

    session.Query("CREATE DATABASE IF NOT EXISTS testdb; " +
    "CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;")

    session.Query("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);")

    ret := session.Query("SELECT * FROM testtable;")
    fmt.Println(ret)
}

Go SQL driver for chDB

package main

import (
        "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

Thanks

About

Go bindings and cli for chDB, an in-process SQL OLAP Engine powered by ClickHouse

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 5