使用CreateTable接口创建数据表时,需要指定数据表的结构信息和配置信息,高性能实例中的数据表还可以根据需要设置预留读/写吞吐量。创建数据表的同时支持创建一个或者多个索引表。
说明
- 创建数据表后需要几秒钟进行加载,在此期间对该数据表的读/写数据操作均会失败。请等待数据表加载完毕后再进行数据操作。
- 创建数据表时必须指定数据表的主键。主键包含1个~4个主键列,每一个主键列都有名称和类型。
前提条件
接口
/// <summary>
/// 根据表信息(包含数据表名称、主键的定义和预留读写吞吐量)创建数据表。
/// </summary>
/// <param name="request">请求参数</param>
/// <returns>CreateTable的返回,此返回实例是空的,不包含具体信息。
/// </returns>
public CreateTableResponse CreateTable(CreateTableRequest request);
/// <summary>
/// CreateTable的异步形式。
/// </summary>
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request);
参数
参数 | 说明 |
---|---|
tableMeta | 数据表的结构信息,包括如下内容:
|
tableOptions | 数据表的配置信息,详情请参见数据版本和生命周期。 配置信息包括如下内容:
|
reservedThroughput | 为数据表配置预留读吞吐量或预留写吞吐量。 容量型实例中的数据表的预留读/写吞吐量只能设置为0,不允许预留。 默认值为0,即完全按量计费。 单位为CU。
|
indexMetas | 索引表的结构信息,每个indexMeta都包括如下内容:
|
示例
- 创建数据表(不带索引)
创建一个有2个主键列,预留读/写吞吐量为(0, 0)的数据表。
//创建主键列的schema,包括PK的个数、名称和类型。 //第一个PK列为整数,名称是pk0,这个同时也是分区键。 //第二个PK列为字符串,名称是pk1。 var primaryKeySchema = new PrimaryKeySchema(); primaryKeySchema.Add("pk0", ColumnValueType.Integer); primaryKeySchema.Add("pk1", ColumnValueType.String); //通过表名和主键列的schema创建一个tableMeta。 var tableMeta = new TableMeta("SampleTable", primaryKeySchema); //设置预留读吞吐量为0,预留写吞吐量为0。 var reservedThroughput = new CapacityUnit(0, 0); try { //构造CreateTableRequest对象。 var request = new CreateTableRequest(tableMeta, reservedThroughput); //调用client的CreateTable接口,如果没有抛出异常,则说明执行成功。 otsClient.CreateTable(request); Console.WriteLine("Create table succeeded."); } //如果抛出异常,则说明失败,处理异常。 catch (Exception ex) { Console.WriteLine("Create table failed, exception:{0}", ex.Message); }
详细代码请参见CreateTable@GitHub。
- 创建数据表(带索引)
public static void CreateTableWithGlobalIndex() { //创建数据表,两列主键为Pk1、Pk2,预定义列为Col1、Col2。 //创建索引表,索引表中Col1放Pk0。 OTSClient otsClient = Config.GetClient(); Console.WriteLine("Start create table with globalIndex..."); PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema { { Pk1, ColumnValueType.String }, { Pk2, ColumnValueType.String } }; TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema); tableMeta.DefinedColumnSchema = new DefinedColumnSchema { { Col1, DefinedColumnType.STRING}, { Col2, DefinedColumnType.STRING} }; IndexMeta indexMeta = new IndexMeta(IndexName); indexMeta.PrimaryKey = new List<string>() { Col1 }; indexMeta.DefinedColumns = new List<string>() { Col2 }; //indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX; //indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX; List<IndexMeta> indexMetas = new List<IndexMeta>() { }; indexMetas.Add(indexMeta); CapacityUnit reservedThroughput = new CapacityUnit(0, 0); CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas); otsClient.CreateTable(request); Console.WriteLine("Table is created: " + TableName); }