Categorygithub.com/ydb-platform/ydb-go-sdk/v3
modulepackage
3.84.1
Repository: https://github.com/ydb-platform/ydb-go-sdk.git
Documentation: pkg.go.dev

# README

ydb-go-sdk - pure Go native and database/sql driver for YDB

License Release PkgGoDev tests lint Go Report Card codecov Code lines View examples Telegram WebSite PRs Welcome

Supports discovery, operation, table, query, coordination, ratelimiter, scheme, scripting and topic clients for YDB. YDB is an open-source Distributed SQL Database that combines high availability and scalability with strict consistency and ACID transactions. YDB was created primarily for OLTP workloads and supports some OLAP scenarious.

Supported Go Versions

ydb-go-sdk supports all Go versions supported by the official Go Release Policy. That is, the latest two versions of Go (or more, but with no guarantees for extra versions).

Versioning Policy

ydb-go-sdk comply to guidelines from SemVer2.0.0 with an several exceptions.

Installation

go get -u github.com/ydb-platform/ydb-go-sdk/v3

Example Usage

  • connect to YDB
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
if err != nil {
    log.Fatal(err)
}
  • execute SELECT query with Query service client
// Do retry operation on errors with best effort
err := db.Query().Do( // Do retry operation on errors with best effort
   ctx, // context manage exiting from Do
   func(ctx context.Context, s query.Session) (err error) { // retry operation
   	streamResult, err := s.Query(ctx, `SELECT 42 as id, "myStr" as myStr;`))
   	if err != nil {
   		return err // for auto-retry with driver
   	}
   	defer func() { _ = streamResult.Close(ctx) }() // cleanup resources
   	for s, err := range streamResult.ResultSets(ctx) {
   		if err != nil {
   			return err
   		}
   		for row, err := range rs.Rows(ctx) {
   			if err != nil {
   				return err
   			}
   			type myStruct struct {
   				id  uint64 `sql:"id"`
   				str string `sql:"myStr"`
   			}
   			var s myStruct
   			if err = row.ScanStruct(&s); err != nil {
   				return err // generally scan error not retryable, return it for driver check error
   			}
   		}
   	}

   	return nil
   },
   query.WithIdempotent(),
)
if err != nil {
   log.Fatal(err)
}
  • usage with database/sql (see additional docs in SQL.md )
import (
    "context"
    "database/sql"
    "log"

    _ "github.com/ydb-platform/ydb-go-sdk/v3"
)

...

db, err := sql.Open("ydb", "grpc://localhost:2136/local")
if err != nil {
    log.Fatal(err)
}
defer db.Close() // cleanup resources
var (
    id    int32
    myStr string
)
row := db.QueryRowContext(context.TODO(), `SELECT 42 as id, "my string" as myStr`)
if err = row.Scan(&id, &myStr); err != nil {
    log.Printf("select failed: %v", err)
    return
}
log.Printf("id = %d, myStr = \"%s\"", id, myStr)

More examples of usage placed in examples directory.

Credentials

Driver implements several ways for making credentials for YDB:

  • ydb.WithAnonymousCredentials() (enabled by default unless otherwise specified)
  • ydb.WithAccessTokenCredentials("token")
  • ydb.WithStaticCredentials("user", "password"),
  • ydb.WithOauth2TokenExchangeCredentials() and ydb,WithOauth2TokenExchangeCredentialsFile(configFilePath)
  • as part of connection string, like as grpcs://user:password@endpoint/database

Another variants of credentials.Credentials object provides with external packages:

PackageTypeDescriptionLink of example usage
ydb-go-yccredentialscredentials provider for Yandex.Cloudyc.WithServiceAccountKeyFileCredentials yc.WithInternalCA yc.WithMetadataCredentials
ydb-go-yc-metadatacredentialsmetadata credentials provider for Yandex.Cloudyc.WithInternalCA yc.WithCredentials
ydb-go-sdk-auth-environcredentialscreate credentials from environydbEnviron.WithEnvironCredentials

Ecosystem of debug tools over ydb-go-sdk

Package ydb-go-sdk provide debugging over trace events in package trace. Next packages provide debug tooling:

PackageTypeDescriptionLink of example usage
ydb-go-sdk-zaplogginglogging ydb-go-sdk events with zap packageydbZap.WithTraces
ydb-go-sdk-zerologlogginglogging ydb-go-sdk events with zerolog packageydbZerolog.WithTraces
ydb-go-sdk-logruslogginglogging ydb-go-sdk events with logrus packageydbLogrus.WithTraces
ydb-go-sdk-prometheusmetricsprometheus wrapper over ydb-go-sdk/v3/metricsydbPrometheus.WithTraces
ydb-go-sdk-opentracingtracingOpenTracing plugin for trace internal ydb-go-sdk callsydbOpentracing.WithTraces
ydb-go-sdk-oteltracingOpenTelemetry plugin for trace internal ydb-go-sdk callsydbOtel.WithTraces

