# Go客户端

MyScale与ClickHouse兼容,因此您可以使用官方的ClickHouse Go客户端 (opens new window)从Go应用程序访问MyScale。

# 安装

go get -u github.com/ClickHouse/clickhouse-go/v2

# 示例

# 连接到数据库

要了解如何建立与集群的连接,请参阅连接详细信息部分。

// 将 "MYSCALE_CLUSTER_URL" 替换为集群的终端主机名,可以在 MyScale Web 控制台中找到。
// 主机名的示例是 "msc-ad2378cf.us-east-1.aws.myscale.com"
// 这里的 "default" 是默认数据库,您可以将 "default" 替换为目标数据库的名称。
conn := clickhouse.OpenDB(&clickhouse.Options{
	  Addr: []string{"MYSCALE_CLUSTER_URL"},
    Auth: clickhouse.Auth{
        Database: "default",
        Username: "YOUR_USERNAME",
        Password: "YOUR_CLUSTER_PASSWORD",
    },
    Protocol: clickhouse.HTTP,
    TLS: &tls.Config{
        InsecureSkipVerify: false,
    },
})
_, err := conn.Query("SELECT 1")
if err != nil {
    fmt.Printf("err %s", err)
    return
}

# 导入数据

创建一个带有向量的表:

_, err = conn.Exec("CREATE TABLE default.myscale_categorical_search" +
    "(" +
    "    id    UInt32," +
    "    data  Array(Float32)," +
    "    CONSTRAINT check_length CHECK length(data) = 128," +
    "    date  Date," +
    "    label Enum8('person' = 1, 'building' = 2, 'animal' = 3)" +
    ")" +
    "ORDER BY id")
if err != nil {
    fmt.Printf("err %s", err)
    return
}

假设我们有一组具有定义的数据结构的数据:

使用 Exec 函数批量写入数据:

scope, err := conn.Begin()
if err != nil {
    fmt.Printf("err %s", err)
    return
}
batch, err := scope.Prepare("INSERT INTO default.myscale_categorical_search")
if err != nil {
    fmt.Printf("err %s", err)
    return
}
for i := 0; i < len(data); i++ {
    _, err := batch.Exec(
        uint32(data[i].ID),
        data[i].Data,
        data[i].Date,
        data[i].Label,
    )
    if err != nil {
        fmt.Printf("err %s", err)
        return
    }
}
err = scope.Commit()
if err != nil {
    fmt.Printf("err %s", err)
    return
}

# 创建向量索引和搜索

创建一个向量搜索索引:

_, err = conn.Exec("ALTER TABLE default.myscale_categorical_search ADD VECTOR INDEX categorical_vector_idx data TYPE MSTG")
if err != nil {
    fmt.Printf("err %s", err)
    return
}

搜索向量并释放结果集:

rows, err := conn.Query("SELECT id, date, label, data," + "distance(data, [3.0,9,45,22,28,11,4,3,77,10,4,1,1,4,3,11,23,0," +
    "0,0,26,49,6,7,5,3,3,1,11,50,8,9,11,7,15,21,12,17,21,25,121,12,4,7,4,7,4," +
    "41,28,2,0,1,10,42,22,20,1,1,4,9,31,79,16,3,23,4,6,26,31,121,87,40,121,82," +
    "16,12,15,41,6,10,76,48,5,3,21,42,41,50,5,17,18,64,86,54,17,6,43,62,56,84," +
    "116,108,38,26,58,63,20,87,105,37,2,2,121,121,38,25,44,33,24,46,3,16,27,74," +
    "121,55,9,4]) AS dist " +
    "FROM default.myscale_categorical_search ORDER BY dist LIMIT 10")
if err != nil {
    fmt.Printf("err %s", err)
    return
}
for rows.Next() {
    var (
        id    int
        date  string
        label string
        data  []float32
        dist  float32
    )
    if err := rows.Scan(&id, &date, &label, &data, &dist); err != nil {
        fmt.Printf("err %s", err)
        return
    }
    fmt.Printf("row: id=%d, date=%s, label=%s, dist=%f\n", id, date, label, dist)
}

输出:

row: id=3, date=2024-08-11T00:00:00+08:00, label=animal, dist=252972.640625
row: id=5, date=2025-04-02T00:00:00+08:00, label=building, dist=255089.046875
row: id=9, date=2024-08-11T00:00:00+08:00, label=person, dist=255117.093750
row: id=9, date=1971-02-02T00:00:00+08:00, label=building, dist=255117.093750
row: id=2, date=2015-12-17T00:00:00+08:00, label=person, dist=255440.062500
row: id=0, date=2030-09-26T00:00:00+08:00, label=person, dist=255492.390625
row: id=1, date=1996-06-22T00:00:00+08:00, label=building, dist=255535.312500
row: id=7, date=1970-09-10T00:00:00+08:00, label=building, dist=255631.140625
row: id=4, date=1970-01-31T00:00:00+08:00, label=animal, dist=255742.671875
row: id=8, date=2007-10-26T00:00:00+08:00, label=person, dist=255818.390625

最后,关闭连接:

conn.Close()