Implementation:4paradigm OpenMLDB Go SDK Driver
| Knowledge Sources | |
|---|---|
| Domains | SDK, Database |
| Last Updated | 2026-10-12 12:00 GMT |
Overview
Go SQL driver implementing the standard database/sql interface for connecting to and querying an OpenMLDB cluster via HTTP.
Description
The Go SDK Driver registers the openmldb driver with Go's database/sql package, enabling standard Go SQL operations against an OpenMLDB API server. It parses the DSN (Data Source Name) in the format openmldb://host:port/db?mode=offsync|offasync|online, creates connections, and routes SQL queries via HTTP POST to the OpenMLDB API server. The driver implements driver.Driver, driver.DriverContext, and driver.Connector interfaces.
Usage
Import this driver when building Go applications that need to query OpenMLDB for feature computation. The driver is compatible with standard Go database/sql patterns, so existing SQL code works with minimal changes. Note: this Go SDK has been moved to github.com/4paradigm/openmldb-go-sdk in newer versions.
Code Reference
Source Location
- Repository: OpenMLDB
- File: go/driver.go
Signature
// init registers the "openmldb" driver with database/sql
func init()
// parseDsn parses the connection string
func parseDsn(dsn string) (host string, db string, mode queryMode, err error)
// Open implements driver.Driver
func (driver) Open(name string) (interfaces.Conn, error)
// OpenConnector implements driver.DriverContext
func (driver) OpenConnector(name string) (interfaces.Connector, error)
// Connect implements driver.Connector
func (c connecter) Connect(ctx context.Context) (interfaces.Conn, error)
// Driver implements driver.Connector
func (connecter) Driver() interfaces.Driver
Import
import (
"database/sql"
_ "github.com/4paradigm/openmldb/go" // registers the driver
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| dsn | string | Yes | offasync|online |
| host | string | Yes | Parsed from DSN: API server host:port |
| db | string | Yes | Parsed from DSN: database name |
| mode | queryMode | No | Query mode: offsync (default), offasync, or online
|
Outputs
| Name | Type | Description |
|---|---|---|
| driver.Conn | interfaces.Conn | Connection object for executing queries |
| connecter | interfaces.Connector | Connector with connection pooling support |
Usage Examples
Basic Connection and Query
package main
import (
"database/sql"
"fmt"
_ "github.com/4paradigm/openmldb/go" // side-effect import registers driver
)
func main() {
// Open connection — mode=online for online feature serving
db, err := sql.Open("openmldb", "openmldb://127.0.0.1:9080/demo_db?mode=online")
if err != nil {
panic(err)
}
defer db.Close()
// Ping to verify connection
if err := db.Ping(); err != nil {
panic(err)
}
// Execute a deployed query
rows, err := db.Query("SELECT * FROM demo")
if err != nil {
panic(err)
}
defer rows.Close()
// Iterate results
for rows.Next() {
var col1 string
var col2 int64
rows.Scan(&col1, &col2)
fmt.Printf("%s: %d\n", col1, col2)
}
}
DSN Parsing
// DSN format: openmldb://host:port/db?mode=offsync|offasync|online
// Examples:
// openmldb://localhost:9080/mydb?mode=online — online serving
// openmldb://localhost:9080/mydb?mode=offsync — synchronous offline (default)
// openmldb://localhost:9080/mydb?mode=offasync — asynchronous offline
// parseDsn returns:
// host = "localhost:9080"
// db = "mydb"
// mode = ModeOnline / ModeOffsync / ModeOffasync
Related Pages
Implements Principle
- Principle:4paradigm_OpenMLDB_Database_Driver_Implementation
- Principle:4paradigm_OpenMLDB_SQL_Feature_Engineering