public void CreateTestTable(string tableName, PrimaryKeySchema schema, CapacityUnit reservedThroughput, bool waitFlag = true)
        {
            var tableMeta = new TableMeta(tableName, schema);
            var request   = new CreateTableRequest(tableMeta, reservedThroughput);

            OTSClient.CreateTable(request);

            if (waitFlag)
            {
                WaitForTableReady();
            }
        }
        private static void PrepareTable()
        {
            // 创建表
            OTSClient otsClient = Config.GetClient();

            IList <string> tables = otsClient.ListTable(new ListTableRequest()).TableNames;

            if (tables.Contains(TableName))
            {
                return;
            }


            PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
            {
                { "pk0", ColumnValueType.Integer },
                { "pk1", ColumnValueType.String }
            };

            TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);

            CapacityUnit       reservedThroughput = new CapacityUnit(0, 0);
            CreateTableRequest request            = new CreateTableRequest(tableMeta, reservedThroughput);

            otsClient.CreateTable(request);
        }
示例#3
0
        public void CreateMultiAutoIncrementColumnTableTest_ShouldFailed()
        {
            var schema = new PrimaryKeySchema
            {
                { "PK0", ColumnValueType.String },
                { "PK1", ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT },
                { "PK2", ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT }
            };
            var tableMeta = new TableMeta(TestTableName, schema);

            var tableOptions = new TableOptions
            {
                MaxVersions = 10,
                TimeToLive  = -1
            };
            var reservedThroughput = new CapacityUnit(0, 0);

            var request = new CreateTableRequest(tableMeta, reservedThroughput)
            {
                TableOptions = tableOptions
            };

            try{
                OTSClient.CreateTable(request);
                WaitForTableReady();
            }catch (Exception e) {
                Assert.IsTrue(e.Message.Contains("AUTO_INCREMENT primary key count must <= 1"));
            }
        }
示例#4
0
        public static void TableOperations()
        {
            // 创建表
            OTSClient otsClient = Config.GetClient();

            {
                Console.WriteLine("Start create table...");
                PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
                {
                    { "pk0", ColumnValueType.Integer },
                    { "pk1", ColumnValueType.String }
                };
                TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);

                CapacityUnit       reservedThroughput = new CapacityUnit(0, 0);
                CreateTableRequest request            = new CreateTableRequest(tableMeta, reservedThroughput);
                otsClient.CreateTable(request);

                Console.WriteLine("Table is created: " + TableName);
            }

            //// 更新表
            //{
            //    Thread.Sleep(60 * 1000); // 每次更新表需要至少间隔1分钟
            //    Console.WriteLine("Start update table...");
            //    CapacityUnit reservedThroughput = new CapacityUnit(0, 0); // 将预留CU调整为0,0
            //    UpdateTableRequest request = new UpdateTableRequest(TableName, reservedThroughput);
            //    UpdateTableResponse response = otsClient.UpdateTable(request);
            //    Console.WriteLine("LastIncreaseTime: " + response.ReservedThroughputDetails.LastIncreaseTime);
            //    Console.WriteLine("LastDecreaseTime: " + response.ReservedThroughputDetails.LastDecreaseTime);
            //    Console.WriteLine("NumberOfDecreaseToday: " + response.ReservedThroughputDetails.LastIncreaseTime);
            //    Console.WriteLine("ReadCapacity: " + response.ReservedThroughputDetails.CapacityUnit.Read);
            //    Console.WriteLine("WriteCapacity: " + response.ReservedThroughputDetails.CapacityUnit.Write);
            //}

            // 描述表
            {
                Console.WriteLine("Start describe table...");
                DescribeTableRequest  request  = new DescribeTableRequest(TableName);
                DescribeTableResponse response = otsClient.DescribeTable(request);
                Console.WriteLine("LastIncreaseTime: " + response.ReservedThroughputDetails.LastIncreaseTime);
                Console.WriteLine("LastDecreaseTime: " + response.ReservedThroughputDetails.LastDecreaseTime);
                Console.WriteLine("NumberOfDecreaseToday: " + response.ReservedThroughputDetails.LastIncreaseTime);
                Console.WriteLine("ReadCapacity: " + response.ReservedThroughputDetails.CapacityUnit.Read);
                Console.WriteLine("WriteCapacity: " + response.ReservedThroughputDetails.CapacityUnit.Write);
            }

            //// 删除表
            //{
            //    Console.WriteLine("Start delete table...");
            //    DeleteTableRequest request = new DeleteTableRequest(TableName);
            //    otsClient.DeleteTable(request);
            //    Console.WriteLine("Table is deleted.");
            //}
        }
