# Go Client

MyScale is compatible with ClickHouse, so you can use the official Clickhouse Go client (opens new window) to access MyScale from your Go applications.

# Install

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

# Examples

# Connecting to Database

To learn how to establish a connection to the cluster, please refer to the Connection Details section.

// Replace "MYSCALE_CLUSTER_URL" with the endpoint hostname of your cluster,
// which can be found in the MyScale web console.
// An example of the hostname is "msc-ad2378cf.us-east-1.aws.myscale.com"
// Here "default" is your default database, and you can replace "default"
// with the name of your target database.
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
}

# Importing Data

Create a table with vectors:

_, 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
}

Assuming we have a set of data with a defined data structure:

Use Exec function to batch write data:

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
}

Create a vector search index:

_, 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
}

Search for vectors and release the result set:

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)
}

Output:

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

At last, close the connection:

conn.Close()
Last Updated: Tue Sep 10 2024 03:19:52 GMT+0000