/// <summary>
        /// Puts the single row.
        /// </summary>
        /// <param name="tableName">Table name.</param>
        /// <param name="primaryKey">Primary key.</param>
        /// <param name="attributes">Attributes.</param>
        public void PutSingleRow(string tableName, PrimaryKey primaryKey, AttributeColumns attributes)
        {
            var request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE));

            request.RowPutChange.PrimaryKey = primaryKey;
            foreach (var attribute in attributes)
            {
                request.RowPutChange.AddColumn(attribute.Key, attribute.Value);
            }

            OTSClient.PutRow(request);
        }
        private void PutRow(OTSClient client, String tableName)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add(COLUMN_GID_NAME, new ColumnValue(1));
            primaryKey.Add(COLUMN_UID_NAME, new ColumnValue(101));

            var attribute = new AttributeColumns();

            attribute.Add(COLUMN_NAME_NAME, new ColumnValue("张三"));
            attribute.Add(COLUMN_MOBILE_NAME, new ColumnValue(111111111));
            attribute.Add(COLUMN_ADDRESS_NAME, new ColumnValue("中国A地"));
            attribute.Add(COLUMN_AGE_NAME, new ColumnValue(20));

            Condition cond    = new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST);
            var       request = new PutRowRequest(tableName, cond, primaryKey, attribute);

            try
            {
                client.PutRow(request);
                Console.WriteLine("PutRow success");
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("PutRow fail: {0}", e.ErrorMessage);
            }
        }
        private bool PutRow(string tableName, Int64 pk, string colName, ColumnValue colValue, ColumnCondition cc)
        {
            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue(pk));

            var attribute = new AttributeColumns();

            attribute.Add(colName, colValue);
            Condition cond = new Condition(RowExistenceExpectation.IGNORE);

            cond.ColumnCondition = cc;
            var request = new PutRowRequest(tableName, cond, primaryKey, attribute);

            bool success = true;

            try
            {
                OTSClient.PutRow(request);
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("PutRow fail: {0}", e.ErrorMessage);
                success = false;
            }
            return(success);
        }
Exemple #4
0
        public static void PutRow()
        {
            Console.WriteLine("Start put row...");
            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey
            {
                { "pk0", new ColumnValue(0) },
                { "pk1", new ColumnValue("abc") }
            };

            // 定义要写入该行的属性列
            AttributeColumns attribute = new AttributeColumns
            {
                { "col0", new ColumnValue(0) },
                { "col1", new ColumnValue("a") },
                { "col2", new ColumnValue(true) }
            };
            PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            otsClient.PutRow(request);
            Console.WriteLine("Put row succeed.");
        }
Exemple #5
0
        public static void PutRow(OTSClient otsClient)
        {
            Console.WriteLine("\n Start put row...");
            List <string> colList = new List <string>()
            {
                "0,0",
                "7,3",
                "2,8",
                "5,9",
                "10,10",
                "20,20",
                "14,18",
                "12,16",
                "15,4",
                "8,13"
            };

            for (int i = 0; i < 10; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey {
                    { Pk0, new ColumnValue(i) },
                    { Pk1, new ColumnValue("pk1value") }
                };
                AttributeColumns attribute = new AttributeColumns {
                    { Geo_type_col, new ColumnValue(colList[i]) }
                };
                PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

                otsClient.PutRow(request);
            }
            Console.WriteLine("Put row succeed.");
        }
Exemple #6
0
        public void TestString()
        {
            CreateTestTable();

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue("abcdefghijklnm"));
            var request1 = new PutRowRequest(
                "SampleTableName",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            var response1 = OTSClient.PutRow(request1);

            Assert.AreEqual(0, response1.ConsumedCapacityUnit.Read);
            Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

            var request2 = new GetRowRequest(
                "SampleTableName",
                primaryKey
                );
            var response2 = OTSClient.GetRow(request2);

            Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
            Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
            AssertColumns(primaryKey, response2.PrimaryKey);
            AssertColumns(attribute, response2.Attribute);
        }