示例#5
0
        public void CreateTestTable()
        {
            var primaryKeys = new PrimaryKeySchema();

            primaryKeys.Add("PK0", ColumnValueType.String);
            primaryKeys.Add("PK1", ColumnValueType.Integer);

            var tableMeta          = new TableMeta("SampleTableName", primaryKeys);
            var reservedThroughput = new CapacityUnit(0, 0);
            var request            = new CreateTableRequest(tableMeta, reservedThroughput);

            OTSClient.CreateTable(request);

            WaitForTableReady();
        }
        private void CreateTable(String tableName)
        {
            foreach (var tableItem in OTSClient.ListTable(new ListTableRequest()).TableNames)
            {
                OTSClient.DeleteTable(new DeleteTableRequest(tableItem));
            }
            var primaryKeySchema = new PrimaryKeySchema();

            primaryKeySchema.Add("PK0", ColumnValueType.Integer);
            var tableMeta          = new TableMeta(tableName, primaryKeySchema);
            var reservedThroughput = new CapacityUnit(0, 0);
            var request            = new CreateTableRequest(tableMeta, reservedThroughput);
            var response           = OTSClient.CreateTable(request);

            WaitForTableReady();
        }
        public void CreateTableAndDelete()
        {
            string tableName = "SampleTableName";

            var primaryKeys = new PrimaryKeySchema
            {
                { "PK0", ColumnValueType.String },
                { "PK1", ColumnValueType.Integer }
            };

            var tableOption = new TableOptions
            {
                MaxVersions = 1,
                TimeToLive  = -1
            };

            var tableMeta          = new TableMeta(tableName, primaryKeys);
            var reservedThroughput = new CapacityUnit(0, 0);
            var request1           = new CreateTableRequest(tableMeta, reservedThroughput)
            {
                TableOptions = tableOption
            };

            var response1 = OTSClient.CreateTable(request1);

            var request2  = new ListTableRequest();
            var response2 = OTSClient.ListTable(request2);

            Assert.IsTrue(response2.TableNames.Contains(tableName));

            Thread.Sleep(1000);
            var request3  = new DescribeTableRequest(tableName);
            var response3 = OTSClient.DescribeTable(request3);

            Assert.AreEqual(tableName, response3.TableMeta.TableName);
            Assert.AreEqual(primaryKeys, response3.TableMeta.PrimaryKeySchema);
            Assert.AreEqual(reservedThroughput.Read, response3.ReservedThroughputDetails.CapacityUnit.Read);
            Assert.AreEqual(reservedThroughput.Write, response3.ReservedThroughputDetails.CapacityUnit.Write);


            OTSClient.DeleteTable(new DeleteTableRequest(tableName));

            var request4  = new ListTableRequest();
            var response4 = OTSClient.ListTable(request4);

            Assert.IsFalse(response4.TableNames.Contains(tableName));
        }
        //static void Main(string[] args)
        //{
        //    OTSClient otsClient = Config.GetClient();
        //    //DeleteSearchIndex(otsClient);
        //    //DeleteTable(otsClient);

        //    //创建一张TableStore表
        //    CreateTable(otsClient);
        //    //在TableStore表上创建一个索引表
        //    CreateSearchIndex(otsClient);

        //    //Wait searchIndex load success
        //    Console.WriteLine("wait searchIndex load success");
        //    Thread.Sleep(3 * 1000);

        //    ListSearchIndex(otsClient);
        //    CreateSearchIndexWithIndexSort(otsClient);
        //    DescribeSearchIndex(otsClient);
        //    PutRow(otsClient);

        //    //等待索引数据同步成功
        //    WaiteAllDataSyncSuccess(otsClient, 7);

        //    //MatchAll Query
        //    MatchAllQuery(otsClient);

        //    //MatchQuery
        //    MatchQuery(otsClient);

        //    //MatchPhraseQuery
        //    MatchPhraseQuery(otsClient);

        //    //RangeQuery
        //    RangeQuery(otsClient);

        //    //PrefixQuery
        //    PrefixQuery(otsClient);

        //    //TermQuery
        //    TermQuery(otsClient);

        //    //WildcardQuery
        //    WildcardQuery(otsClient);

        //    //BoolQuery
        //    BoolQuery(otsClient);

        //    Console.ReadLine();
        //}

        public static void CreateTable(OTSClient otsClient)
        {
            Console.WriteLine("\n Start create table...");
            PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
            {
                { Pk0, ColumnValueType.Integer },
                { Pk1, ColumnValueType.String }
            };
            TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);

            CapacityUnit       reservedThroughput = new CapacityUnit(0, 0);
            CreateTableRequest request            = new CreateTableRequest(tableMeta, reservedThroughput);

            otsClient.CreateTable(request);

            Console.WriteLine("Table is created: " + TableName);
        }