Environment variables

ydb-go-sdk supports next environment variables which redefines default behavior of driver

NameTypeDefaultDescription
YDB_SSL_ROOT_CERTIFICATES_FILEstringpath to certificates file
YDB_LOG_SEVERITY_LEVELstringquietseverity logging level of internal driver logger. Supported: trace, debug, info, warn, error, fatal, quiet
YDB_LOG_DETAILSstring.*regexp for lookup internal logger logs
GRPC_GO_LOG_VERBOSITY_LEVELintegerset to 99 to see grpc logs
GRPC_GO_LOG_SEVERITY_LEVELstringset to info to see grpc logs

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
GRPCConn casts *ydb.Driver to grpc.ClientConnInterface for executing unary and streaming RPC over internal driver balancer.
IsOperationError reports whether any error is an operation error with one of passed codes.
IsOperationErrorAlreadyExistsError checks whether given err is an operation error with code AlreadyExistsError.
IsOperationErrorNotFoundError checks whether given err is an operation error with code NotFoundError.
IsOperationErrorOverloaded checks whether given err is an operation error with code Overloaded.
IsOperationErrorSchemeError checks whether given err is an operation error with code SchemeError.
IsOperationErrorTransactionLocksInvalidated checks does err a TLI issue nolint:nonamedreturns.
IsOperationErrorUnavailable checks whether given err is an operation error with code Unavailable.
IsRatelimiterAcquireError checks whether given err is an ratelimiter acquire error.
IsTimeoutError checks whether given err is a some timeout error (context, transport or operation).
IsTransportError checks whether given err is a transport (grpc) error.
IsYdbError reports when given error is and ydb error (transport, operation or internal driver error).
IterateByIssues helps to iterate over internal issues of operation error.
MergeOptions concatentaes provided options to one cumulative value.
No description provided by the author
No description provided by the author
New connects to database and return driver runtime holder Deprecated: use ydb.Open instead.
Open connects to database by DSN and return driver runtime holder DSN accept Driver string like "grpc[s]://{endpoint}/{database}[?param=value]" See sugar.DSN helper for make dsn from endpoint and database nolint:nonamedreturns.
OperationError returns operation error description.
ParamsBuilder used for create query arguments instead of tons options.
RegisterDsnParser registers DSN parser for ydb.Open and sql.Open driver constructors Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental.
ToRatelimiterAcquireError casts given err to ratelimiter.AcquireError.
TransportError checks when given error is a transport error and returns description of transport error.
UnregisterDsnParser unregisters DSN parser by key Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental.
No description provided by the author
With collects additional configuration options.
No description provided by the author
WithAnonymousCredentials force to make requests withou authentication.
WithApplicationName add provided application name to all api requests.
No description provided by the author
No description provided by the author
WithCertificate appends certificate to TLS config root certificates.
WithCertificatesFromFile appends certificates by filepath to TLS config root certificates.
WithCertificatesFromPem appends certificates from pem-encoded data to TLS config root certificates.
WithConnectionString accept Driver string like grpc[s]://{endpoint}/{database}[?param=value] Warning: WithConnectionString will be removed at next major release (Driver string will be required string param of ydb.Open).
WithConnectionTTL defines duration for parking idle connections.
WithCreateCredentialsFunc add callback funcion to provide requests credentials.
WithCredentials in conjunction with Driver.With function prohibit reuse of conn pool.
WithDatabase defines database option Warning: use ydb.Open with required Driver string parameter instead For making Driver string from endpoint+database+secure - use sugar.DSN().
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithDialTimeout sets timeout for establishing new Driver to cluster Default dial timeout is config.DefaultDialTimeout.
No description provided by the author
WithDiscoveryInterval sets interval between cluster discovery calls.
WithEndpoint defines endpoint option Warning: use ydb.Open with required Driver string parameter instead For making Driver string from endpoint+database+secure - use sugar.DSN().
No description provided by the author
WithIgnoreTruncated disables errors on truncated flag.
WithInsecure defines secure option.
WithLazyTx enables lazy transactions in query service client Lazy transaction means that begin call will be noop and first execute creates interactive transaction with given transaction settings Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental.
WithLogger add enables logging for selected tracing events.
WithMinTLSVersion set minimum TLS version acceptable for connections.
WithNodeAddressMutator applies mutator for node addresses from discovery.ListEndpoints response Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental.
No description provided by the author
WithOauth2TokenExchangeCredentials adds credentials that exchange token using OAuth 2.0 token exchange protocol: https://www.rfc-editor.org/rfc/rfc8693.
WithOauth2TokenExchangeCredentialsFile adds credentials that exchange token using OAuth 2.0 token exchange protocol: https://www.rfc-editor.org/rfc/rfc8693 Config file must be a valid json file Fields of json file grant-type: [string] Grant type option (default: "urn:ietf:params:oauth:grant-type:token-exchange") res: [string | list of strings] Resource option (optional) aud: [string | list of strings] Audience option for token exchange request (optional) scope: [string | list of strings] Scope option (optional) requested-token-type: [string] Requested token type option (default: "urn:ietf:params:oauth:token-type:access_token") subject-credentials: [creds_json] Subject credentials options (optional) actor-credentials: [creds_json] Actor credentials options (optional) token-endpoint: [string] Token endpoint Fields of creds_json (JWT): type: [string] Token source type.
WithOperationCancelAfter returns a copy of parent context in which YDB operation cancel after parameter is set to d.
WithOperationTimeout returns a copy of parent context in which YDB operation timeout parameter is set to d.
WithPanicCallback specified behavior on panic Warning: WithPanicCallback must be defined on start of all options (before `WithTrace{Driver,Table,Scheme,Scripting,Coordination,Ratelimiter}` and other options) If not defined - panic would not intercept with driver.
No description provided by the author
WithQueryConfigOption collects additional configuration options for query.Client.
No description provided by the author
WithRatelimiterOptions returns reatelimiter option.
No description provided by the author
WithRequestType returns a copy of parent context with custom request type Deprecated: use meta.WithRequestType instead.
WithRetryBudget sets retry budget for all calls of all retryers.
WithSecure defines secure option Warning: use ydb.Open with required Driver string parameter instead For making Driver string from endpoint+database+secure - use sugar.DSN().
WithSessionPoolCreateSessionTimeout set timeout for new session creation process in table.Client.
WithSessionPoolDeleteTimeout set timeout to gracefully close deleting session in table.Client.
WithSessionPoolIdleThreshold defines interval for idle sessions.
WithSessionPoolKeepAliveMinSize set minimum sessions should be keeped alive in table.Client Deprecated: use WithApplicationName instead.
WithSessionPoolKeepAliveTimeout set timeout of keep alive requests for session in table.Client Deprecated: use WithApplicationName instead.
WithSessionPoolSessionIdleTimeToLive limits maximum time to live of idle session If idleTimeToLive is less than or equal to zero then sessions will not be closed by idle.
WithSessionPoolSessionUsageLimit set max count for use session.
WithSessionPoolSizeLimit set max size of internal sessions pool in table.Client.
No description provided by the author
WithTableConfigOption collects additional configuration options for table.Client.
No description provided by the author
WithTLSConfig replaces older TLS config Warning: all early TLS config changes (such as WithCertificate, WithCertificatesFromFile, WithCertificatesFromPem, WithMinTLSVersion, WithTLSSInsecureSkipVerify) will be lost.
WithTLSSInsecureSkipVerify applies InsecureSkipVerify flag to TLS config.
WithTraceCoordination returns coordination trace option.
WithTraceDatabaseSQL adds configured discovery tracer to Driver.
WithTraceDiscovery adds configured discovery tracer to Driver.
WithTraceDriver appends trace.Driver into driver traces.
WithTraceID returns a copy of parent context with traceID Deprecated: use meta.WithTraceID instead.
WithTraceQuery appends trace.Query into query traces.
WithTraceRatelimiter returns ratelimiter trace option.
WithTraceRetry appends trace.Retry into retry traces.
WithTraceScheme returns scheme trace option.
WithTraceScripting scripting trace option.
WithTraceTable appends trace.Table into table traces.
WithTraceTopic adds configured discovery tracer to Driver.
No description provided by the author
WithUserAgent add provided user agent value to all api requests Deprecated: use WithApplicationName instead.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Version reports current version of sdk.

# Structs

Driver type provide access to YDB service clients.

# Interfaces

Connection interface provide access to YDB service clients Interface and list of clients may be changed in the future Deprecated: use Driver instance instead.
Error is an interface of error which reports about error code and error name.
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
Option contains configuration values for Driver.
No description provided by the author