本文介紹如何通過Python SDK建立Table Store的資料表。
注意事項
建立資料表後,請等待資料表載入完成後再進行資料操作,否則資料操作會失敗,這個過程通常需要幾秒鐘。
前提條件
方法說明
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[])範例程式碼
以下範例程式碼建立了一張test_table表,該表包含1個 String類型的主鍵。
# 建立資料表至少需要添加一個主鍵
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模式高效能執行個體支援設定資料表的預留讀寫輸送量為非零值)
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)