Create a Tablestore data table using the Tablestore SDK for Python. Configure the table schema, options, secondary indexes, and encryption settings as needed.
Usage notes
After creating a data table, wait for it to finish loading before performing any operations. This typically takes a few seconds. Operations attempted before the table is ready will fail.
Prerequisites
Before you begin, ensure that you have:
Method
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[], sse_spec)
Examples
The following example creates a data table named test_table with a single primary key column of type STRING.
# A data table requires at least one primary key column.
schema_of_primary_key = [('id', 'STRING')]
# Define the table schema.
table_meta = TableMeta('test_table', schema_of_primary_key)
# Configure table options.
table_options = TableOptions(time_to_live=-1, max_version=1, max_time_deviation=86400, allow_update=True)
# Set the reserved throughput. Default is 0 CU.
# A non-zero value only takes effect for high-performance instances in CU mode.
reserved_throughput = ReservedThroughput(CapacityUnit(0,0))
try:
client.create_table(table_meta, table_options, reserved_throughput)
print("Create table succeeded.")
except Exception as e:
print("Create table failed. %s" % e)
The following examples show how to configure specific settings when creating a data table.
-
Add predefined columns
defined_columns = [('name', 'STRING')] # Include predefined columns in the table schema. table_meta = TableMeta('test_table', schema_of_primary_key, defined_columns) -
Add secondary indexes
# Define the secondary index list. secondary_indexes = [ # Specify the index name, primary key columns, predefined columns, and index type. SecondaryIndexMeta('test_table_index', ['id', 'name'], [], index_type= SecondaryIndexType.LOCAL_INDEX) ] client.create_table(table_meta, table_options, reserved_throughput, secondary_indexes) -
Configure data encryption
Use the
SSESpecificationclass to configure server-side encryption (SSE) for the data table.-
KMS encryption
sse_specification = SSESpecification(enable=True, key_type=SSEKeyType.SSE_KMS_SERVICE, key_id=None, role_arn=None) client.create_table(table_meta, table_option, reserved_throughput, sse_spec=sse_specification) -
BYOK encryption
NoteBefore running this code, get the CMK ID and the RAM role ARN. For more information, see BYOK encryption.
key_id = "key-hzz6*****************" role_arn = "acs:ram::1705************:role/tabletorebyok" sse_specification = SSESpecification(enable=True, key_type=SSEKeyType.SSE_BYOK, key_id=key_id, role_arn=role_arn) client.create_table(table_meta, table_option, reserved_throughput, sse_spec=sse_specification)
-