impala

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 21 Imported by: 2

README

Golang Apache Impala Driver

project logo - gopher with impala horns

The actively supported Apache Impala driver for Go's database/sql package

This driver started as a fork of github.com/bippio/go-impala, which hasn't been updated in over four years and appears to be abandoned. Several issues have been fixed since - some quite severe. The original codebase also didn't support Go modules.

Go Reference Go Report Card Tests Coverage Status

Install

Add impala-go to your Go module:

go get github.com/sclgo/impala-go

Alternatively, see below how to use as a CLI.

Connection Parameters and DSN

The connection string uses a URL format: impala://username:password@host:port?param1=value&param2=value

Parameters:
  • auth - string. Authentication mode. Supported values: "noauth", "ldap"
  • tls - boolean. Enable TLS
  • ca-cert - The file that contains the public key certificate of the CA that signed the Impala certificate
  • batch-size - integer value (default: 1024). Maximum number of rows fetched per request
  • buffer-size- in bytes (default: 4096); Buffer size for the Thrift transport
  • mem-limit - string value (example: 3m); Memory limit for query

A string of this format can be constructed using the URL type in the net/url package.

  query := url.Values{}
  query.Add("auth", "ldap")

  u := &url.URL{
      Scheme:   "impala",
      User:     url.UserPassword(username, password),
      Host:     net.JoinHostPort(hostname, port),
      RawQuery: query.Encode(),
  }
  db, err := sql.Open("impala", u.String())

Also, you can bypass string-base data source name by using sql.OpenDB:

  opts := impala.DefaultOptions
  opts.Host = hostname
  opts.UseLDAP = true
  opts.Username = username
  opts.Password = password

  connector := impala.NewConnector(&opts)
  db, err := sql.OpenDB(connector)

Try out with as a CLI

impala-go is compatible with xo/usql - the universal SQL CLI, inspired by psql. Since impala-go is not yet included in usql by default, you need a Go 1.21+ runtime to build a bundle from source with usqlgen.

To build the CLI, run:

go run github.com/sclgo/usqlgen@latest build --import github.com/sclgo/impala-go -- -tags no_base

To connect to Impala in interactive mode, run:

./usql impala:DSN

In that command, DSN is a connection string in the format shown above. Note that the DSN itself starts with impala:.

For example, to run show databases in an Impala instance on localhost, use:

./usql impala:impala://localhost -c "show databases"

Example Go code

package main

// Simple program to list databases and the tables

import (
	"context"
	"database/sql"
	"log"

	"github.com/sclgo/impala-go"
)

func main() {
	opts := impala.DefaultOptions

	opts.Host = "localhost" // impala host
	opts.Port = "21050"

	// enable LDAP authentication:
	//opts.UseLDAP = true
	//opts.Username = "<ldap username>"
	//opts.Password = "<ldap password>"
	//
	// enable TLS
	//opts.UseTLS = true
	//opts.CACertPath = "/path/to/cacert"

	connector := impala.NewConnector(&opts)
	db := sql.OpenDB(connector)
	defer func() {
		_ = db.Close()
	}()

	ctx := context.Background()

	rows, err := db.QueryContext(ctx, "SHOW DATABASES")
	if err != nil {
		log.Fatal(err)
	}

	var name, comment string
	databases := make([]string, 0) // databases will contain all the DBs to enumerate later
	for rows.Next() {
		if err := rows.Scan(&name, &comment); err != nil {
			log.Fatal(err)
		}
		databases = append(databases, name)
	}
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
	log.Println("List of Databases", databases)

	tables, err := impala.NewMetadata(db).GetTables(ctx, "%", "%")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("List of Tables", tables)
}

Check out also an open data end-to-end demo.

Support

The library is actively tested with Impala 4.4 and 3.4. All 3.x and 4.x minor versions should work well. 2.x is also supported on a best-effort basis.

File any issues that you encounter as Github issues.

This library started as a fork of github.com/bippio/go-impala, under the MIT license. This library retains the same license.

The project logo combines the Golang Gopher from github.com/golang-samples/gopher-vector with the Apache Impala logo, licensed under the Apache 2 license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultOptions for impala driver
	DefaultOptions = Options{BatchSize: 1024, BufferSize: 4096, Port: "21050", LogOut: io.Discard}
)
View Source
var (
	// ErrNotSupported means this operation is not supported by impala driver
	ErrNotSupported = isql.ErrNotSupported
)

Functions

func NewConnector

func NewConnector(opts *Options) driver.Connector

NewConnector creates connector with specified options

Types

type AuthError added in v1.1.0

type AuthError = sasl.AuthError

AuthError indicates that there was an authentication or authorization failure. The error message documents the username that was used, if any. errors.Unwrap() returns the underlying error that was interpreted as auth. failure, if any. This error will not be top-level in the chain - earlier errors in the chain reflect the process during which the auth. error happened.

type ColumnName added in v1.1.0

type ColumnName = hive.ColumnName

ColumnName contains all attributes that identify a columns

type ConnRawAccess added in v0.2.0

type ConnRawAccess interface {
	Raw(func(driverConn any) error) error
}

ConnRawAccess exposes the Raw method of sql.Conn

type Driver

type Driver struct{}

Driver to impala

func (*Driver) Open

func (d *Driver) Open(dsn string) (driver.Conn, error)

Open creates new connection to impala using the given data source name. Implements driver.Driver. Returned error wraps any errors coming from thrift or stdlib - typically crypto or net packages. If TLS is requested, and server certificate fails validation, error chain includes *tls.CertificateVerificationError If there was an authentication error, error chain includes one of the exported auth. errors in this package.

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

OpenConnector parses name as a DSN (data source name) and returns connector with fixed options Implements driver.DriverContext

type Metadata added in v0.1.1

type Metadata struct {
	// contains filtered or unexported fields
}

Metadata exposes the schema and other metadata in an Impala instance

func NewMetadata added in v0.1.1

func NewMetadata(db *sql.DB) *Metadata

NewMetadata creates Metadata instance with the given Impala DB as data source. A new connection will be retrieved for any call. If that's an issue, use NewMetadataFromConn

func NewMetadataFromConn added in v0.2.0

func NewMetadataFromConn(conn ConnRawAccess) *Metadata

NewMetadataFromConn creates Metadata instance with the given Impala connection as data source *sql.Conn implements ConnRawAccess

func (Metadata) GetColumns added in v1.1.0

func (m Metadata) GetColumns(ctx context.Context, schemaPattern string, tableNamePattern string, columnNamePattern string) ([]ColumnName, error)

GetColumns retrieves columns that match the provided LIKE patterns

func (Metadata) GetSchemas added in v0.2.0

func (m Metadata) GetSchemas(ctx context.Context, schemaPattern string) ([]string, error)

GetSchemas retrieves schemas that match the provided LIKE pattern

func (Metadata) GetTables added in v0.1.1

func (m Metadata) GetTables(ctx context.Context, schemaPattern string, tableNamePattern string) ([]TableName, error)

GetTables retrieves tables and views that match the provided LIKE patterns

type Options

type Options struct {
	Host     string
	Port     string
	Username string
	Password string

	UseLDAP      bool
	UseTLS       bool
	CACertPath   string
	BufferSize   int
	BatchSize    int
	MemoryLimit  string
	QueryTimeout int

	LogOut io.Writer
}

Options for impala driver connection

type TableName added in v0.2.0

type TableName = hive.TableName

TableName contains all attributes that identify a table

Directories

Path Synopsis
internal
fi
Package fi provides various shorthands for testify patterns - "fi is short for testify"
Package fi provides various shorthands for testify patterns - "fi is short for testify"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL