Unable to connect to Dremio with ODBC in Golang

341 views Asked by At

I am new to Golang and I am trying to connect to Dremio using ODBC in Golang with Dremio host, port, username and password. The following code gives me error

2022/04/19 17:36:42 missing username and password

exit status 1

import (
    "database/sql"
    "fmt"
    "gographqlservice/graph/model"
    "log"

    _ "github.com/pinpt/go-dremio/driver"
)

const (
    host     = "dremio.xyz.abc.com"
    user     = "user1"
    password = "user_password"
    port     = 32010
)
    
func GetAsset(id string) (*model.Asset, error) {

    drminfo := fmt.Sprintf("host=%s port=%d user=%s password=%s sslmode=disable",
        host, port, user, password)

    db, err := sql.Open("dremio", drminfo)

    if err != nil {
        log.Fatal("error occurred")
        log.Fatal(err)
    }

    rows, err := db.Query("SELECT * FROM space.xyz.\"assets\" WHERE ASSET_ID = ?", id)
    if err != nil {
        log.Fatal(err)
    }

    var asset model.Asset

    defer rows.Close()
    for rows.Next() {

        // some code goes here
    }

    err = rows.Err()
    if err != nil {
        log.Fatal(err)
    }

    defer db.Close()

    return &asset, err
}
1

There are 1 answers

0
Getit On

It looks like you are using driver code from "Pinpoint" that is documented here: https://pkg.go.dev/github.com/pinpt/go-dremio#section-readme

You can see their connection string should be in the following form:

db, err := sql.Open("dremio", "https://user:[email protected]")

Your code appears to be in the form:

drminfo := fmt.Sprintf("host=%s port=%d user=%s password=%s sslmode=disable",
    host, port, user, password)

To allow the Pinpoint code to parse this connection string and pull out the user and password, try the following instead:

drminfo := fmt.Sprintf("http://%s:%s@%s",
        user, password, host)

The error message you're seeing comes from the following line in the Pinpoint code. In this case, it means they were not able to figure out the user name from the connection string provided. https://github.com/pinpt/go-dremio/blob/master/driver/driver.go#L187

if u.User == nil {
        return nil, fmt.Errorf("missing username and password")
    }