Created
September 10, 2024 14:21
-
-
Save yannick/e31fd9b259e80f776bde7ca1be355d67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/ClickHouse/clickhouse-go/v2" | |
"github.com/vmihailenco/msgpack/v5" | |
) | |
// Example struct that matches the schema of your ClickHouse table | |
type Example struct { | |
ID uint32 `msgpack:"id"` | |
Name string `msgpack:"name"` | |
Age uint32 `msgpack:"age"` | |
} | |
func main() { | |
// Connect to ClickHouse | |
conn, err := clickhouse.Open(&clickhouse.Options{ | |
Addr: []string{"localhost:9000"}, | |
Auth: clickhouse.Auth{ | |
Database: "default", | |
Username: "default", | |
Password: "", | |
}, | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
ctx := context.Background() | |
// Create an example record | |
exampleRecord := &Example{ | |
ID: 1, | |
Name: "John Doe", | |
Age: 30, | |
} | |
// Marshal the record to binary MsgPack format | |
binaryData, err := msgpack.Marshal(exampleRecord) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Insert the binary MsgPack data into ClickHouse | |
query := "INSERT INTO test FORMAT MsgPack" | |
if err := conn.Exec(ctx, query, binaryData); err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Data inserted successfully") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment