使用CreateTable介面建立資料表時,需要指定資料表的結構資訊和配置資訊,高效能執行個體中的資料表還可以根據需要設定預留讀/寫輸送量。建立資料表的同時支援建立一個或者多個索引表。
建立資料表後需要幾秒鐘進行載入,在此期間對該資料表的讀/寫資料操作均會失敗。請等待資料表載入完畢後再進行資料操作。
建立資料表時必須指定資料表的主鍵。主鍵包含1個~4個主鍵列,每一個主鍵列都有名稱和類型。
前提條件
介面
"""
說明:根據指定表結構資訊建立資料表。
``table_meta``是``tablestore.metadata.TableMeta``類的執行個體,它包含資料表名稱和PrimaryKey的schema。
請參見``TableMeta``類的文檔。當建立一個資料表後,通常需要等待幾秒鐘時間使partition load完成,才能進行各種操作。
``table_options``是``tablestore.metadata.TableOptions``類的執行個體,它包含time_to_live,max_version和max_time_deviation三個參數。
``reserved_throughput``是``tablestore.metadata.ReservedThroughput``類的執行個體,表示預留讀寫輸送量。
``secondary_indexes``是一個數組,可以包含一個或多個``tablestore.metadata.SecondaryIndexMeta``類的執行個體,表示要建立的全域二級索引。
返回:無。
"""
def create_table(self, table_meta, table_options, reserved_throughput, secondary_indexes=[]):
參數
參數 | 說明 |
table_meta | 資料表的結構資訊,包括如下內容:
|
table_options | 資料表的配置資訊。更多資訊,請參見資料版本和生命週期。 配置資訊包括如下內容:
|
reserved_throughput | 為資料表配置預留讀輸送量或預留寫輸送量。 容量型執行個體中的資料表的預留讀/寫輸送量只能設定為0,不允許預留。 預設值為0,即完全隨用隨付。 單位為CU。
|
secondary_indexes | 索引表的結構資訊,每個SecondaryIndexMeta包括如下內容:
|
樣本
建立資料表(不帶索引)
建立一個有2個主鍵列,資料保留1年(60*60*24*365=31536000秒),最大版本數3,寫入時間戳記位移小於1天(86400秒),預留讀寫輸送量為(0, 0)的資料表。
# 建立主鍵列的schema,包括主鍵的個數、名稱和類型。 # 第一個PK列為整型,名稱是pk0,該列同時也是分區鍵。 # 第二個PK列為整型,名稱是pk1。其他可選的類型包括STRING和BINARY,此處使用INTEGER。 schema_of_primary_key = [('pk0', 'INTEGER'), ('pk1', 'INTEGER')] # 通過資料表名稱和主鍵列的schema建立一個tableMeta。 table_meta = TableMeta('SampleTable', schema_of_primary_key) # 建立TableOptions,資料保留31536000秒,超過後自動刪除;最大3個版本;寫入時指定的版本值和目前時間相差不能超過86400秒(即1天)。 table_options = TableOptions(31536000, 3, 86400) # 設定預留讀輸送量為0,預留寫輸送量為0。 reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) # 調用client的create_table介面,如果沒有拋出異常,則說明執行成功。 try: ots_client.create_table(table_meta, table_options, reserved_throughput) print "create table succeeded" # 如果拋出異常,則說明執行失敗,處理異常。 except Exception: print "create table failed."
詳細代碼請參見CreateTable@GitHub。
建立資料表(帶索引且索引類型為全域二級索引)
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'STRING')] defined_columns = [('i', 'INTEGER'), ('bool', 'BOOLEAN'), ('d', 'DOUBLE'), ('s', 'STRING'), ('b', 'BINARY')] table_meta = TableMeta(table_name, schema_of_primary_key, defined_columns) table_option = TableOptions(-1, 1) reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) secondary_indexes = [ SecondaryIndexMeta('index1', ['i', 's'], ['bool', 'b', 'd']), ] client.create_table(table_meta, table_option, reserved_throughput, secondary_indexes)
建立資料表(帶索引且索引類型為本地二級索引)
schema_of_primary_key = [('gid', 'INTEGER'), ('uid', 'STRING')] defined_columns = [('i', 'INTEGER'), ('bool', 'BOOLEAN'), ('d', 'DOUBLE'), ('s', 'STRING'), ('b', 'BINARY')] table_meta = TableMeta(table_name, schema_of_primary_key, defined_columns) table_option = TableOptions(-1, 1) reserved_throughput = ReservedThroughput(CapacityUnit(0, 0)) secondary_indexes = [ SecondaryIndexMeta('index1', ['gid', 's'], ['bool', 'b', 'd'],index_type= SecondaryIndexType.LOCAL_INDEX), ] client.create_table(table_meta, table_option, reserved_throughput, secondary_indexes)