|
1 | | -# ydb-go-sdk |
| 1 | +# `ydb-go-sdk` - native Go's driver for [YDB](https://github.com/ydb-platform/ydb). |
2 | 2 |
|
3 | 3 | [](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3) |
4 | | -[](https://godoc.org/github.com/ydb-platform/ydb-go-sdk/v3) |
5 | 4 |  |
6 | 5 |  |
7 | 6 | [](https://goreportcard.com/report/github.com/ydb-platform/ydb-go-sdk/v3) |
8 | 7 | [](https://app.codecov.io/gh/ydb-platform/ydb-go-sdk) |
9 | 8 |
|
10 | | -[YDB](https://github.com/ydb-platform/ydb) native Go's driver. |
| 9 | +Supports `table`, `discovery`, `coordination`, `ratelimiter`, `scheme` and `scripting` clients for `YDB`. |
| 10 | +`YDB` is an open-source Distributed SQL Database that combines high availability and scalability with strict consistency and ACID transactions. |
11 | 11 |
|
12 | | -Supports `table`, `discovery`, `coordination`, `ratelimiter`, `scheme` and `scripting` clients for `YDB`. |
| 12 | +## Example Usage <a name="example"></a> |
13 | 13 |
|
14 | | -Simple example see in [example](example_table_test.go). More examples of usage placed in [examples](https://github.com/ydb-platform/ydb-go-examples) repository. |
| 14 | +* connect to YDB |
| 15 | + ``` |
| 16 | + db, err := ydb.New(ctx, |
| 17 | + ydb.WithConnectionString("grpcs://localhost:2135/?database=/local"), |
| 18 | + ydb.WithAnonymousCredentials(), |
| 19 | + ) |
| 20 | + if err != nil { |
| 21 | + log.Fatal(err) |
| 22 | + } |
| 23 | + ``` |
| 24 | +* execute `SELECT` query |
| 25 | + ``` |
| 26 | + const query = `SELECT 42 as id, "myStr" as myStr;` |
| 27 | + |
| 28 | + // Do retry operation on errors with best effort |
| 29 | + queryErr := db.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) { |
| 30 | + _, res, err := s.Execute(ctx, table.DefaultTxControl(), query, nil) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + defer res.Close() |
| 35 | + if err = res.NextResultSetErr(ctx); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + for res.NextRow() { |
| 39 | + var id int32 |
| 40 | + var myStr string |
| 41 | + err = res.ScanNamed(named.Required("id", &id),named.OptionalWithDefault("myStr", &myStr)) |
| 42 | + if err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | + log.Printf("id=%v, myStr='%s'\n", id, myStr) |
| 46 | + } |
| 47 | + return res.Err() // for driver retry if not nil |
| 48 | + }) |
| 49 | + if queryErr != nil { |
| 50 | + log.Fatal(queryErr) |
| 51 | + } |
| 52 | + ``` |
| 53 | +More examples of usage placed in [examples](https://github.com/ydb-platform/ydb-go-examples) repository. |
| 54 | +
|
| 55 | +## Credentials <a name="credentials"></a> |
| 56 | +
|
| 57 | +Driver contains two options for making simple `credentials.Credentials`: |
| 58 | +- `ydb.WithAnonymousCredentials()` |
| 59 | +- `ydb.WithAccessTokenCredentials("token")` |
| 60 | +
|
| 61 | +Another variants of `credentials.Credentials` object provides with external packages: |
| 62 | +
|
| 63 | +Package | Type | Description | Link of example usage |
| 64 | +--- | --- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --- |
| 65 | +[ydb-go-yc](https://github.com/ydb-platform/ydb-go-yc) | credentials | credentials provider for Yandex.Cloud | [yc.WithServiceAccountKeyFileCredentials](https://github.com/ydb-platform/ydb-go-yc/blob/master/internal/cmd/connect/main.go#L22) |
| 66 | +[yc.WithInternalCA](https://github.com/ydb-platform/ydb-go-yc/blob/master/internal/cmd/connect/main.go#L22) [yc.WithMetadataCredentials](https://github.com/ydb-platform/ydb-go-yc/blob/master/internal/cmd/connect/main.go#L24) |
| 67 | +[ydb-go-yc-metadata](https://github.com/ydb-platform/ydb-go-yc-metadata) | credentials | metadata credentials provider for Yandex.Cloud | [yc.WithInternalCA](https://github.com/ydb-platform/ydb-go-yc-metadata/blob/master/options.go#L23) [yc.WithCredentials](https://github.com/ydb-platform/ydb-go-yc-metadata/blob/master/options.go#L17) |
| 68 | +[ydb-go-sdk-auth-environ](https://github.com/ydb-platform/ydb-go-sdk-auth-environ) | credentials | create credentials from environ | [ydbEnviron.WithEnvironCredentials](https://github.com/ydb-platform/ydb-go-sdk-auth-environ/blob/master/env.go#L11) |
| 69 | +
|
| 70 | +## Ecosystem of debug tools over `ydb-go-sdk` <a name="debug"></a> |
| 71 | +
|
| 72 | +Package `ydb-go-sdk` provide debugging over trace events in package `trace`. |
| 73 | +Next packages provide debug tooling: |
| 74 | +
|
| 75 | +Package | Type | Description | Link of example usage |
| 76 | +--- | --- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --- |
| 77 | +[ydb-go-sdk-zap](https://github.com/ydb-platform/ydb-go-sdk-zap) | logging | logging ydb-go-sdk events with zap package | [ydbZap.WithTraces](https://github.com/ydb-platform/ydb-go-sdk-zap/blob/master/internal/cmd/bench/main.go#L64) |
| 78 | +[ydb-go-sdk-zerolog](https://github.com/ydb-platform/ydb-go-sdk-zap) | logging | logging ydb-go-sdk events with zerolog package | [ydbZerolog.WithTraces](https://github.com/ydb-platform/ydb-go-sdk-zerolog/blob/master/internal/cmd/bench/main.go#L47) |
| 79 | +[ydb-go-sdk-metrics](https://github.com/ydb-platform/ydb-go-sdk-metrics) | metrics | common metrics of ydb-go-sdk. Package declare interfaces such as `Registry`, `GaugeVec` and `Gauge` and use it for traces | |
| 80 | +[ydb-go-sdk-prometheus](https://github.com/ydb-platform/ydb-go-sdk-prometheus) | metrics | prometheus wrapper over [ydb-go-sdk-metrics](https://github.com/ydb-platform/ydb-go-sdk-metrics) | [ydbPrometheus.WithTraces](https://github.com/ydb-platform/ydb-go-sdk-prometheus/blob/master/internal/cmd/bench/main.go#L56) |
| 81 | +[ydb-go-sdk-opentracing](https://github.com/ydb-platform/ydb-go-sdk-opentracing) | tracing | opentracing plugin for trace internal ydb-go-sdk calls | [ydbOpentracing.WithTraces](https://github.com/ydb-platform/ydb-go-sdk-opentracing/blob/master/internal/cmd/bench/main.go#L86) |
| 82 | +
|
| 83 | +## Environment variables <a name="environ"></a> |
| 84 | +
|
| 85 | +`ydb-go-sdk` supports next environment variables which redefines default behavior of driver |
| 86 | +
|
| 87 | +Name | Type | Default | Description |
| 88 | +--- | --- | --- | --- |
| 89 | +`YDB_SSL_ROOT_CERTIFICATES_FILE` | `string` | | path to certificates file |
| 90 | +`YDB_LOG_SEVERITY_LEVEL` | `string` | `quiet` | severity logging level of internal driver logger. Supported: `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `quiet` |
| 91 | +`YDB_LOG_NO_COLOR` | `bool` | `false` | set any non empty value to disable colouring logs with internal driver logger |
| 92 | +`GRPC_GO_LOG_VERBOSITY_LEVEL` | `integer` | | set to `99` to see grpc logs |
| 93 | +`GRPC_GO_LOG_SEVERITY_LEVEL` | `string` | | set to `info` to see grpc logs |
15 | 94 |
|
16 | | -See also [CREDENTIALS.md](CREDENTIALS.md) about supported YDB credentials. |
17 | 95 |
|
18 | | -See [DEBUG.md](DEBUG.md) about supported debug tooling over `ydb-go-sdk`. |
19 | 96 |
|
20 | | -See [ENVIRON.md](ENVIRON.md) about supported environment variables of `ydb-go-sdk`. |
|
0 commit comments