建立資料表
更新時間:
Copy as MD
通過 Python SDK建立Table Store資料表,按需配置表結構、表選項、二級索引和資料加密。
使用說明
建立資料表後,需等待資料表載入完成再進行資料操作,否則操作會失敗。載入過程通常需要幾秒鐘。
前提條件
方法說明
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[], sse_spec)
範例程式碼
以下樣本建立了一張名為 test_table 的資料表,包含 1 個 STRING 類型的主鍵列。
# 資料表至少需要 1 個主鍵列
schema_of_primary_key = [('id', 'STRING')]
# 構造表結構資訊
table_meta = TableMeta('test_table', schema_of_primary_key)
# 構造表配置資訊
table_options = TableOptions(time_to_live=-1, max_version=1, max_time_deviation=86400, allow_update=True)
# 設定預留讀寫輸送量,預設為 0 CU
# 僅 CU 模式的高效能執行個體支援設定為非零值
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)
建立資料表時還可以進行以下可選配置。
-
添加預定義列
defined_columns = [('name', 'STRING')] # 將預定義列加入表結構資訊 table_meta = TableMeta('test_table', schema_of_primary_key, defined_columns) -
添加二級索引
# 構造二級索引列表 secondary_indexes = [ # 設定索引名稱、索引主鍵列、索引預定義列、索引類型 SecondaryIndexMeta('test_table_index', ['id', 'name'], [], index_type= SecondaryIndexType.LOCAL_INDEX) ] client.create_table(table_meta, table_options, reserved_throughput, secondary_indexes) -
設定資料加密
通過
SSESpecification類配置資料表的服務端加密(SSE)。-
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) -
BYOK 加密
說明運行代碼前需擷取使用者主要金鑰ID和RAM角色 ARN,具體操作請參見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)
-
相關文檔
该文章对您有帮助吗?