Crie uma tabela de dados do Tablestore com o Tablestore SDK for Python. Configure o esquema da tabela, as opções, os índices secundários e a criptografia conforme necessário.
Observações de uso
Após criar uma tabela de dados, aguarde a conclusão do carregamento antes de executar qualquer operação. Esse processo geralmente leva alguns segundos. Operações realizadas antes que a tabela esteja pronta falharão.
Pré-requisitos
Antes de começar, verifique se você:
Método
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[], sse_spec)
Exemplos
O exemplo a seguir cria uma tabela de dados chamada test_table com uma única coluna de chave primária do tipo 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)
Os exemplos a seguir mostram como definir configurações específicas ao criar uma tabela de dados.
-
Adicionar colunas predefinidas
defined_columns = [('name', 'STRING')] # Include predefined columns in the table schema. table_meta = TableMeta('test_table', schema_of_primary_key, defined_columns) -
Adicionar índices secundários
# 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) -
Configurar criptografia de dados
Use a classe
SSESpecificationpara configurar a criptografia no lado do servidor (SSE) da tabela de dados.-
Criptografia KMS
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) -
Criptografia BYOK
NotaAntes de executar este código, obtenha o ID da CMK e o ARN da função do RAM. Para obter mais informações, consulte Criptografia BYOK.
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)
-