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 TestColumnMissing() { string tableName = "condition_update_test_table"; CreateTable(tableName); bool success = PutRow(tableName, 2, "Col1", new ColumnValue("Value1"), null); Assert.IsTrue(success); // put row with condition: colX != valueY // with passIfMissing == true, this should succeed success = PutRow(tableName, 2, "Col2", new ColumnValue("Value2"), new RelationalCondition("ColX", CompareOperator.NOT_EQUAL, new ColumnValue("ValueY"))); Assert.IsTrue(success); // put row with condition: colX != valueY // with passIfMissing == false, this should fail RelationalCondition cond = new RelationalCondition("ColX", CompareOperator.NOT_EQUAL, new ColumnValue("ValueY")) { PassIfMissing = false }; success = PutRow(tableName, 2, "Col2", new ColumnValue("Value2"), cond); Assert.IsFalse(success); }
public void TestGetRowWithFilterAndLogicOrAllNotHit() { CreateTestTableWith4PK(); var pk = new PrimaryKey { { "PK0", new ColumnValue("a") }, { "PK1", new ColumnValue("a") }, { "PK2", new ColumnValue(10) }, { "PK3", new ColumnValue(20) } }; var attribute = new AttributeColumns { { "Col0", new ColumnValue(10) }, { "Col1", new ColumnValue(2) }, { "Col2", new ColumnValue(3) } }; PutSingleRow(TestTableName, pk, attribute); var columnsToGet = new HashSet <string>(); var filter1 = new RelationalCondition("Col0", CompareOperator.LESS_EQUAL, new ColumnValue(5)); var filter2 = new RelationalCondition("Col1", CompareOperator.NOT_EQUAL, new ColumnValue(2)); var filter = new CompositeCondition(Aliyun.OTS.DataModel.LogicOperator.OR); filter.AddCondition(filter1); filter.AddCondition(filter2); CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, isEmpty: true, condition: filter); }
public void TestGetRowWithFilterOneRowAndReturnCol1() { CreateTestTableWith4PK(); var pk = new PrimaryKey { { "PK0", new ColumnValue("a") }, { "PK1", new ColumnValue("aff") }, { "PK2", new ColumnValue(10) }, { "PK3", new ColumnValue(20) } }; var attribute = new AttributeColumns { { "Col0", new ColumnValue(10) }, { "Col1", new ColumnValue(2) }, { "Col2", new ColumnValue(3) } }; PutSingleRow(TestTableName, pk, attribute); var columnsToGet = new HashSet <string> { "Col0", "Col1" }; var filter = new RelationalCondition("Col0", CompareOperator.LESS_THAN, new ColumnValue(5)); CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, isEmpty: true, condition: filter); }
public void TestGetRowWithFilterAndLogicAndPartHit() { CreateTestTableWith4PK(); var pk = new PrimaryKey { { "PK0", new ColumnValue("a") }, { "PK1", new ColumnValue("a") }, { "PK2", new ColumnValue(10) }, { "PK3", new ColumnValue(20) } }; var attribute = new AttributeColumns { { "Col0", new ColumnValue(10) }, { "Col1", new ColumnValue(2) }, { "Col2", new ColumnValue(3) } }; PutSingleRow(TestTableName, pk, attribute); var columnsToGet = new HashSet <string>(); var filter1 = new RelationalCondition("Col0", CompareOperator.GREATER_THAN, new ColumnValue(5)); var filter2 = new RelationalCondition("Col1", CompareOperator.NOT_EQUAL, new ColumnValue(2)); var filter = new CompositeCondition(LogicOperator.AND); filter.AddCondition(filter1); filter.AddCondition(filter2); CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, true, filter); }
public void TestGetRowWithFilterAndLogicOrAllHit() { CreateTestTableWith4PK(); var pk = new PrimaryKey(); pk.Add("PK0", new ColumnValue("a")); pk.Add("PK1", new ColumnValue("a")); pk.Add("PK2", new ColumnValue(10)); pk.Add("PK3", new ColumnValue(20)); var attribute = new AttributeColumns(); attribute.Add("Col0", new ColumnValue(10)); attribute.Add("Col1", new ColumnValue(2)); attribute.Add("Col2", new ColumnValue(3)); PutSingleRow(TestTableName, pk, attribute); var columnsToGet = new HashSet <string>(); var filter1 = new RelationalCondition("Col0", RelationalCondition.CompareOperator.GREATER_THAN, new ColumnValue(5)); var filter2 = new RelationalCondition("Col1", RelationalCondition.CompareOperator.EQUAL, new ColumnValue(2)); var filter = new CompositeCondition(CompositeCondition.LogicOperator.OR); filter.AddCondition(filter1); filter.AddCondition(filter2); CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, condition: filter); }
public void TestGetRowWithFilterZeroRowAndReturnOneColumn() { CreateTestTableWith4PK(); var pk = new PrimaryKey(); pk.Add("PK0", new ColumnValue("a")); pk.Add("PK1", new ColumnValue("a")); pk.Add("PK2", new ColumnValue(10)); pk.Add("PK3", new ColumnValue(20)); var attribute = new AttributeColumns(); attribute.Add("Col0", new ColumnValue(10)); attribute.Add("Col1", new ColumnValue(2)); attribute.Add("Col2", new ColumnValue(3)); PutSingleRow(TestTableName, pk, attribute); var columnsToGet = new HashSet <string>(); columnsToGet.Add("Col1"); var filter = new RelationalCondition("Col0", RelationalCondition.CompareOperator.GREATER_THAN, new ColumnValue(5)); CheckSingleRow(TestTableName, pk, attribute, new CapacityUnit(1, 0), columnsToGet, condition: filter); }
public void ConditionUpdateExampleTest() { var otsClient = OTSClient; String tableName = "condition_update_test_example"; try { CreateTable(otsClient, tableName); PutRow(otsClient, tableName); GetRow(otsClient, tableName); // 设置update condition:年龄< 20岁 // UpdateRow应该失败 ColumnCondition cond = new RelationalCondition(COLUMN_AGE_NAME, RelationalCondition.CompareOperator.LESS_THAN, new ColumnValue(20)); UpdateRow(otsClient, tableName, cond); //设置update condition: 年龄 >= 20岁 并且 地址是“中国A地“ //UpdateRow应该成功 cond = new CompositeCondition(CompositeCondition.LogicOperator.AND) .AddCondition(new RelationalCondition( COLUMN_AGE_NAME, RelationalCondition.CompareOperator.GREATER_THAN, new ColumnValue(20))) .AddCondition(new RelationalCondition( COLUMN_ADDRESS_NAME, RelationalCondition.CompareOperator.EQUAL, new ColumnValue("中国A地"))); UpdateRow(otsClient, tableName, cond); GetRow(otsClient, tableName); DeleteRow(otsClient, tableName); } catch (OTSServerException e) { Console.WriteLine("服务失败:{0}", e.ErrorMessage); Console.WriteLine("Request ID:{0}", e.RequestID); } catch (OTSClientException e) { Console.WriteLine("OTSClientException:{0}", e.ErrorMessage); } catch (Exception e) { Console.WriteLine("Exception:{0}", e.Message); } finally { try { DeleteTable(otsClient, tableName); }catch (OTSServerException e) { Console.WriteLine("删表失败:{0}", e.ErrorMessage); } catch (OTSClientException e) { Console.WriteLine("删表请求失败:{0}", e.ErrorMessage); } } }
private ByteString BuildRelationalCondition(RelationalCondition scc) { PB.RelationCondition.Builder builder = PB.RelationCondition.CreateBuilder(); builder.SetColumnName(scc.ColumnName); builder.SetComparator(MakeComparatorType(scc.Operator)); builder.SetColumnValue(MakeColumnValue(scc.ColumnValue)); builder.SetPassIfMissing(scc.PassIfMissing); return(builder.Build().ToByteString()); }
public static void BatchGetRowWithFilter() { Console.WriteLine("Start batch get row with filter ..."); PrepareTable(); PrepareData(); OTSClient otsClient = Config.GetClient(); // 批量一次读10行 BatchGetRowRequest request = new BatchGetRowRequest(); List <PrimaryKey> primaryKeys = new List <PrimaryKey>(); for (int i = 0; i < 10; i++) { PrimaryKey primaryKey = new PrimaryKey { { "pk0", new ColumnValue(i) }, { "pk1", new ColumnValue("abc") } }; primaryKeys.Add(primaryKey); } var condition = new RelationalCondition("col2", CompareOperator.EQUAL, new ColumnValue(false)); request.Add(TableName, primaryKeys, null, condition); BatchGetRowResponse response = otsClient.BatchGetRow(request); var tableRows = response.RowDataGroupByTable; var rows = tableRows[TableName]; foreach (var row in rows) { // 注意:batch操作可能部分成功部分失败,需要为每行检查状态 if (row.IsOK) { Console.WriteLine("-----------------"); foreach (KeyValuePair <string, ColumnValue> entry in row.PrimaryKey) { Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value)); } foreach (KeyValuePair <string, ColumnValue> entry in row.Attribute) { Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value)); } Console.WriteLine("-----------------"); } else { Console.WriteLine("Read row with filter failed: " + row.ErrorMessage); } } Console.WriteLine("RowsCount with filter"); }
public static void GetRowWithFilter() { Console.WriteLine("Start get row with filter ..."); PrepareTable(); OTSClient otsClient = Config.GetClient(); // 定义行的主键,必须与创建表时的TableMeta中定义的一致 PrimaryKey primaryKey = new PrimaryKey { { "pk0", new ColumnValue(0) }, { "pk1", new ColumnValue("abc") } }; var rowQueryCriteria = new SingleRowQueryCriteria(TableName) { RowPrimaryKey = primaryKey }; // 只返回col0的值等于5的行或者col1不等于ff的行 var filter1 = new RelationalCondition("col0", CompareOperator.EQUAL, new ColumnValue(5)); var filter2 = new RelationalCondition("col1", CompareOperator.NOT_EQUAL, new ColumnValue("ff")); var filter = new CompositeCondition(LogicOperator.OR); filter.AddCondition(filter1); filter.AddCondition(filter2); rowQueryCriteria.Filter = filter.ToFilter(); rowQueryCriteria.AddColumnsToGet("col0"); rowQueryCriteria.AddColumnsToGet("col1"); GetRowRequest request = new GetRowRequest(rowQueryCriteria); // 查询 GetRowResponse response = otsClient.GetRow(request); PrimaryKey primaryKeyRead = response.PrimaryKey; AttributeColumns attributesRead = response.Attribute; Console.WriteLine("Primary key read: "); foreach (KeyValuePair <string, ColumnValue> entry in primaryKeyRead) { Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value)); } Console.WriteLine("Attributes read: "); foreach (KeyValuePair <string, ColumnValue> entry in attributesRead) { Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value)); } Console.WriteLine("Get row with filter succeed."); }
public static void GetRangeWithFilter() { Console.WriteLine("Start get range with filter ..."); PrepareTable(); PrepareData(); OTSClient otsClient = Config.GetClient(); // 读取 (0, INF_MIN)到(100, INF_MAX)这个范围内的所有行,且col2等于false的行 PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey { { "pk0", new ColumnValue(0) }, { "pk1", ColumnValue.INF_MIN } }; PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey { { "pk0", new ColumnValue(100) }, { "pk1", ColumnValue.INF_MAX } }; // 构造列过滤条件 var condition = new RelationalCondition("col2", CompareOperator.EQUAL, new ColumnValue(false)); var queryCriteria = new RangeRowQueryCriteria(TableName) { Direction = GetRangeDirection.Forward, InclusiveStartPrimaryKey = inclusiveStartPrimaryKey, ExclusiveEndPrimaryKey = exclusiveEndPrimaryKey, Filter = condition.ToFilter() }; GetRangeResponse response = otsClient.GetRange(new GetRangeRequest(queryCriteria)); IList <Row> rows = response.RowDataList; PrimaryKey nextStartPrimaryKey = response.NextPrimaryKey; while (nextStartPrimaryKey != null) { queryCriteria = new RangeRowQueryCriteria(TableName) { Direction = GetRangeDirection.Forward, InclusiveStartPrimaryKey = nextStartPrimaryKey, ExclusiveEndPrimaryKey = exclusiveEndPrimaryKey, Filter = condition.ToFilter() }; response = otsClient.GetRange(new GetRangeRequest(queryCriteria)); nextStartPrimaryKey = response.NextPrimaryKey; foreach (var row in response.RowDataList) { rows.Add(row); } } foreach (var row in rows) { PrintRow(row); } Console.WriteLine("TotalRowsRead with filter: " + rows.Count); }
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); } }