示例#9
0
        //static void Main(string[] args)
        //{
        //    Console.WriteLine("GlobalIndexSample");

        //    CreateTableWithGlobalIndex();

        //    CreateGlobalIndex();

        //    PutRow();

        //    GetRangeFromIndexTable();

        //    DeleteGlobalIndex();

        //    DeleteTable();

        //    Console.ReadLine();

        //}

        /// <summary>
        /// 创建一个带二级索引的表
        /// </summary>
        public static void CreateTableWithGlobalIndex()
        {
            //建主表,两列Pk: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);
        }
        private void CreateTable(OTSClient client, String tableName)
        {
            foreach (var tableItem in client.ListTable(new ListTableRequest()).TableNames)
            {
                client.DeleteTable(new DeleteTableRequest(tableItem));
            }
            var primaryKeySchema = new PrimaryKeySchema();

            primaryKeySchema.Add(COLUMN_GID_NAME, ColumnValueType.Integer);
            primaryKeySchema.Add(COLUMN_UID_NAME, ColumnValueType.Integer);
            var tableMeta          = new TableMeta(tableName, primaryKeySchema);
            var reservedThroughput = new CapacityUnit(0, 0);
            var request            = new CreateTableRequest(tableMeta, reservedThroughput);
            var response           = OTSClient.CreateTable(request);

            // 创建表只是提交请求,OTS创建表需要一段时间,这里是简单的sleep,请根据实际逻辑修改
            WaitForTableReady();
        }
