最近更新时间:2023.08.17 18:58:09
首次发布时间:2023.07.27 10:36:33
本文介绍如何通过 ByteHouse Go Driver 连接并访问 ByteHouse 云数仓。
请访问 github 项目主页,获取 ByteHouse Go 驱动最新的文档和发布版本。
请参考通过驱动连接到 ByteHouse,了解如何通过API Token 或用户名+密码的方式连接到 ByteHouse 。
创建一个数据库和数据表。
说明
参考使用时,需编辑“host:port”和“api token”的字段。
package main import ( "context" "fmt" "github.com/bytehouse-cloud/driver-go/sdk" ) func main() { dsn := fmt.Sprintf("tcp://host:port?secure=true&token=%v", "Your-API-Token", ) ctx := context.Background() g, err := sdk.Open(ctx, dsn) if err != nil { panic(err) } if err := g.Ping(); err != nil { panic(err) } if qs, err := g.QueryContext(ctx, "CREATE DATABASE my_db"); err != nil || qs.Exception() != nil { if err != nil { panic(err) } if qs.Exception() != nil { panic(qs.Exception()) } } if qs, err := g.QueryContext(ctx, `CREATE TABLE my_db.animal ( dog Int64, cat Int64 ) ENGINE=CnchMergeTree ORDER BY dog`); err != nil || qs.Exception() != nil { if err != nil { panic(err) } if qs.Exception() != nil { panic(qs.Exception()) } } }
将值插入到新建的表中。
说明
参考使用时,需编辑“host:port”和“api token”的字段。
package main import ( "context" "fmt" "github.com/bytehouse-cloud/driver-go/sdk" ) func main() { dsn := fmt.Sprintf("tcp://host:port?secure=true&token=%v", "Your-API-Token", ) ctx := context.Background() g, err := sdk.Open(ctx, dsn) if err != nil { panic(err) } if err := g.Ping(); err != nil { panic(err) } if err := g.SendInsertQuery(ctx, "INSERT INTO my_db.animal VALUES (1,2), (3,4)"); err != nil { panic(err) } }
说明
参考使用时,需编辑“host:port”和“api token”的字段。
package main import ( "context" "fmt" "github.com/bytehouse-cloud/driver-go/sdk" ) func main() { dsn := fmt.Sprintf("tcp://host:port?secure=true&token=%v", "Your-API-Token", ) ctx := context.Background() g, err := sdk.Open(ctx, dsn) if err != nil { panic(err) } if err := g.Ping(); err != nil { panic(err) } qs, err := g.QueryContext(ctx, "SELECT * FROM my_db.animal") if err != nil || qs.Exception() != nil { if err != nil { panic(err) } if qs.Exception() != nil { panic(qs.Exception()) } } for { rows, ok := qs.NextRow() if !ok { break } fmt.Printf("%+v", rows) } }