public void UpdateRowTest() { CreateTable(); var otsClient = OTSClient; var primaryKey = new PrimaryKey(); primaryKey.Add("PK0", new ColumnValue("ABC")); primaryKey.Add("PK1", new ColumnValue(123)); var updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToPut("NewColumn", new ColumnValue(123)); updateOfAttribute.AddAttributeColumnToDelete("IntAttr0"); var updateRowRequest = new UpdateRowRequest( "SampleTable", new Condition(RowExistenceExpectation.IGNORE), primaryKey, updateOfAttribute); var updateRowResponse = otsClient.UpdateRow(updateRowRequest); System.Console.WriteLine("UpdateRow CU Consumed: Read {0} Write {0}", updateRowResponse.ConsumedCapacityUnit.Read, updateRowResponse.ConsumedCapacityUnit.Write); DeleteTable(); }
private void Run() { for (int i = 0; i < round; ++i) { var primaryKey = new PrimaryKey(); primaryKey.Add("PK0", new ColumnValue(pk)); var request = new GetRowRequest(tableName, primaryKey); var response = OTSClient.GetRow(request); var attr = response.Attribute["Col1"]; long oldIntValue = attr.IntegerValue; ColumnValue oldValue = new ColumnValue(oldIntValue); ColumnValue newValue = new ColumnValue(oldIntValue + 1); RelationalCondition cc = new RelationalCondition("Col1", RelationalCondition.CompareOperator.EQUAL, oldValue); Condition cond = new Condition(RowExistenceExpectation.IGNORE); cond.ColumnCondition = cc; UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute(); updateOfAttributeForPut.AddAttributeColumnToPut("Col1", newValue); UpdateRowRequest updateReq = new UpdateRowRequest(tableName, cond, primaryKey, updateOfAttributeForPut); bool success = true; try { OTSClient.UpdateRow(updateReq); } catch (OTSServerException) { success = false; } if (success) { ++count; } } }
public void Test1000PutAnd1000DeleteInUpdateRow() { var pk = new PrimaryKey { { "PK0", new ColumnValue("123") }, { "PK1", new ColumnValue(123) } }; var updateOfAttribute = new UpdateOfAttribute(); for (int i = 0; i < 1000; i++) { updateOfAttribute.AddAttributeColumnToPut("Put" + i, new ColumnValue("blah")); updateOfAttribute.AddAttributeColumnToDelete("Delete" + i); } var request = new UpdateRowRequest( TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute); try { var response = OTSClient.UpdateRow(request); } catch (OTSServerException e) { AssertOTSServerException(e, new OTSServerException( "/UpdateRow", HttpStatusCode.BadRequest, "OTSParameterInvalid", "The number of attribute columns exceeds the limit, limit count: 1024, column count: 2000." )); } }
public void TestEmptyUpdateRow() { var primaryKey = new PrimaryKey { { "PK0", new ColumnValue(0) } }; var updateOfAttribute = new UpdateOfAttribute(); var request = new UpdateRowRequest(TestTableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, updateOfAttribute); try { OTSClient.UpdateRow(request); Assert.Fail(); } catch (OTSServerException e) { AssertOTSServerException(e, new OTSServerException( "/UpdateRow", HttpStatusCode.BadRequest, "OTSParameterInvalid", "Attribute column is missing." )); } }
public void TestDuplicateDeleteInUpdateRow() { var pk = new PrimaryKey(); pk.Add("PK0", new ColumnValue("123")); pk.Add("PK1", new ColumnValue(123)); var updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToDelete("Col0"); updateOfAttribute.AddAttributeColumnToDelete("Col0"); var request = new UpdateRowRequest( TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute); try { var response = OTSClient.UpdateRow(request); Assert.Fail(); } catch (OTSServerException e) { AssertOTSServerException(e, new OTSServerException( "/UpdateRow", HttpStatusCode.BadRequest, "OTSParameterInvalid", "Duplicated attribute column name: 'Col0' while updating row." )); } }
public bool UpdateRow(String tableName, Int64 pk, String colName, ColumnValue colValue, IColumnCondition cond) { bool success = true; var primaryKey = new PrimaryKey { { "PK0", new ColumnValue(pk) } }; Condition rowCond = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = cond }; UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute(); updateOfAttributeForPut.AddAttributeColumnToPut(colName, colValue); var request = new UpdateRowRequest(tableName, rowCond, primaryKey, updateOfAttributeForPut); try { OTSClient.UpdateRow(request); } catch (OTSServerException e) { Console.WriteLine("UpdateRow fail: {0}", e.ErrorMessage); success = false; } return(success); }
public UpdateRowRequest(string tableName, Condition condition, PrimaryKey primaryKey, UpdateOfAttribute updateOfAttribute) { TableName = tableName; Condition = condition; PrimaryKey = primaryKey; UpdateOfAttribute = updateOfAttribute; }
public void GetTimeRangeRowTest() { CreateTable(); PutRow(); var otsClient = OTSClient; var primaryKey = new PrimaryKey { { "PK0", new ColumnValue("ABC") }, { "PK1", new ColumnValue(123) } }; // update row var updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToPut("Col0", new ColumnValue("Beijing")); var updateRowRequest = new UpdateRowRequest( TestTableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, updateOfAttribute); var updateRowResponse = otsClient.UpdateRow(updateRowRequest); var getRowRequest = new GetRowRequest( TestTableName, primaryKey ); getRowRequest.QueryCriteria.TimeRange = new TimeRange { StartTime = 0, EndTime = Int64.MaxValue }; var getRowResponse = otsClient.GetRow(getRowRequest); Console.WriteLine("GetRow CU Consumed: Read {0} Write {1}", getRowResponse.ConsumedCapacityUnit.Read, getRowResponse.ConsumedCapacityUnit.Write); var pk0 = getRowResponse.PrimaryKey["PK0"]; Console.WriteLine("PrimaryKey PK0 Value {0}", pk0.StringValue); var pk1 = getRowResponse.PrimaryKey["PK1"]; Console.WriteLine("PrimaryKey PK1 Value {0}", pk1.IntegerValue); var row = getRowResponse.Row; foreach (Column column in row.GetColumn("Col0")) { Console.WriteLine(column); } Assert.AreEqual(2, row.GetColumn("Col0").Count); DeleteTable(); }
public static void BatchWriteRow() { Console.WriteLine("Start batch write row..."); PrepareTable(); PrepareData(); OTSClient otsClient = Config.GetClient(); // 一次批量导入100行数据 var request = new BatchWriteRowRequest(); var rowChanges = new RowChanges(TableName); for (int i = 0; i < 100; i++) { PrimaryKey primaryKey = new PrimaryKey { { "pk0", new ColumnValue(i) }, { "pk1", new ColumnValue("abc") } }; // 定义要写入改行的属性列 UpdateOfAttribute attribute = new UpdateOfAttribute(); attribute.AddAttributeColumnToPut("col0", new ColumnValue(0)); attribute.AddAttributeColumnToPut("col1", new ColumnValue("a")); attribute.AddAttributeColumnToPut("col2", new ColumnValue(true)); rowChanges.AddUpdate(new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); } request.Add(TableName, rowChanges); var response = otsClient.BatchWriteRow(request); var tableRows = response.TableRespones; var rows = tableRows[TableName]; int succeedRows = 0; int failedRows = 0; foreach (var row in rows.Responses) { // 注意:batch操作可能部分成功部分失败,需要为每行检查状态 if (row.IsOK) { succeedRows++; } else { Console.WriteLine("Read row failed: " + row.ErrorMessage); failedRows++; } } Console.WriteLine("SucceedRows: " + succeedRows); Console.WriteLine("FailedRows: " + failedRows); }
public UpdateRowRequest(string tableName, Condition condition, PrimaryKey primaryKey, UpdateOfAttribute updateOfAttribute) { TableName = tableName; Condition = condition; PrimaryKey = primaryKey; UpdateOfAttribute = updateOfAttribute; RowUpdateChange = new RowUpdateChange(tableName, primaryKey) { Condition = condition }; RowUpdateChange.FromUpdateOfAtrribute(updateOfAttribute); }
public void Test4PutAnd4DeleteInUpdateRow() { CreateTestTableWith2PK(); var pk = new PrimaryKey { { "PK0", new ColumnValue("123") }, { "PK1", new ColumnValue(123) } }; var attribute = new AttributeColumns { { "Col0", new ColumnValue("0") }, { "Col1", new ColumnValue("1") }, { "Col2", new ColumnValue("2") }, { "Col3", new ColumnValue("3") }, { "Col4", new ColumnValue("4") } }; PutSingleRow(TestTableName, pk, attribute); var updateOfAttribute = new UpdateOfAttribute(); updateOfAttribute.AddAttributeColumnToPut("Col3", new ColumnValue("5")); updateOfAttribute.AddAttributeColumnToPut("Col4", new ColumnValue("6")); updateOfAttribute.AddAttributeColumnToPut("Col5", new ColumnValue("7")); updateOfAttribute.AddAttributeColumnToPut("Col6", new ColumnValue("8")); updateOfAttribute.AddAttributeColumnToDelete("Col1"); updateOfAttribute.AddAttributeColumnToDelete("Col2"); updateOfAttribute.AddAttributeColumnToDelete("Col7"); updateOfAttribute.AddAttributeColumnToDelete("Col8"); var request = new UpdateRowRequest( TestTableName, new Condition(RowExistenceExpectation.IGNORE), pk, updateOfAttribute); var response = OTSClient.UpdateRow(request); AssertCapacityUnit(new CapacityUnit(0, 1), response.ConsumedCapacityUnit); var expectAttribute = new AttributeColumns { { "Col0", new ColumnValue("0") }, { "Col3", new ColumnValue("5") }, { "Col4", new ColumnValue("6") }, { "Col5", new ColumnValue("7") }, { "Col6", new ColumnValue("8") } }; CheckSingleRow(TestTableName, pk, expectAttribute); }
private void UpdateRow(OTSClient client, String tableName, IColumnCondition cond) { var primaryKey = new PrimaryKey { { COLUMN_GID_NAME, new ColumnValue(1) }, { COLUMN_UID_NAME, new ColumnValue(101) } }; UpdateOfAttribute updateOfAttributeForPut = new UpdateOfAttribute(); updateOfAttributeForPut.AddAttributeColumnToPut(COLUMN_NAME_NAME, new ColumnValue("张三")); updateOfAttributeForPut.AddAttributeColumnToPut(COLUMN_ADDRESS_NAME, new ColumnValue("中国B地")); updateOfAttributeForPut.AddAttributeColumnToDelete(COLUMN_MOBILE_NAME); updateOfAttributeForPut.AddAttributeColumnToDelete(COLUMN_AGE_NAME); Condition condition = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = cond }; var request = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForPut); try { client.UpdateRow(request); Console.WriteLine("UpdateRow success"); } catch (OTSServerException e) { //服务端异常 Console.WriteLine("操作失败:{0}", e.ErrorMessage); Console.WriteLine("请求ID:{0}", e.RequestID); } catch (OTSClientException e) { //可能是网络不好或者返回结果有问题 Console.WriteLine("请求失败:{0}", e.ErrorMessage); } }
public static void UpdateRow() { Console.WriteLine("Start update row..."); PrepareTable(); OTSClient otsClient = Config.GetClient(); // 定义行的主键,必须与创建表时的TableMeta中定义的一致 PrimaryKey primaryKey = new PrimaryKey(); primaryKey.Add("pk0", new ColumnValue(0)); primaryKey.Add("pk1", new ColumnValue("abc")); // 定义要写入改行的属性列 UpdateOfAttribute attribute = new UpdateOfAttribute(); attribute.AddAttributeColumnToPut("col0", new ColumnValue(0)); attribute.AddAttributeColumnToPut("col1", new ColumnValue("b")); // 将原先的值'a'改为'b' attribute.AddAttributeColumnToPut("col2", new ColumnValue(true)); UpdateRowRequest request = new UpdateRowRequest(TableName, new Condition(RowExistenceExpectation.IGNORE), primaryKey, attribute); otsClient.UpdateRow(request); Console.WriteLine("Update row succeed."); }
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); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> public void UpdateRow(Object obj) { var type = obj.GetType(); var name = tableNames.ContainsKey(type) ? tableNames[type] : SetTable(type); var keys = new PrimaryKey(); var columns = new UpdateOfAttribute(); 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.AddAttributeColumnToPut(info.Name, new ColumnValue((int)propertyValue)); } else if (info.PropertyType == typeof(long)) { columns.AddAttributeColumnToPut(info.Name, new ColumnValue((long)propertyValue)); } else if (info.PropertyType == typeof(string)) { columns.AddAttributeColumnToPut(info.Name, new ColumnValue((string)propertyValue)); } else if (info.PropertyType == typeof(bool)) { columns.AddAttributeColumnToPut(info.Name, new ColumnValue((bool)propertyValue)); } else if (info.PropertyType == typeof(double)) { columns.AddAttributeColumnToPut(info.Name, new ColumnValue((double)propertyValue)); } else if (info.PropertyType == typeof(ulong)) { columns.AddAttributeColumnToPut(info.Name, new ColumnValue((ulong)propertyValue)); } else if (info.PropertyType == typeof(byte[])) { columns.AddAttributeColumnToPut(info.Name, new ColumnValue((byte[])propertyValue)); } #endregion } else { #region 主键字段处理 var propertyValue = info.GetValue(obj, null); if (propertyValue == null) { throw new OTSException("PrimaryKey can not null"); } 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 UpdateRowRequest(name, new Condition(RowExistenceExpectation.EXPECT_EXIST), keys, columns); OtsClient.UpdateRow(req); }
public void SetTestConext(string tableName = null, PrimaryKeySchema pkSchema = null, CapacityUnit reservedThroughput = null, PrimaryKey primaryKey = null, AttributeColumns attribute = null, UpdateOfAttribute updateOfAttributeForPut = null, UpdateOfAttribute updateOfAttributeForDelete = null, Condition condition = null, GetRangeDirection direction = GetRangeDirection.Forward, PrimaryKey startPrimaryKey = null, PrimaryKey endPrimaryKey = null, HashSet <string> columnsToGet = null, int?limit = null, CapacityUnit putRowConsumed = null, CapacityUnit getRowConsumed = null, CapacityUnit updateRowConsumed = null, CapacityUnit deleteRowConsumed = null, CapacityUnit getRangeConsumed = null, Dictionary <string, string> expectedFailure = null, string allFailedMessage = null) { var DefaultPrimaryKeySchema = new PrimaryKeySchema(); DefaultPrimaryKeySchema.Add("PK0", ColumnValueType.String); DefaultPrimaryKeySchema.Add("PK1", ColumnValueType.String); DefaultPrimaryKeySchema.Add("PK2", ColumnValueType.Integer); DefaultPrimaryKeySchema.Add("PK3", ColumnValueType.Integer); var DefaultReservedThroughput = new CapacityUnit(0, 0); TestContext = new APITestContext(); TestContext.expectedFailure = expectedFailure; TestContext.allFailedMessage = allFailedMessage; TestContext.tableName = tableName ?? OTSUnitTestBase.TestTableName; TestContext.pkSchema = pkSchema ?? DefaultPrimaryKeySchema; TestContext.reservedThroughput = reservedThroughput ?? DefaultReservedThroughput; TestContext.primaryKey = primaryKey ?? PrimaryKeyWith4Columns; TestContext.attribute = attribute ?? AttributeWith5Columns; TestContext.condition = condition ?? new Condition(RowExistenceExpectation.IGNORE); TestContext.startPrimaryKey = startPrimaryKey ?? MinPrimaryKeyWith4Columns; TestContext.endPrimaryKey = endPrimaryKey ?? MaxPrimaryKeyWith4Columns; TestContext.putRowConsumed = putRowConsumed ?? new CapacityUnit(0, 1); TestContext.getRowConsumed = getRowConsumed ?? new CapacityUnit(1, 0); TestContext.updateRowConsumed = updateRowConsumed ?? new CapacityUnit(0, 1); TestContext.deleteRowConsumed = deleteRowConsumed ?? new CapacityUnit(0, 1); TestContext.getRangeConsumed = getRangeConsumed ?? new CapacityUnit(1, 0); TestContext.columnsToGet = columnsToGet; TestContext.limit = limit; TestContext.direction = direction; if (updateOfAttributeForPut == null) { updateOfAttributeForPut = new UpdateOfAttribute(); foreach (var item in TestContext.attribute) { updateOfAttributeForPut.AddAttributeColumnToPut(item.Key, item.Value); } } if (updateOfAttributeForDelete == null) { updateOfAttributeForDelete = new UpdateOfAttribute(); foreach (var item in TestContext.attribute) { updateOfAttributeForDelete.AddAttributeColumnToDelete(item.Key); } } TestContext.updateOfAttributeForPut = updateOfAttributeForPut; TestContext.updateOfAttributeForDelete = updateOfAttributeForDelete; }
public void TestBatchWriteRow() { String tableName = "condition_update_test_table"; CreateTable(tableName); bool success = PutRow(tableName, 20, "Col1", new ColumnValue("Value20"), null); Assert.IsTrue(success); success = PutRow(tableName, 21, "Col1", new ColumnValue("Value21"), null); Assert.IsTrue(success); success = PutRow(tableName, 22, "Col1", new ColumnValue("Value22"), null); Assert.IsTrue(success); var primaryKey = new PrimaryKey { { "PK0", new ColumnValue(20) } }; var attribute = new AttributeColumns { { "Col2", new ColumnValue("Value2") } }; Condition cond1 = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = new RelationalCondition( "Col1", CompareOperator.NOT_EQUAL, new ColumnValue("Value20")) }; var updatePrimaryKey = new PrimaryKey { { "PK0", new ColumnValue(21) } }; UpdateOfAttribute update2 = new UpdateOfAttribute(); update2.AddAttributeColumnToPut("Col3", new ColumnValue("Value3")); Condition cond2 = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = new RelationalCondition( "Col1", CompareOperator.EQUAL, new ColumnValue("Value21")) }; var deletePrimaryKey = new PrimaryKey { { "PK0", new ColumnValue(22) } }; Condition cond3 = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = new RelationalCondition( "Col1", CompareOperator.GREATER_THAN, new ColumnValue("Value22")) }; var batchWriteRequest = new BatchWriteRowRequest(); RowChanges changes = new RowChanges(tableName); changes.AddPut(cond1, primaryKey, attribute); changes.AddUpdate(cond2, updatePrimaryKey, update2); changes.AddDelete(cond3, deletePrimaryKey); batchWriteRequest.Add(tableName, changes); var response = OTSClient.BatchWriteRow(batchWriteRequest); var tables = response.TableRespones; Assert.AreEqual(1, tables.Count); var rows = tables[tableName]; Assert.IsFalse(rows.Responses[0].IsOK); }