示例#11
0
        private void CreateAutoIncrementColumnTable(PrimaryKeySchema schema)
        {
            var tableMeta = new TableMeta(TestTableName, schema);

            var tableOptions = new TableOptions
            {
                MaxVersions = 10,
                TimeToLive  = -1
            };
            var reservedThroughput = new CapacityUnit(0, 0);

            var request = new CreateTableRequest(tableMeta, reservedThroughput)
            {
                TableOptions = tableOptions
            };

            OTSClient.CreateTable(request);

            WaitForTableReady();
        }
        public void UpdateTableAndThenDescribe()
        {
            string tableName   = "update_table_and_then_describe";
            var    primaryKeys = new PrimaryKeySchema
            {
                { "PK0", ColumnValueType.String },
                { "PK1", ColumnValueType.Integer }
            };

            var tableMeta          = new TableMeta(tableName, primaryKeys);
            var reservedThroughput = new CapacityUnit(0, 0);

            var tableOption = new TableOptions
            {
                MaxVersions = 1,
                TimeToLive  = -1
            };


            var request1 = new CreateTableRequest(tableMeta, reservedThroughput)
            {
                TableOptions = tableOption
            };

            var response1 = OTSClient.CreateTable(request1);

            WaitBeforeUpdateTable();

            var request2 = new UpdateTableRequest(tableName)
            {
                TableOptions = tableOption
            };

            var response2 = OTSClient.UpdateTable(request2);

            OTSClient.DeleteTable(new DeleteTableRequest(tableName));

            Assert.AreEqual(0, response2.ReservedThroughputDetails.NumberOfDecreasesToday);
            Assert.AreEqual(0, response2.ReservedThroughputDetails.CapacityUnit.Read);
            Assert.AreEqual(0, response2.ReservedThroughputDetails.CapacityUnit.Write);
        }
        public void CreateTableAndDelete()
        {
            var primaryKeys = new PrimaryKeySchema();

            primaryKeys.Add("PK0", ColumnValueType.String);
            primaryKeys.Add("PK1", ColumnValueType.Integer);

            var tableMeta          = new TableMeta("SampleTableName", primaryKeys);
            var reservedThroughput = new CapacityUnit(0, 0);
            var request1           = new CreateTableRequest(tableMeta, reservedThroughput);
            var response1          = OTSClient.CreateTable(request1);

            var request2  = new ListTableRequest();
            var response2 = OTSClient.ListTable(request2);

            Assert.AreEqual(new List <string>()
            {
                "SampleTableName"
            }, response2.TableNames);

            Thread.Sleep(1000);
            var request3  = new DescribeTableRequest("SampleTableName");
            var response3 = OTSClient.DescribeTable(request3);

            Assert.AreEqual("SampleTableName", response3.TableMeta.TableName);
            Assert.AreEqual(primaryKeys, response3.TableMeta.PrimaryKeySchema);
            Assert.AreEqual(reservedThroughput.Read, response3.ReservedThroughputDetails.CapacityUnit.Read);
            Assert.AreEqual(reservedThroughput.Write, response3.ReservedThroughputDetails.CapacityUnit.Write);


            OTSClient.DeleteTable(new DeleteTableRequest("SampleTableName"));

            var request4  = new ListTableRequest();
            var response4 = OTSClient.ListTable(request4);

            Assert.AreEqual(new List <string>()
            {
            }, response4.TableNames);
        }
示例#14
0
        //static void Main(string[] args)
        //{
        //    Console.WriteLine("AutoIncrementSample");

        //    //创建一个带自增列的表
        //    CreateTableWithAutoIncrementPk();

        //    //写入10行,自增列Pk2将
        //    for (int i = 0; i < 10; i++)
        //    {
        //        PutRow(i.ToString());
        //    }

        //    Console.ReadLine();

        //}

        /// <summary>
        /// 创建一个带自增列的表
        /// </summary>
        private static void CreateTableWithAutoIncrementPk()
        {
            OTSClient otsClient = Config.GetClient();

            IList <string> tables = otsClient.ListTable(new ListTableRequest()).TableNames;

            if (tables.Contains(TableName))
            {
                return;
            }

            PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
            {
                { Pk1, ColumnValueType.String },
                //指定Pk2为自增列主键
                { Pk2, ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT }
            };
            TableMeta tableMeta = new TableMeta(TableName, primaryKeySchema);

            CapacityUnit       reservedThroughput = new CapacityUnit(0, 0);
            CreateTableRequest request            = new CreateTableRequest(tableMeta, reservedThroughput);

            otsClient.CreateTable(request);
        }
        public void UpdateTableAndThenDescribe()
        {
            var primaryKeys = new PrimaryKeySchema();

            primaryKeys.Add("PK0", ColumnValueType.String);
            primaryKeys.Add("PK1", ColumnValueType.Integer);

            var tableMeta          = new TableMeta("update_table_and_then_describe", primaryKeys);
            var reservedThroughput = new CapacityUnit(0, 0);
            var request1           = new CreateTableRequest(tableMeta, reservedThroughput);
            var response1          = OTSClient.CreateTable(request1);

            WaitBeforeUpdateTable();

            var request2 = new UpdateTableRequest(
                "update_table_and_then_describe",
                new CapacityUnit(1, 1)
                );
            var response2 = OTSClient.UpdateTable(request2);

            Assert.AreEqual(0, response2.ReservedThroughputDetails.NumberOfDecreasesToday);
            Assert.AreEqual(100, response2.ReservedThroughputDetails.CapacityUnit.Read);
            Assert.AreEqual(100, response2.ReservedThroughputDetails.CapacityUnit.Write);
        }