Exemple #7
0
        public static void PutRow(OTSClient otsClient)
        {
            Console.WriteLine("\n Start put row...");
            List <string> colList = new List <string>()
            {
                "TableStore SearchIndex Sample",
                "TableStore",
                "SearchIndex",
                "Sample",
                "SearchIndex Sample",
                "TableStore Sample",
                "TableStore SearchIndex"
            };

            for (int i = 0; i < 500; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey {
                    { Pk0, new ColumnValue(i) },
                    { Pk1, new ColumnValue("pk1value") }
                };
                var colId = i % 7;
                AttributeColumns attribute = new AttributeColumns {
                    { Long_type_col, new ColumnValue(i) },
                    { Text_type_col, new ColumnValue(colList[colId]) },
                    { Keyword_type_col, new ColumnValue(colList[colId]) }
                };
                PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

                otsClient.PutRow(request);
            }
            Console.WriteLine("Put row succeed.");
        }
Exemple #8
0
        public static void PutRow(string pk1Value)
        {
            Console.WriteLine("Start put row...");
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey
            {
                { Pk1, new ColumnValue(pk1Value) },
                { Pk2, ColumnValue.AUTO_INCREMENT }
            };

            // 定义要写入该行的属性列
            AttributeColumns attribute = new AttributeColumns
            {
                { "Col1", new ColumnValue(0) }
            };
            PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            request.RowPutChange.ReturnType = ReturnType.RT_PK;

            var response = otsClient.PutRow(request);

            Console.WriteLine("Put row succeed,autoIncrement Pk value:" + response.Row.GetPrimaryKey()[Pk2].IntegerValue);
        }
        private static void PrepareData()
        {
            OTSClient otsClient = Config.GetClient();

            // 插入100条数据
            for (int i = 0; i < 100; i++)
            {
                PrimaryKey primaryKey = new PrimaryKey
                {
                    { "pk0", new ColumnValue(i) },
                    { "pk1", new ColumnValue("abc") }
                };

                // 定义要写入改行的属性列
                AttributeColumns attribute = new AttributeColumns
                {
                    { "col0", new ColumnValue(0) },
                    { "col1", new ColumnValue("a") },
                    { "col2", new ColumnValue(i % 3 != 0) }
                };
                PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

                otsClient.PutRow(request);
            }
        }
        public static void ConditionDeleteRow()
        {
            Console.WriteLine("Start delete row...");

            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(2));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            // 定义要写入改行的属性列
            AttributeColumns attribute = new AttributeColumns();

            attribute.Add("col0", new ColumnValue(2));
            attribute.Add("col1", new ColumnValue("a"));
            attribute.Add("col2", new ColumnValue(true));

            PutRowRequest putRequest = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            // 新创建一行数据
            try
            {
                otsClient.PutRow(putRequest);

                Console.WriteLine("Put row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }

            // 当col2列的值等于true的时候,允许删除
            try
            {
                // 构造条件语句:col2列的值等于true
                var condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
                condition.ColumnCondition = new RelationalCondition("col2",
                                                                    CompareOperator.EQUAL,
                                                                    new ColumnValue(true));

                // 构造删除请求
                var deleteRequest = new DeleteRowRequest(tableName, condition, primaryKey);

                // 删除满足条件的特定行
                otsClient.DeleteRow(deleteRequest);

                Console.WriteLine("Delete row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete row failed. error:{0}", ex.Message);
            }

            Console.WriteLine("Delete row succeeded.");
        }
Exemple #11
0
        public override void HandlePutError(Tuple <Condition, PrimaryKey, AttributeColumns> row, string tableName, ILog log)
        {
            try
            {
                OTSClient      otsClient   = new OTSClient(TableStoreModel.endPointPublic, TableStoreModel.accessKeyID, TableStoreModel.accessKeySecret, TableStoreModel.instanceName);
                var            request     = new PutRowRequest(tableName, row.Item1, row.Item2, row.Item3);
                PutRowResponse rowResponse = otsClient.PutRow(request);
                log.Info("数据,操作成功!");
            }
            catch (Exception ex)
            {
                //错误行重试失败,记录主键信息
                StringBuilder sbPrimaryKey = new StringBuilder();
                foreach (var item in row.Item2)
                {
                    if (item.Key == "et")//日期字段处理
                    {
                        sbPrimaryKey.Append(item.Key + ":" + item.Value.IntegerValue + "【" + TimeHelper.ConvertStringToDateTime(item.Value.IntegerValue.ToString()) + "】;");
                    }
                    else if (item.Key == "ei")//事件id二进制字段处理
                    {
                        sbPrimaryKey.Append(item.Key + ":" + ByteIntHelper.bytesToInt2(item.Value.BinaryValue, 0, 1) + ";");
                    }
                    else
                    {
                        sbPrimaryKey.Append(item.Key + ":" + item.Value.IntegerValue + ";");
                    }
                }

                //记录位置信息【属性列】
                StringBuilder sbAttributeColumns = new StringBuilder();
                foreach (var item in row.Item3)
                {
                    switch (item.Key)
                    {
                    case "ep":
                        //事件参数字段暂不做处理
                        //byte[] ep = item.Value.BinaryValue;
                        break;

                    case "t":
                        sbAttributeColumns.Append(item.Key + ":" + item.Value.IntegerValue + "【" + TimeHelper.ConvertStringToDateTime(item.Value.IntegerValue.ToString()) + "】;");
                        break;

                    case "l":
                        byte[] lbyte = item.Value.BinaryValue;
                        Dictionary <string, int> dictionary = ByteIntHelper.GetLocationByByte(lbyte);
                        foreach (var dic in dictionary)
                        {
                            sbAttributeColumns.Append(dic.Key + ":" + dic.Value + ";");
                        }
                        break;
                    }
                }

                log.Error(string.Format("错误行重试PUT失败:" + ex.Message + "--参数信息:PrimaryKey:{0},AttributeColumns{1}", sbPrimaryKey.ToString(), sbAttributeColumns.ToString()));
            }
        }
Exemple #12
0
        public void TestConditionExpectNotExist()
        {
            CreateTestTable();

            var primaryKey = new PrimaryKey();

            primaryKey.Add("PK0", new ColumnValue("ABC"));
            primaryKey.Add("PK1", new ColumnValue(123));

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(new byte[] { 0x20, 0x21, 0x23, 0x24 }));
            {
                var request1 = new PutRowRequest(
                    "SampleTableName",
                    new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST),
                    primaryKey,
                    attribute
                    );

                var response1 = OTSClient.PutRow(request1);
                Assert.AreEqual(1, response1.ConsumedCapacityUnit.Read);
                Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

                var request2 = new GetRowRequest(
                    "SampleTableName",
                    primaryKey
                    );
                var response2 = OTSClient.GetRow(request2);
                Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
                Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
                AssertColumns(primaryKey, response2.PrimaryKey);
                AssertColumns(attribute, response2.Attribute);
            }
            {
                var request1 = new PutRowRequest(
                    "SampleTableName",
                    new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST),
                    primaryKey,
                    attribute
                    );

                try
                {
                    OTSClient.PutRow(request1);
                    Assert.Fail();
                }
                catch (OTSServerException e)
                {
                    Assert.AreEqual("/PutRow", e.APIName);
                    Assert.AreEqual(403, (int)e.HttpStatusCode);
                    Assert.AreEqual("OTSConditionCheckFail", e.ErrorCode);
                    Assert.AreEqual("Condition check failed.", e.ErrorMessage);
                    Assert.NotNull(e.RequestID);
                }
            }
        }
        public void TestColumnsToGet()
        {
            CreateTable();

            var primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue("ABC") },
                { "PK1", new ColumnValue(123) }
            };

            var attribute = new AttributeColumns
            {
                { "Col0", new ColumnValue(123) },
                { "Col1", new ColumnValue("ABC") },
                { "Col2", new ColumnValue(new byte[] { 0x20, 0x21, 0x23, 0x24 }) }
            };

            var request1 = new PutRowRequest(
                TestTableName,
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            var response1 = OTSClient.PutRow(request1);

            Assert.AreEqual(0, response1.ConsumedCapacityUnit.Read);
            Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

            var request2 = new GetRowRequest(
                TestTableName,
                primaryKey,
                new HashSet <string>()
            {
                "Col0", "Col2"
            }
                );

            var response2 = OTSClient.GetRow(request2);

            Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
            Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
            AssertColumns(primaryKey, response2.PrimaryKey);


            var attributeToExpect = new AttributeColumns
            {
                { "Col0", new ColumnValue(123) },
                { "Col2", new ColumnValue(new byte[] { 0x20, 0x21, 0x23, 0x24 }) }
            };

            AssertColumns(attributeToExpect, response2.Attribute);

            DeleteTable();
        }
        public void TestEmptyPrimaryKey()
        {
            CreateTable();
            var primaryKey = new PrimaryKey();

            var attribute = new AttributeColumns
            {
                { "Col0", new ColumnValue(true) }
            };

            var request1 = new PutRowRequest(
                TestTableName,
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            try
            {
                OTSClient.PutRow(request1);
                Assert.Fail();
            }
            catch (OTSServerException e)
            {
                Assert.AreEqual("/PutRow", e.APIName);
                Assert.AreEqual(400, (int)e.HttpStatusCode);
                Assert.AreEqual("OTSParameterInvalid", e.ErrorCode);
                Assert.AreEqual("Cell data broken, empty PK.", e.ErrorMessage);
                Assert.NotNull(e.RequestID);
            }

            var request2 = new GetRowRequest(
                TestTableName,
                primaryKey
                );

            try
            {
                OTSClient.GetRow(request2);
                Assert.Fail();
            }
            catch (OTSServerException e)
            {
                Assert.AreEqual("/GetRow", e.APIName);
                Assert.AreEqual(400, (int)e.HttpStatusCode);
                Assert.AreEqual("OTSParameterInvalid", e.ErrorCode);
                Assert.AreEqual("Cell data broken, empty PK.", e.ErrorMessage);
                Assert.NotNull(e.RequestID);
            }

            DeleteTable();
        }
Exemple #15
0
        public void TestEmptyPrimaryKey()
        {
            var primaryKey = new PrimaryKey();

            var attribute = new AttributeColumns();

            attribute.Add("Col0", new ColumnValue(true));
            var request1 = new PutRowRequest(
                "SampleTableName",
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            try
            {
                OTSClient.PutRow(request1);
                Assert.Fail();
            }
            catch (OTSServerException e)
            {
                Assert.AreEqual("/PutRow", e.APIName);
                Assert.AreEqual(400, (int)e.HttpStatusCode);
                Assert.AreEqual("OTSParameterInvalid", e.ErrorCode);
                Assert.AreEqual("The number of primary key columns must be in range: [1, 4].", e.ErrorMessage);
                Assert.NotNull(e.RequestID);
            }

            var request2 = new GetRowRequest(
                "SampleTableName",
                primaryKey
                );

            try
            {
                OTSClient.GetRow(request2);
                Assert.Fail();
            }
            catch (OTSServerException e)
            {
                Assert.AreEqual("/GetRow", e.APIName);
                Assert.AreEqual(400, (int)e.HttpStatusCode);
                Assert.AreEqual("OTSParameterInvalid", e.ErrorCode);
                Assert.AreEqual("The number of primary key columns must be in range: [1, 4].", e.ErrorMessage);
                Assert.NotNull(e.RequestID);
            }
        }
Exemple #16
0
        public override void HandlePutError(Tuple <Condition, PrimaryKey, AttributeColumns> row, string tableName)
        {
            try
            {
                OTSClient      otsClient   = OTSHelper._oTSClient_Location;
                var            request     = new PutRowRequest(tableName, row.Item1, row.Item2, row.Item3);
                PutRowResponse rowResponse = otsClient.PutRow(request);
                log.Info("数据,操作成功!");
            }
            catch (Exception ex)
            {
                //错误行重试失败,记录主键信息
                StringBuilder sbPrimaryKey = new StringBuilder();
                foreach (var item in row.Item2)
                {
                    if (item.Key == "t")
                    {
                        sbPrimaryKey.Append(item.Key + ":" + item.Value.IntegerValue + "【" + TimeHelper.ConvertStringToDateTime(item.Value.IntegerValue.ToString()) + "】;");
                    }
                    else
                    {
                        sbPrimaryKey.Append(item.Key + ":" + item.Value.IntegerValue + ";");
                    }
                }

                //记录位置信息【属性列】
                StringBuilder sbAttributeColumns = new StringBuilder();
                foreach (var item in row.Item3)
                {
                    if (item.Key == "l")
                    {
                        byte[] lbyte = item.Value.BinaryValue;
                        Dictionary <string, int> dictionary = ByteIntHelper.GetLocationByByte(lbyte);
                        foreach (var dic in dictionary)
                        {
                            sbAttributeColumns.Append(dic.Key + ":" + dic.Value + ";");
                        }
                    }
                }

                log.Error(string.Format("错误行重试PUT失败:" + ex.Message + "--参数信息:PrimaryKey:{0},AttributeColumns{1}", sbPrimaryKey.ToString(), sbAttributeColumns.ToString()));
            }
        }
        public void TestDouble()
        {
            CreateTable();

            var primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue("ABC") },
                { "PK1", new ColumnValue(123) }
            };

            var attribute = new AttributeColumns
            {
                { "Col0", new ColumnValue(123.321) }
            };

            var request1 = new PutRowRequest(
                TestTableName,
                new Condition(RowExistenceExpectation.IGNORE),
                primaryKey,
                attribute
                );

            var response1 = OTSClient.PutRow(request1);

            Assert.AreEqual(0, response1.ConsumedCapacityUnit.Read);
            Assert.AreEqual(1, response1.ConsumedCapacityUnit.Write);

            var request2 = new GetRowRequest(
                TestTableName,
                primaryKey
                );

            var response2 = OTSClient.GetRow(request2);

            Assert.AreEqual(1, response2.ConsumedCapacityUnit.Read);
            Assert.AreEqual(0, response2.ConsumedCapacityUnit.Write);
            AssertColumns(primaryKey, response2.PrimaryKey);
            AssertColumns(attribute, response2.Attribute);

            DeleteTable();
        }
