Creating row-based tables in a database
Warning
Currently, the service only supports creating row-based tables. The Terraform provider functionality for creating column-based tables is under development.
In the Terraform context, a table is a resource. That is why, we describe database tables in independent sections (resource "yandex_ydb_table"
), specifying an internal link to the previously created database.
yandex_ydb_table
resource
Description of the Example of creating a test row-based table with three columns in an existing database:
resource "yandex_ydb_table" "test_table" {
path = "test_dir/test_table_3_col"
connection_string = yandex_ydb_database_serverless.database1.ydb_full_endpoint
column {
name = "a"
type = "Utf8"
not_null = true
}
column {
name = "b"
type = "Uint32"
not_null = true
}
column {
name = "c"
type = "Int32"
not_null = false
}
column {
name = "d"
type = "Timestamp"
}
primary_key = ["a","b"]
}
Properties of the yandex_ydb_table
resource fields:
path
: Path within the database to include the table being created. The table name is specified without the trailing slash (/
). If there is no directory to host the table, it will be created automatically.connection_string
: Path to connect to the database. It is used together with theydb_full_endpoint
parameter, which contains the full path to the database:grpcs://ydb.serverless.yandexcloud.net:2135/?database=/ru-central1/b1gv7kfcttio********/etn66ecf1qbt********
. For brevity and simplicity, you can use a link to theyandex_ydb_database_serverless
resource specifying the ID and theydb_full_endpoint
parameter, e.g.,yandex_ydb_database_serverless.database1.ydb_full_endpoint
.primary_key
: Primary key of the table. The key can be composite.
Full list of fields in the yandex_ydb_table
resource:
Field name | Type | Value | Description |
---|---|---|---|
path | string |
required |
Table path |
connection_string | string |
required |
Connection string |
column | array[column] |
required |
Column data type |
family | array[family] |
optional |
Column group |
primary_key | array[string] |
required |
Table primary key |
ttl | ttl |
optional |
TTL settings |
attributes | map[string]string |
optional |
Table attributes |
partitioning_settings | partitioning_settings |
optional |
Partitioning settings |
key_bloom_filter | boolean |
optional |
Using the Bloom filter for the primary key |
read_replicas_settings | string |
optional |
Read replication settings |
In the yandex_ydb_table
section, there are nested column
sections to describe individual column properties, such as:
Field name | Type | Description |
---|---|---|
name | string required |
Column name |
type | string required |
Column data type. YQL data types |
family | string optional |
Column group |
not_null | boolean optional Default: false |
A column cannot have the NULL data type. |
Example of a full column description:
column {
name = "column_name"
type = "Utf8"
family = "some_family"
not_null = true
}
Warning
Currently, you cannot delete a column using Terraform, you can only add one. To delete a column, first delete it at the database level (for example, using a YQL query), then remove the column from the configuration file.
You can group columns into families
DATA
: Type of storage device for column data in this family (acceptable values:ssd
,rot
(from HDD spindle rotation)).COMPRESSION
: Data codec (acceptable values:off
,lz4
).
To group columns into a family, add the family
section at the same level with the column
section:
family {
name = "my_family"
data = "ssd"
compression = "lz4"
}
family
section fields description:
Field name | Type | Description |
---|---|---|
name | string required |
Column group name |
data | string required |
Data storage device type for columns of this group |
compression | string required |
Data compression codec |
Here is an example of grouping two columns into a family:
resource "yandex_ydb_table" "test_table" {
path = "test_dir/test_table_3_col"
connection_string = yandex_ydb_database_serverless.database1.ydb_full_endpoint
column {
name = "a"
type = "Uint16"
family = "test_family_group"
not_null = true
}
column {
name = "b"
type = "Uint32"
family = "test_family_group"
not_null = true
}
family {
name = "test_family_group"
data = "ssd"
compression = "lz4"
}
}
YDB allows you to create a TTL columnDate
, Datetime
, Timestamp
, Uint32
, Uint64
, and DyNumber
.
The TTL column is configured by the following section:
ttl {
column_name = "d"
expire_interval = "PT1H" # 1 hour
unit = "seconds" # for numeric column types (non ISO 8601)
}
ttl
fields description:
Field name | Type | Description |
---|---|---|
column_name | string required |
TTL column name |
expire_interval | string required |
Interval in ISO 8601 |
unit | string optional |
It is set if the TTL column is of the numeric typeseconds , milliseconds , microseconds , and nanoseconds . |
Partitioning row-based tables
Partitioningpartitioning_settings
parameter of the yandex_ydb_table
resource.
partitioning_settings
section fields
Description of the Example:
resource "yandex_ydb_table" "test_table" {
path = "/test_dir/test_table_3_col"
connection_string = yandex_ydb_database_serverless.database1.ydb_full_endpoint
partitioning_settings {
auto_partitioning_min_partitions_count = 5
auto_partitioning_max_partitions_count = 8
auto_partitioning_partition_size_mb = 256
auto_partitioning_by_load = true
...
}
...
}
Full description of the partitioning_settings
fields:
Field name | Type | Description |
---|---|---|
uniform_partitions | number optional |
Number of pre-allocated partitions |
partition_at_keys | string optional |
Partitioning by primary key |
auto_partitioning_min_partitions_count | number optional |
Minimum possible number of partitions |
auto_partitioning_max_partitions_count | number optional |
Maximum possible number of partitions |
auto_partitioning_partition_size_mb | number optional |
Setting the value for auto-partitioning by size |
auto_partitioning_by_size_enabled | bool optional |
Enabling auto-partitioning by size (bool) which is enabled by default (true). |
auto_partitioning_by_load | bool optional |
Enabling auto-partitioning by load (bool) which is disabled by default (false). |