示例#16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        protected static string SetTable(Type type)
        {
            #region 初始化缓存
            if (!tableNames.ContainsKey(type))
            {
                foreach (var attr in type.CustomAttributes)
                {
                    if (attr.AttributeType == typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute))
                    {
                        tableNames.TryAdd(type, attr.ConstructorArguments[0].Value.ToString());
                        break;
                    }
                }
            }
            #endregion

            var name = tableNames.ContainsKey(type) ? tableNames[type] : type.Name.ToLower();

            #region 分析表结构
            var pkSchema = new PrimaryKeySchema();
            foreach (PropertyInfo info in rft.GetPropertyList(type))
            {
                if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.KeyAttribute)) == null)
                {
                    continue;
                }
                else if (info.PropertyType == typeof(int))
                {
                    pkSchema.Add(info.Name, ColumnValueType.Integer);
                }
                else if (info.PropertyType == typeof(string))
                {
                    pkSchema.Add(info.Name, ColumnValueType.String);
                }
                else if (info.PropertyType == typeof(byte[]))
                {
                    pkSchema.Add(info.Name, ColumnValueType.Binary);
                }
                else
                {
                    throw new Exception("不支持当前类型的主键");
                }
            }
            ;
            #endregion

            #region 生成表结构
            if (tableKey.ContainsKey(name))
            {
                #region 对比已有结构
                tableKey[name].ForEach(key =>
                {
                    pkSchema.ForEach(tmp =>
                    {
                        if (key.Item1 == tmp.Item1 && (key.Item2 != tmp.Item2 || key.Item3 != tmp.Item3))
                        {
                            log.Fatal("Table structure is inconsistent, table name:" + name);
                            return;
                        }
                    });
                });
                #endregion
            }
            else
            {
                #region 创建新表
                //通过表名和主键列的schema创建一个tableMeta
                var client    = new OTSClient(OtsConfig);
                var tableMeta = new TableMeta(name, pkSchema);
                // 设定预留读吞吐量为0,预留写吞吐量为0
                var reservedThroughput = new CapacityUnit(0, 0);

                try
                {
                    // 构造CreateTableRequest对象
                    var reqCreateTable = new CreateTableRequest(tableMeta, reservedThroughput);
                    // 调用client的CreateTable接口,如果没有抛出异常,则说明成功,否则失败
                    client.CreateTable(reqCreateTable);

                    // 生成查询索引
                    var reqCreateSearchIndex = new CreateSearchIndexRequest(name, name);
                    var fieldSchemas         = new List <FieldSchema>();
                    foreach (PropertyInfo info in rft.GetPropertyList(type))
                    {
                        if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.CompareAttribute)) == null)
                        {
                            continue;
                        }
                        else if (info.PropertyType == typeof(int) || info.PropertyType == typeof(long))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.LONG)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(string))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.TEXT)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(double))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.DOUBLE)
                            {
                                index = true
                            });
                        }
                        else if (info.PropertyType == typeof(bool))
                        {
                            fieldSchemas.Add(new FieldSchema(info.Name, FieldType.BOOLEAN)
                            {
                                index = true
                            });
                        }
                    }
                    ;
                    if (fieldSchemas.Count > 0)
                    {
                        reqCreateSearchIndex.IndexSchame = new IndexSchema()
                        {
                            FieldSchemas = fieldSchemas
                        };
                        client.CreateSearchIndex(reqCreateSearchIndex);
                        log.Info("Create table succeeded, table name:" + name);
                    }
                }
                // 处理异常
                catch (Exception ex)
                {
                    Console.WriteLine("Create table failed, exception:{0}", ex.Message);
                }
                #endregion
            }
            #endregion

            return(name);
        }