Exemple #18
0
        /// <summary>
        /// 向主表中写入数据(自动同步到索引表)
        /// </summary>
        public static void PutRow()
        {
            Console.WriteLine("Start put row...");
            OTSClient  otsClient  = Config.GetClient();
            PrimaryKey primaryKey = new PrimaryKey
            {
                { Pk1, new ColumnValue("abc") },
                { Pk2, new ColumnValue("edf") }
            };

            // 定义要写入改行的属性列
            AttributeColumns attribute = new AttributeColumns
            {
                { Col1, new ColumnValue("Col1Value") },
                { Col2, new ColumnValue("Col2Value") }
            };
            PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            otsClient.PutRow(request);
            Console.WriteLine("Put row succeed.");
        }
Exemple #19
0
        private Tuple <List <PrimaryKey>, List <AttributeColumns> > PutRows()
        {
            List <PrimaryKey>       primaryKeys = new List <PrimaryKey>();
            List <AttributeColumns> columns     = new List <AttributeColumns>();
            var schema = new PrimaryKeySchema
            {
                { "pk1", ColumnValueType.String }
            };

            CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0));

            for (int i = 0; i < batchCount; i++)
            {
                var pk = new PrimaryKey
                {
                    { "pk1", new ColumnValue(i.ToString()) }
                };

                primaryKeys.Add(pk);

                var attr = new AttributeColumns
                {
                    { "attr", new ColumnValue("attr_value" + i) }
                };

                columns.Add(attr);
            }

            for (int i = 0; i < primaryKeys.Count; i++)
            {
                var putRequest = new PutRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE));
                putRequest.RowPutChange.PrimaryKey = primaryKeys[i];
                putRequest.RowPutChange.AddColumns(columns[i]);
                OTSClient.PutRow(putRequest);
            }

            return(new Tuple <List <PrimaryKey>, List <AttributeColumns> >(primaryKeys, columns));
        }
