public override void HandlePutError(Tuple <Condition, PrimaryKey, AttributeColumns> row, string tableName) { try { OTSClient otsClient = OTSHelper._oTSClient_Event; 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())); } }
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", RelationalCondition.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."); }
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 GetRowTest() { CreateTable(); var otsClient = OTSClient; var primaryKey = new PrimaryKey(); primaryKey.Add("PK0", new ColumnValue("ABC")); primaryKey.Add("PK1", new ColumnValue(123)); var attribute = new AttributeColumns(); attribute.Add("IntAttr0", new ColumnValue(12345)); attribute.Add("StringAttr1", new ColumnValue("ABC")); attribute.Add("DoubleAttr2", new ColumnValue(3.14)); attribute.Add("BooleanAttr3", new ColumnValue(true)); var putRowRequest = new PutRowRequest( "SampleTable", new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute ); var putRowResponse = otsClient.PutRow(putRowRequest); var getRowRequest = new GetRowRequest( "SampleTable", primaryKey ); var getRowResponse = otsClient.GetRow(getRowRequest); System.Console.WriteLine("GetRow CU Consumed: Read {0} Write {0}", getRowResponse.ConsumedCapacityUnit.Read, getRowResponse.ConsumedCapacityUnit.Write); var pk0 = getRowResponse.PrimaryKey["PK0"]; System.Console.WriteLine("PrimaryKey PK0 Value {0}", pk0.StringValue); var pk1 = getRowResponse.PrimaryKey["PK1"]; System.Console.WriteLine("PrimaryKey PK1 Value {0}", pk1.IntegerValue); var attr0 = getRowResponse.Attribute["IntAttr0"]; System.Console.WriteLine("Attribute IntAttr0 Value {0}", attr0.IntegerValue); var attr1 = getRowResponse.Attribute["StringAttr1"]; System.Console.WriteLine("Attribute StringAttr1 Value {0}", attr1.StringValue); var attr2 = getRowResponse.Attribute["DoubleAttr2"]; System.Console.WriteLine("Attribute DoubleAttr2 Value {0}", attr2.DoubleValue); var attr3 = getRowResponse.Attribute["BooleanAttr3"]; System.Console.WriteLine("Attribute BooleanAttr3 Value {0}", attr2.BooleanValue); DeleteTable(); }
/// <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); }
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(); }
public void PutRowWithAutoIncrementColumnTableTest() { var schema = new PrimaryKeySchema { { "PK0", ColumnValueType.String }, { "PK1", ColumnValueType.Integer, PrimaryKeyOption.AUTO_INCREMENT } }; CreateAutoIncrementColumnTable(schema); var otsClient = OTSClient; var putRowRequest = new PutRowRequest( TestTableName, new Condition(RowExistenceExpectation.IGNORE) ); // put first row var primaryKey = new PrimaryKey { { "PK0", new ColumnValue("ABC") }, { "PK1", ColumnValue.AUTO_INCREMENT } }; var attribute1 = new Column("Col0", new ColumnValue("Hangzhou")); putRowRequest.RowPutChange = new RowPutChange(TestTableName, primaryKey); putRowRequest.RowPutChange.AddColumn(attribute1); var putRowResponse = otsClient.PutRow(putRowRequest); Console.WriteLine("PutRow CU Consumed: Read {0} Write {1}", putRowResponse.ConsumedCapacityUnit.Read, putRowResponse.ConsumedCapacityUnit.Write); // put second row primaryKey = new PrimaryKey { { "PK0", new ColumnValue("CDE") }, { "PK1", ColumnValue.AUTO_INCREMENT } }; attribute1 = new Column("Col0", new ColumnValue("Beijing")); putRowRequest.RowPutChange = new RowPutChange(TestTableName, primaryKey); putRowRequest.RowPutChange.AddColumn(attribute1); putRowResponse = otsClient.PutRow(putRowRequest); Console.WriteLine("PutRow CU Consumed: Read {0} Write {1}", putRowResponse.ConsumedCapacityUnit.Read, putRowResponse.ConsumedCapacityUnit.Write); }
public void TestAsyncOperations() { var clientConfig = new OTSClientConfig( TestEndPoint, TestAccessKeyID, TestAccessKeySecret, TestInstanceName ); clientConfig.OTSDebugLogHandler = null; clientConfig.OTSErrorLogHandler = null; OTSClient = new OTSClient(clientConfig); OTSClientTestHelper.Reset(); CreateTestTableWith4PK(new CapacityUnit(0, 0)); var putRowTaskList = new List <Task <PutRowResponse> >(); for (int i = 0; i < 1000; i++) { var request = new PutRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE), GetPredefinedPrimaryKeyWith4PK(i), GetPredefinedAttributeWith5PK(i)); putRowTaskList.Add(OTSClient.PutRowAsync(request)); } foreach (var task in putRowTaskList) { task.Wait(); AssertCapacityUnit(new CapacityUnit(0, 1), task.Result.ConsumedCapacityUnit); } var getRowTaskList = new List <Task <GetRowResponse> >(); for (int i = 0; i < 1000; i++) { var request = new GetRowRequest(TestTableName, GetPredefinedPrimaryKeyWith4PK(i)); getRowTaskList.Add(OTSClient.GetRowAsync(request)); } for (int i = 0; i < 1000; i++) { var task = getRowTaskList[i]; task.Wait(); var response = task.Result; AssertCapacityUnit(new CapacityUnit(1, 0), response.ConsumedCapacityUnit); AssertPrimaryKey(GetPredefinedPrimaryKeyWith4PK(i), response.PrimaryKey); AssertAttribute(GetPredefinedAttributeWith5PK(i), response.Attribute); } }
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); } }
public static void PutRowAsync() { Console.WriteLine("Start put row async..."); PrepareTable(); OTSClient TabeStoreClient = Config.GetClient(); try { var putRowTaskList = new List <Task <PutRowResponse> >(); for (int i = 0; i < 100; i++) { // 定义行的主键,必须与创建表时的TableMeta中定义的一致 var primaryKey = new PrimaryKey { { "pk0", new ColumnValue(i) }, { "pk1", new ColumnValue("abc") } }; // 定义要写入改行的属性列 var attribute = new AttributeColumns { { "col0", new ColumnValue(i) }, { "col1", new ColumnValue("a") }, { "col2", new ColumnValue(true) } }; var request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); putRowTaskList.Add(TabeStoreClient.PutRowAsync(request)); } foreach (var task in putRowTaskList) { task.Wait(); Console.WriteLine("consumed read:{0}, write:{1}", task.Result.ConsumedCapacityUnit.Read, task.Result.ConsumedCapacityUnit.Write); } Console.WriteLine("Put row async succeeded."); } catch (Exception ex) { Console.WriteLine("Put row async failed. exception:{0}", ex.Message); } }
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 == "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(); }
/// <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."); }
private static void PrepareData() { OTSClient otsClient = Config.GetClient(); // 插入100条数据 for (int i = 0; i < 100; i++) { PrimaryKey primaryKey = new PrimaryKey(); primaryKey.Add("pk0", new ColumnValue(i)); 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(i % 3 != 0)); PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); otsClient.PutRow(request); } }
public static void PutRow() { Console.WriteLine("Start put row..."); OTSClient otsClient = Config.GetClient(); // 定义行的主键,必须与创建表时的TableMeta中定义的一致 PrimaryKey primaryKey = new PrimaryKey { { Pk1, new ColumnValue(0) }, { Pk2, new ColumnValue("abc") } }; // 定义要写入该行的属性列 AttributeColumns attribute = new AttributeColumns { { IncrementCol, new ColumnValue(0) } }; PutRowRequest request = new PutRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); otsClient.PutRow(request); Console.WriteLine("Put row succeed."); }
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)); }
public static void PutRow() { 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); otsClient.PutRow(request); Console.WriteLine("Put row succeed."); }
public void TestBoolean() { Console.WriteLine("1"); CreateTestTable(); Console.WriteLine("2"); 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(true)); 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); }
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 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 static void ConditionUpdateRow() { Console.WriteLine("Start update row..."); PrepareTable(); var otsClient = Config.GetClient(); // 定义行的主键,必须与创建表时的TableMeta中定义的一致 var primaryKey = new PrimaryKey(); primaryKey.Add("pk0", new ColumnValue(1)); primaryKey.Add("pk1", new ColumnValue("abc")); // 定义要写入改行的属性列 var attribute = new AttributeColumns(); attribute.Add("col0", new ColumnValue(1)); attribute.Add("col1", new ColumnValue("a")); attribute.Add("col2", new ColumnValue(true)); var 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,且col1等于'a'时,允许修改,否则不允许修改 try { // 构造condition var cond1 = new RelationalCondition("col0", CompareOperator.NOT_EQUAL, new ColumnValue(5)); var cond2 = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("a")); var columenCondition = new CompositeCondition(LogicOperator.AND); columenCondition.AddCondition(cond1); columenCondition.AddCondition(cond2); var condition = new Condition(RowExistenceExpectation.IGNORE); condition.ColumnCondition = columenCondition; // 构造更新请求 var updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToPut("col2", new ColumnValue(false)); var updateRowRequest = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttribute); // 更新数据 otsClient.UpdateRow(updateRowRequest); // 更新成功 Console.WriteLine("Update row succeeded."); } catch (Exception ex) { Console.WriteLine("Update row failed. error:{0}", ex.Message); } }
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 void PutSingleRow(string tableName, PrimaryKey primaryKey, AttributeColumns attribute) { var request = new PutRowRequest(tableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); OTSClient.PutRow(request); }
/// <summary> /// PutRow的异步形式。 /// </summary> /// <param name="request"></param> /// <returns></returns> public Task <PutRowResponse> PutRowAsync(PutRowRequest request) { return(CallAsync <PutRowRequest, PutRowResponse>("/PutRow", request)); }
/// <summary> /// 插入数据到指定的行,如果该行不存在,则新增一行;若该行存在,则覆盖原有行。 /// 指定表名、主键和属性,写入一行数据。返回本次操作消耗的CapacityUnit。 /// </summary> /// <param name="request">请求实例</param> /// <returns>响应实例</returns> /// <example> /// 写入1行数据,并打印出返回的读写能力消耗。 /// <code> /// var primaryKey = new PrimaryKey(); /// primaryKey.Add("PK0", new ColumnValue("ABC")); /// primaryKey.Add("PK1", new ColumnValue(123)); /// /// var attribute = new AttributeColumns(); /// attribute.Add("IntAttr0", new ColumnValue(12345)); /// attribute.Add("StringAttr1", new ColumnValue("ABC")); /// attribute.Add("DoubleAttr2", new ColumnValue(3.14)); /// attribute.Add("BooleanAttr3", new ColumnValue(true)); /// /// var putRowRequest = new PutRowRequest( /// "SampleTableName", /// new Condition(RowExistenceExpectation.IGNORE), /// primaryKey, /// attribute /// ); /// /// var putRowResponse = otsClient.PutRow(putRowRequest); /// System.Console.WriteLine("PutRow CU Consumed: Read {0} Write {0}", /// putRowResponse.ConsumedCapacityUnit.Read, /// putRowResponse.ConsumedCapacityUnit.Write); /// </code> /// </example> public PutRowResponse PutRow(PutRowRequest request) { return(GetResponseFromAsyncTask(PutRowAsync(request))); }
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); } }
public void TestAPIWithParameter(string apiName) { var tableName = TestContext.tableName; var pkSchema = TestContext.pkSchema; var reservedThroughput = TestContext.reservedThroughput; var primaryKey = TestContext.primaryKey; var attribute = TestContext.attribute; var condition = TestContext.condition; var startPrimaryKey = TestContext.startPrimaryKey; var endPrimaryKey = TestContext.endPrimaryKey; var putRowConsumed = TestContext.putRowConsumed; var getRowConsumed = TestContext.getRowConsumed; var updateRowConsumed = TestContext.updateRowConsumed; var deleteRowConsumed = TestContext.deleteRowConsumed; var getRangeConsumed = TestContext.getRangeConsumed; var updateOfAttributeForPut = TestContext.updateOfAttributeForPut; var updateOfAttributeForDelete = TestContext.updateOfAttributeForDelete; var columnsToGet = TestContext.columnsToGet; var limit = TestContext.limit; var direction = TestContext.direction; var tableMeta = new TableMeta(tableName, pkSchema); switch (apiName) { case "CreateTable": var request0 = new CreateTableRequest(tableMeta, reservedThroughput); OTSClient.CreateTable(request0); return; case "ListTable": var request1 = new ListTableRequest(); var response1 = OTSClient.ListTable(request1); Assert.AreEqual(new List <string>() { tableName }, response1.TableNames); return; case "UpdateTable": var request2 = new UpdateTableRequest(tableName, reservedThroughput); var response2 = OTSClient.UpdateTable(request2); if (reservedThroughput.Read.HasValue && reservedThroughput.Write.HasValue) { AssertCapacityUnit( reservedThroughput, response2.ReservedThroughputDetails.CapacityUnit); } Assert.IsTrue(response2.ReservedThroughputDetails.LastDecreaseTime >= 0); Assert.IsTrue(response2.ReservedThroughputDetails.LastIncreaseTime >= 0); Assert.IsTrue(response2.ReservedThroughputDetails.NumberOfDecreasesToday >= 0); return; case "DeleteTable": var request3 = new DeleteTableRequest(tableName); OTSClient.DeleteTable(request3); var request31 = new ListTableRequest(); var response31 = OTSClient.ListTable(request31); Assert.AreEqual(new List <string>() { }, response31.TableNames); return; case "DescribeTable": var request4 = new DescribeTableRequest(tableName); var response4 = OTSClient.DescribeTable(request4); Assert.AreEqual(tableName, response4.TableMeta.TableName); AssertPrimaryKeySchema(pkSchema, response4.TableMeta.PrimaryKeySchema); AssertCapacityUnit(reservedThroughput, response4.ReservedThroughputDetails.CapacityUnit); Assert.IsTrue(response4.ReservedThroughputDetails.LastDecreaseTime >= 0); Assert.IsTrue(response4.ReservedThroughputDetails.LastIncreaseTime >= 0); Assert.IsTrue(response4.ReservedThroughputDetails.NumberOfDecreasesToday >= 0); return; case "PutRow": var request5 = new PutRowRequest(tableName, condition, primaryKey, attribute); var response5 = OTSClient.PutRow(request5); AssertCapacityUnit(putRowConsumed, response5.ConsumedCapacityUnit); return; case "GetRow": var request6 = new GetRowRequest(tableName, primaryKey, columnsToGet); var response6 = OTSClient.GetRow(request6); AssertPrimaryKey(primaryKey, response6.PrimaryKey, columnsToGet); AssertAttribute(attribute, response6.Attribute, columnsToGet); AssertCapacityUnit(getRowConsumed, response6.ConsumedCapacityUnit); return; case "DeleteRow": var request7 = new DeleteRowRequest(tableName, condition, primaryKey); var response7 = OTSClient.DeleteRow(request7); AssertCapacityUnit(deleteRowConsumed, response7.ConsumedCapacityUnit); var request71 = new GetRowRequest(tableName, primaryKey); var response71 = OTSClient.GetRow(request71); AssertPrimaryKey(new PrimaryKey(), response71.PrimaryKey); AssertAttribute(new AttributeColumns(), response71.Attribute); return; case "UpdateRow_Put": var request8 = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForPut); var response8 = OTSClient.UpdateRow(request8); AssertCapacityUnit(updateRowConsumed, response8.ConsumedCapacityUnit); var request81 = new GetRowRequest(tableName, primaryKey); var response81 = OTSClient.GetRow(request81); AssertPrimaryKey(primaryKey, response81.PrimaryKey); AssertAttribute(attribute, response81.Attribute); AssertCapacityUnit(getRowConsumed, response81.ConsumedCapacityUnit); return; case "UpdateRow_Delete": var request9 = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForDelete); var response9 = OTSClient.UpdateRow(request9); AssertCapacityUnit(deleteRowConsumed, response9.ConsumedCapacityUnit); var request91 = new GetRowRequest(tableName, primaryKey); var response91 = OTSClient.GetRow(request91); // Don't assert primary key AssertAttribute(new AttributeColumns(), response91.Attribute); return; case "BatchGetRow": var request11 = new BatchGetRowRequest(); request11.Add(tableName, new List <PrimaryKey>() { primaryKey }, columnsToGet); var response11 = OTSClient.BatchGetRow(request11); Assert.AreEqual(1, response11.RowDataGroupByTable.Count); Assert.IsTrue(response11.RowDataGroupByTable.ContainsKey(tableName)); Assert.AreEqual(1, response11.RowDataGroupByTable[tableName].Count); if (!response11.RowDataGroupByTable[tableName][0].IsOK) { throw new OTSServerException(apiName, HttpStatusCode.OK, response11.RowDataGroupByTable[tableName][0].ErrorCode, response11.RowDataGroupByTable[tableName][0].ErrorMessage); } AssertPrimaryKey(primaryKey, response11.RowDataGroupByTable[tableName][0].PrimaryKey); AssertAttribute(attribute, response11.RowDataGroupByTable[tableName][0].Attribute); AssertCapacityUnit(getRowConsumed, response11.RowDataGroupByTable[tableName][0].Consumed); return; case "BatchWriteRow_Put": var request12 = new BatchWriteRowRequest(); var rowChanges = new RowChanges(); rowChanges.AddPut(condition, primaryKey, attribute); request12.Add(tableName, rowChanges); var response12 = OTSClient.BatchWriteRow(request12); Assert.AreEqual(1, response12.TableRespones.Count); Assert.IsTrue(response12.TableRespones.ContainsKey(tableName)); Assert.AreEqual(1, response12.TableRespones[tableName].PutResponses.Count); Assert.AreEqual(0, response12.TableRespones[tableName].UpdateResponses.Count); Assert.AreEqual(0, response12.TableRespones[tableName].DeleteResponses.Count); if (response12.TableRespones[tableName].PutResponses[0].IsOK) { AssertCapacityUnit(putRowConsumed, response12.TableRespones[tableName].PutResponses[0].Consumed); } else { throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK, response12.TableRespones[tableName].PutResponses[0].ErrorCode, response12.TableRespones[tableName].PutResponses[0].ErrorMessage); } var request121 = new GetRowRequest(tableName, primaryKey); var response121 = OTSClient.GetRow(request121); AssertPrimaryKey(primaryKey, response121.PrimaryKey); AssertAttribute(attribute, response121.Attribute); AssertCapacityUnit(getRowConsumed, response121.ConsumedCapacityUnit); return; case "BatchWriteRow_Update": var request13 = new BatchWriteRowRequest(); var rowChanges2 = new RowChanges(); rowChanges2.AddUpdate(condition, primaryKey, updateOfAttributeForPut); request13.Add(tableName, rowChanges2); var response13 = OTSClient.BatchWriteRow(request13); Assert.AreEqual(1, response13.TableRespones.Count); Assert.IsTrue(response13.TableRespones.ContainsKey(tableName)); Assert.AreEqual(0, response13.TableRespones[tableName].PutResponses.Count); Assert.AreEqual(1, response13.TableRespones[tableName].UpdateResponses.Count); Assert.AreEqual(0, response13.TableRespones[tableName].DeleteResponses.Count); if (response13.TableRespones[tableName].UpdateResponses[0].IsOK) { AssertCapacityUnit(updateRowConsumed, response13.TableRespones[tableName].UpdateResponses[0].Consumed); } else { throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK, response13.TableRespones[tableName].UpdateResponses[0].ErrorCode, response13.TableRespones[tableName].UpdateResponses[0].ErrorMessage); } var request131 = new GetRowRequest(tableName, primaryKey); var response131 = OTSClient.GetRow(request131); AssertPrimaryKey(primaryKey, response131.PrimaryKey); AssertAttribute(attribute, response131.Attribute); AssertCapacityUnit(getRowConsumed, response131.ConsumedCapacityUnit); return; case "BatchWriteRow_Delete": var request14 = new BatchWriteRowRequest(); var rowChanges3 = new RowChanges(); rowChanges3.AddDelete(condition, primaryKey); request14.Add(tableName, rowChanges3); var response14 = OTSClient.BatchWriteRow(request14); Assert.AreEqual(1, response14.TableRespones.Count); Assert.IsTrue(response14.TableRespones.ContainsKey(tableName)); Assert.AreEqual(0, response14.TableRespones[tableName].PutResponses.Count); Assert.AreEqual(0, response14.TableRespones[tableName].UpdateResponses.Count); Assert.AreEqual(1, response14.TableRespones[tableName].DeleteResponses.Count); if (response14.TableRespones[tableName].DeleteResponses[0].IsOK) { AssertCapacityUnit(deleteRowConsumed, response14.TableRespones[tableName].DeleteResponses[0].Consumed); } else { throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK, response14.TableRespones[tableName].DeleteResponses[0].ErrorCode, response14.TableRespones[tableName].DeleteResponses[0].ErrorMessage); } var request141 = new GetRowRequest(tableName, primaryKey); var response141 = OTSClient.GetRow(request141); AssertPrimaryKey(new PrimaryKey(), response141.PrimaryKey); AssertAttribute(new AttributeColumns(), response141.Attribute); return; case "GetRange": var request15 = new GetRangeRequest(tableName, direction, startPrimaryKey, endPrimaryKey, columnsToGet, limit); var response15 = OTSClient.GetRange(request15); Assert.AreEqual(1, response15.RowDataList.Count); Assert.AreEqual(null, response15.NextPrimaryKey); AssertCapacityUnit(getRangeConsumed, response15.ConsumedCapacityUnit); AssertPrimaryKey(primaryKey, response15.RowDataList[0].PrimaryKey, columnsToGet); AssertAttribute(attribute, response15.RowDataList[0].Attribute, columnsToGet); return; default: throw new Exception(String.Format("invalid api name: {0}", apiName)); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> public void PutRow(Object obj) { var type = obj.GetType(); var name = tableNames.ContainsKey(type) ? tableNames[type] : SetTable(type); var keys = new PrimaryKey(); var columns = new AttributeColumns(); foreach (PropertyInfo info in rft.GetPropertyList(type)) { var methodInfo = info.GetGetMethod(); if (methodInfo == null || methodInfo.IsStatic || !methodInfo.IsPublic) { continue; } if (info.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.KeyAttribute)) == null) { #region 属性列处理 var propertyValue = info.GetValue(obj, null); if (propertyValue == null) { continue; } else if (info.PropertyType == typeof(int)) { columns.Add(info.Name, new ColumnValue((int)propertyValue)); } else if (info.PropertyType == typeof(long)) { columns.Add(info.Name, new ColumnValue((long)propertyValue)); } else if (info.PropertyType == typeof(string)) { columns.Add(info.Name, new ColumnValue((string)propertyValue)); } else if (info.PropertyType == typeof(bool)) { columns.Add(info.Name, new ColumnValue((bool)propertyValue)); } else if (info.PropertyType == typeof(double)) { columns.Add(info.Name, new ColumnValue((double)propertyValue)); } else if (info.PropertyType == typeof(ulong)) { columns.Add(info.Name, new ColumnValue((ulong)propertyValue)); } else if (info.PropertyType == typeof(byte[])) { columns.Add(info.Name, new ColumnValue((byte[])propertyValue)); } #endregion } else { #region 主键字段处理 var propertyValue = info.GetValue(obj, null); if (propertyValue == null) { throw new OTSException("Empty PrimaryKey: " + info.Name); } else if (info.PropertyType == typeof(int)) { keys.Add(info.Name, new ColumnValue((int)propertyValue)); } else if (info.PropertyType == typeof(string)) { keys.Add(info.Name, new ColumnValue((string)propertyValue)); } else if (info.PropertyType == typeof(byte[])) { keys.Add(info.Name, new ColumnValue((byte[])propertyValue)); } #endregion } } var req = new PutRowRequest(name, new Condition(RowExistenceExpectation.IGNORE), keys, columns); OtsClient.PutRow(req); }