Go Client Reference

The Avatica Go client is an Avatica driver for Go’sdatabase/sqlpackage.

It also works with the Phoenix Query Server from the ApachePhoenix project, as the Phoenix Query Server uses Avatica under thehood.

Getting Started

If you are using Go 1.10 and below, install using dep:

  1. $ dep ensure -add github.com/apache/calcite-avatica-go

If you are using Go 1.11 and above, install using Go modules:

  1. $ go get github.com/apache/calcite-avatica-go

Usage

The Avatica Go driver implements Go’s database/sql/driver interface, so, import Go’sdatabase/sql package and the driver:

  1. import "database/sql"
  2. import _ "github.com/apache/calcite-avatica-go/v4"
  3. db, err := sql.Open("avatica", "http://localhost:8765")

Then simply use the database connection to query some data, for example:

  1. rows := db.Query("SELECT COUNT(*) FROM test")

DSN (Data Source Name)

The DSN has the following format (optional parts are marked by square brackets):

  1. http://address:port[/schema][?parameter1=value&...parameterN=value]

In other words, the scheme (http), address and port are mandatory, but the schema and parameters are optional.

schema

The schema path sets the default schema to use for this connection. For example, if you set it to myschema,then executing the query SELECT FROM my_table will have the equivalence of SELECT FROM myschema.my_table.If schema is set, you can still work on tables in other schemas by supplying a schema prefix:SELECT * FROM myotherschema.my_other_table.

The parameters references the options used by the Java implementation as much as possible.The following parameters are supported:

authentication

The authentication type to use when authenticating against Avatica. Valid values are BASIC for HTTP Basic authentication,DIGEST for HTTP Digest authentication, and SPNEGO for Kerberos with SPNEGO authentication.

avaticaUser

The user to use when authenticating against Avatica. This parameter is required if authentication is BASIC or DIGEST.

avaticaPassword

The password to use when authenticating against Avatica. This parameter is required if authentication is BASIC or DIGEST.

principal

The Kerberos principal to use when authenticating against Avatica. It should be in the form primary/instance@realm, wherethe instance is optional. This parameter is required if authentication is SPNEGO and you want the driver to perform theKerberos login.

keytab

The path to the Kerberos keytab to use when authenticating against Avatica. This parameter is required if authenticationis SPNEGO and you want the driver to perform the Kerberos login.

krb5Conf

The path to the Kerberos configuration to use when authenticating against Avatica. This parameter is required if authenticationis SPNEGO and you want the driver to perform the Kerberos login.

krb5CredentialsCache

The path to the Kerberos credential cache file to use when authenticating against Avatica. This parameter is required ifauthentication is SPNEGO and you have logged into Kerberos already and want the driver to use the existing credentials.

location

The location will be set as the location of unserialized time.Time values. It must be a valid timezone.If you want to use the local timezone, use Local. By default, this is set to UTC.

maxRowsTotal

The maxRowsTotal parameter sets the maximum number of rows to return for a given query. By default, this is set to-1, so that there is no limit on the number of rows returned.

frameMaxSize

The frameMaxSize parameter sets the maximum number of rows to return in a frame. Depending on the number of rowsreturned and subject to the limits of maxRowsTotal, a query result set can contain rows in multiple frames. Theseadditional frames are then fetched on a as-needed basis. frameMaxSize allows you to control the number of rowsin each frame to suit your application’s performance profile. By default this is set to -1, so that there is no limiton the number of rows in a frame.

transactionIsolation

Setting transactionIsolation allows you to set the isolation level for transactions using the connection. The valueshould be a positive integer analogous to the transaction levels defined by the JDBC specification. The default valueis 0, which means transactions are not supported. This is to deal with the fact that Calcite/Avatica works withmany types of backends, with some backends having no transaction support. If you are using Apache Phoenix 4.7 onwards,we recommend setting it to 4, which is the maximum isolation level supported.

The supported values for transactionIsolation are:

ValueJDBC ConstantDescription
0noneTransactions are not supported
1TRANSACTION_READ_UNCOMMITTEDDirty reads, non-repeatable reads and phantom reads may occur.
2TRANSACTION_READ_COMMITTEDDirty reads are prevented, but non-repeatable reads and phantom reads may occur.
4TRANSACTION_REPEATABLE_READDirty reads and non-repeatable reads are prevented, but phantom reads may occur.
8TRANSACTION_SERIALIZABLEDirty reads, non-repeatable reads, and phantom reads are all prevented.

time.Time support

The following datatypes are automatically converted to and from time.Time:TIME, DATE and TIMESTAMP.

It is important to understand that Avatica and the underlying database ignores the timezone. If you save a time.Timeto the database, the timezone is ignored and vice-versa. This is why you need to make sure the location parameterin your DSN is set to the same value as the location of the time.Time values you are inserting into the database.

We recommend using UTC, which is the default value of location.

Apache Phoenix Error Codes

The Go client comes with support for retrieving the error code when an error occurs. This is extremely useful whenyou want to take specific action when a particular type of error occurs.

If the error returned is a ResponseError, the Name field on the error will return the appropriateApache Phoenix error code:

  1. _, err := db.Exec("SELECT * FROM table_that_does_not_exist") // Query undefined table
  2. // First, assert the error type
  3. perr, ok := err.(avatica.ResponseError)
  4. // If it cannot be asserted
  5. if !ok {
  6. // Error was not an Avatica ResponseError
  7. }
  8. // Print the Apache Phoenix error code
  9. fmt.Println(perr.Name) // Prints: table_undefined

Version Compatibility

Driver VersionPhoenix VersionCalcite-Avatica Version
3.x.x>= 4.8.0>= 1.11.0