Exemple #20
0
        private bool PutRow(string tableName, Int64 pk, string colName, ColumnValue colValue, IColumnCondition cc)
        {
            var primaryKey = new PrimaryKey
            {
                { "PK0", new ColumnValue(pk) }
            };

            var attribute = new AttributeColumns
            {
                { colName, colValue }
            };
            Condition cond = new Condition(RowExistenceExpectation.IGNORE)
            {
                ColumnCondition = cc
            };

            var request = new PutRowRequest(tableName, cond)
            {
                RowPutChange = new RowPutChange(tableName, primaryKey)
            };

            request.RowPutChange.AddColumns(attribute);

            bool success = true;

            try
            {
                OTSClient.PutRow(request);
            }
            catch (OTSServerException e)
            {
                Console.WriteLine("PutRow fail: {0}", e.ErrorMessage);
                success = false;
            }
            return(success);
        }
        public void PutSingleRow(string tableName, PrimaryKey primaryKey, AttributeColumns attribute)
        {
            var request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            OTSClient.PutRow(request);
        }
        public static void ConditionBatchWriteRow()
        {
            Console.WriteLine("Start batch write row...");

            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(3));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            // 定义要写入改行的属性列
            AttributeColumns attribute = new AttributeColumns();

            attribute.Add("col0", new ColumnValue(0));
            attribute.Add("col1", new ColumnValue("a"));
            attribute.Add("col2", new ColumnValue(true));

            PutRowRequest request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            // 新创建一行数据
            try
            {
                otsClient.PutRow(request);

                Console.WriteLine("Put row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }

            // 当col0列的值不等于5的时候,允许再次put row,覆盖掉原值,预期成功
            try
            {
                // 构造条件语句:col0列的值不等于5
                var condition = new Condition(RowExistenceExpectation.IGNORE);
                condition.ColumnCondition = new RelationalCondition("col0",
                                                                    CompareOperator.NOT_EQUAL,
                                                                    new ColumnValue(5));

                // 构造col2列的值
                var attr1 = new AttributeColumns();
                attr1.Add("col2", new ColumnValue(false));

                // 构造批量写请求
                var rowChange = new RowChanges(tableName);
                rowChange.AddPut(condition, primaryKey, attr1);

                var batchWriteRequest = new BatchWriteRowRequest();
                batchWriteRequest.Add(tableName, rowChange);

                // 批量写数据
                otsClient.BatchWriteRow(batchWriteRequest);

                Console.WriteLine("Batch write row succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Batch write row failed. error:{0}", ex.Message);
            }
        }
        public static void ConditionPutRow()
        {
            Console.WriteLine("Start put row...");

            PrepareTable();
            OTSClient otsClient = Config.GetClient();

            // 定义行的主键,必须与创建表时的TableMeta中定义的一致
            PrimaryKey primaryKey = new PrimaryKey();

            primaryKey.Add("pk0", new ColumnValue(0));
            primaryKey.Add("pk1", new ColumnValue("abc"));

            // 定义要写入改行的属性列
            AttributeColumns attribute = new AttributeColumns();

            attribute.Add("col0", new ColumnValue(0));
            attribute.Add("col1", new ColumnValue("a"));
            attribute.Add("col2", new ColumnValue(true));

            PutRowRequest request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute);

            // 不带condition时put row,预期成功
            try
            {
                otsClient.PutRow(request);

                Console.WriteLine("Put row succeeded.");
            } catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }

            // 当col0列的值不等于5的时候,允许再次put row,覆盖掉原值,预期成功
            try
            {
                request.Condition.ColumnCondition = new RelationalCondition("col0",
                                                                            CompareOperator.NOT_EQUAL,
                                                                            new ColumnValue(5));
                otsClient.PutRow(request);

                Console.WriteLine("Put row succeeded.");
            } catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }

            // 当col0列的值等于5的时候,允许再次put row,覆盖掉原值,预期失败
            try
            {
                // 新增条件:col0列的值等于5
                request.Condition.ColumnCondition = new RelationalCondition("col0",
                                                                            CompareOperator.EQUAL,
                                                                            new ColumnValue(5));
                otsClient.PutRow(request);

                Console.WriteLine("Put row succeeded.");
            }
            catch (OTSServerException)
            {
                // 由于条件不满足,抛出OTSServerException
                Console.WriteLine("Put row failed  because condition check failed. but expected");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put row failed. error:{0}", ex.Message);
            }
        }
        public void TestForNormal()
        {
            var schema = new PrimaryKeySchema();

            schema.Add("pk1", ColumnValueType.String);

            CreateTestTable(TestTableName, schema, new CapacityUnit(0, 0));

            var attr1 = new AttributeColumns();

            attr1.Add("attr", new ColumnValue("attr_value1"));
            var attr2 = new AttributeColumns();

            attr2.Add("attr", new ColumnValue("attr_value2"));
            var attr3 = new AttributeColumns();

            attr3.Add("attr", new ColumnValue("attr_value3"));
            {
                var pk = new PrimaryKey();
                pk.Add("pk1", new ColumnValue("1"));

                var request = new PutRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, attr1);
                OTSClient.PutRow(request);
            }
            {
                var pk = new PrimaryKey();
                pk.Add("pk1", new ColumnValue("2"));
                var request = new PutRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, attr2);
                OTSClient.PutRow(request);
            }
            {
                var pk = new PrimaryKey();
                pk.Add("pk1", new ColumnValue("3"));
                var request = new PutRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, attr3);
                OTSClient.PutRow(request);
            }

            {
                var request = new BatchGetRowRequest();
                List <PrimaryKey> primaryKeys = new List <PrimaryKey>();
                var pk1 = new PrimaryKey();
                pk1.Add("pk1", new ColumnValue("1"));

                var pk2 = new PrimaryKey();
                pk2.Add("pk1", new ColumnValue("2"));

                var pk3 = new PrimaryKey();
                pk3.Add("pk1", new ColumnValue("3"));

                primaryKeys.Add(pk1);
                primaryKeys.Add(pk2);
                primaryKeys.Add(pk3);

                request.Add(TestTableName, primaryKeys);

                var response = OTSClient.BatchGetRow(request);
                var tables   = response.RowDataGroupByTable;
                Assert.AreEqual(1, tables.Count);

                var rows = tables[TestTableName];

                Assert.AreEqual(3, rows.Count);

                AssertPrimaryKey(pk1, rows[0].PrimaryKey);
                AssertPrimaryKey(pk2, rows[1].PrimaryKey);
                AssertPrimaryKey(pk3, rows[2].PrimaryKey);

                AssertAttribute(attr1, rows[0].Attribute);
                AssertAttribute(attr2, rows[1].Attribute);
                AssertAttribute(attr3, rows[2].Attribute);
            }
        }