OtsClientCreateTable Method Aliyun Open Services SDK for .NET
创建表(Table)及其相关视图(View)。

Namespace: Aliyun.OpenServices.OpenTableService
Assembly: Aliyun.OpenServices (in Aliyun.OpenServices.dll) Version: 1.0.5290.21916
Syntax

public void CreateTable(
	TableMeta tableMeta
)

Parameters

tableMeta
Type: Aliyun.OpenServices.OpenTableServiceTableMeta
TableMeta的对象,包含表(Table)及视图(View)的结构信息。

Implements

IOtsCreateTable(TableMeta)
Exceptions

ExceptionCondition
ArgumentException

tableMeta为空引用,

- 或 -

tableMeta中的结构信息无效。

OtsException

OTS访问返回错误消息。

WebException

由于网络原因请求失败,

- 或 -

访问超时。

InvalidOperationException

返回结果解析错误。

Remarks

这一操作在OTS中提交一个创建表的请求, 但该操作成功并不代表数据表已经创建完成。请在表创建完成后再对表进行进一步的操作,比如插入数据等。 如需检查表是否创建完成,请使用ListTables方法检查表是否存在。

OTS要求视图中数据行与原表数据行一一对应,因此强烈建议视图的主键(Primary Key)列包含表的全部主键列。

Examples

下面的示例代码演示如何通过OtsClient的方法在OTS中创建一个表(Table)。
using System;
using System.Linq;
using Aliyun.OpenTableService;

namespace Aliyun.OpenServices.Samples.OpenTableService
{
    class CreateTableSample
    {
        string endpoint = "http://ots.aliyuncs.com";
        string accessId = "<your access id>";
        string accessKey = "<your access key>";

        string tableName = "contact_table";
        string viewName = "view1";

        public void CreateTable()
        {
            // 创建表结构信息。
            var tableMeta = new TableMeta(tableName);
            // 指定表的主键。
            tableMeta.PrimaryKeys.Add("uid", PrimaryKeyType.Integer);
            tableMeta.PrimaryKeys.Add("flag", PrimaryKeyType.Boolean);
            tableMeta.PrimaryKeys.Add("name", PrimaryKeyType.String);
            tableMeta.PagingKeyLength = 2;

            var viewMeta = new ViewMeta(viewName);
            viewMeta.PrimaryKeys.Add("uid", PrimaryKeyType.Integer);
            viewMeta.PrimaryKeys.Add("flag", PrimaryKeyType.Boolean);
            tableMeta.PrimaryKeys.Add("name", PrimaryKeyType.String);
            viewMeta.PrimaryKeys.Add("groupid", PrimaryKeyType.Integer);

            tableMeta.Views.Add(viewMeta);

            // 在OTS中创建一个表。
            var otsClient = new OtsClient(endpoint, accessId, accessKey);

            try
            {
                otsClient.CreateTable(tableMeta);
            }
            catch (OtsException ex)
            {
                Console.WriteLine("创建表失败。OTS异常消息:" + ex.Message);
                // RequestId和HostId可以在有问题时用于联系客服诊断异常。
                Console.WriteLine("Request ID: {0}\tHostID: {1}", ex.RequestId, ex.HostId);
            }
            catch (System.Net.WebException ex)
            {
                Console.WriteLine("创建表失败。网络异常:{0}。请检查Endpoint或网络链接。", ex.Message);
            }
        }
    }
}
See Also

Reference