/// <summary> /// 通过多个参数构造一个新的<see cref="GetRangeRequest"/> /// </summary> /// <param name="tableName">表名称</param> /// <param name="direction">前向还是后向</param> /// <param name="inclusiveStartPrimaryKey">区间开始位置,包含</param> /// <param name="exclusiveEndPrimaryKey">区间结束位置,不包含</param> /// <param name="columnsToGet">返回的列名称的列表</param> /// <param name="limit">最大返回数</param> public GetRangeRequest(string tableName, GetRangeDirection direction, PrimaryKey inclusiveStartPrimaryKey, PrimaryKey exclusiveEndPrimaryKey, HashSet <string> columnsToGet = null, int?limit = null, IColumnCondition condition = null) { QueryCriteria = new RangeRowQueryCriteria(tableName) { Direction = direction, Limit = limit, InclusiveStartPrimaryKey = inclusiveStartPrimaryKey, ExclusiveEndPrimaryKey = exclusiveEndPrimaryKey }; if (columnsToGet != null) { QueryCriteria.SetColumnsToGet(columnsToGet); } if (condition != null) { QueryCriteria.Filter = condition.ToFilter(); } }
private static ByteString BuildFilter(IColumnCondition filter) { PB.Filter.Builder builder = PB.Filter.CreateBuilder(); builder.SetType(EncodeFilterType(filter.GetConditionType())); builder.SetFilter_(filter.Serialize()); return(builder.Build().ToByteString()); }
/// <summary> /// 通过多个参数构造一个新的<see cref="GetRangeRequest"/> /// </summary> /// <param name="tableName">表名称</param> /// <param name="direction">前向还是后向</param> /// <param name="inclusiveStartPrimaryKey">区间开始位置,包含</param> /// <param name="exclusiveEndPrimaryKey">区间结束位置,不包含</param> /// <param name="consumedCapacityUnitCounter">用户传入的CapacityUnit消耗计数器。</param> /// <param name="columnsToGet">返回的列名称的列表</param> /// <param name="limit">最大返回数</param> public GetIteratorRequest(string tableName, GetRangeDirection direction, PrimaryKey inclusiveStartPrimaryKey, PrimaryKey exclusiveEndPrimaryKey, CapacityUnit consumedCapacityUnitCounter, HashSet <string> columnsToGet = null, int?limit = null, IColumnCondition condition = null) : base(tableName, direction, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, columnsToGet, limit, condition) { ConsumedCapacityUnitCounter = consumedCapacityUnitCounter; }
public void CheckSingleRow(string tableName, PrimaryKey primaryKey, AttributeColumns attribute, CapacityUnit expectCapacityUnitConsumed = null, HashSet <string> columnsToGet = null, bool isEmpty = false, IColumnCondition condition = null) { var request = new GetRowRequest(tableName, primaryKey, columnsToGet, condition); var response = OTSClient.GetRow(request); PrimaryKey primaryKeyToExpect; AttributeColumns attributeToExpect; if (isEmpty) { primaryKeyToExpect = new PrimaryKey(); attributeToExpect = new AttributeColumns(); } else if (columnsToGet == null || columnsToGet.Count == 0) { primaryKeyToExpect = primaryKey; attributeToExpect = attribute; } else { primaryKeyToExpect = primaryKey; attributeToExpect = new AttributeColumns(); foreach (var columnName in columnsToGet) { if (attribute.ContainsKey(columnName)) { attributeToExpect.Add(columnName, attribute[columnName]); } } } AssertColumns(primaryKeyToExpect, response.PrimaryKey); AssertColumns(attributeToExpect, response.Attribute); if (expectCapacityUnitConsumed != null) { AssertCapacityUnit(expectCapacityUnitConsumed, response.ConsumedCapacityUnit); } }
public IEnumerable <Row> GetRangeIterator( string tableName, GetRangeDirection direction, PrimaryKey inclusiveStartPrimaryKey, PrimaryKey exclusiveEndPrimaryKey, CapacityUnit consumedCapacityUnitCounter, HashSet <string> columnsToGet = null, int?count = null, IColumnCondition condition = null) { int?leftCount = count; if (leftCount != null && leftCount < 0) { throw new OTSClientException("the value of count must be larger than 0"); } PrimaryKey nextStartPrimaryKey = inclusiveStartPrimaryKey; while (nextStartPrimaryKey != null) { var request = new GetRangeRequest( tableName, direction, nextStartPrimaryKey, exclusiveEndPrimaryKey, columnsToGet, leftCount, condition); var response = GetRange(request); consumedCapacityUnitCounter.Read += response.ConsumedCapacityUnit.Read; nextStartPrimaryKey = response.NextPrimaryKey; foreach (var rowData in response.RowDataList) { yield return(rowData); } if (leftCount != null) { leftCount -= response.RowDataList.Count; if (leftCount <= 0) { break; } } } }
/// <summary> /// 添加一个表的多行读请求。 /// </summary> /// <param name="tableName">表名</param> /// <param name="primaryKeys">多行的主键</param> /// <param name="columnsToGet">要读取的列</param> /// <param name="condition">过滤条件</param> public void Add(string tableName, List <PrimaryKey> primaryKeys, HashSet <string> columnsToGet = null, IColumnCondition condition = null) { var rowQueryCriteria = new MultiRowQueryCriteria(tableName); rowQueryCriteria.SetRowKeys(primaryKeys); if (columnsToGet != null) { rowQueryCriteria.SetColumnsToGet(columnsToGet); } if (condition != null) { rowQueryCriteria.Filter = condition.ToFilter(); } rowQueryCriteriaDict[tableName] = rowQueryCriteria; }
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); } }
/// <summary> /// 构造一个新的<see cref="GetRowRequest" />实例。 /// </summary> /// <param name="tableName">表名称</param> /// <param name="primaryKey">主键</param> /// <param name="columnsToGet">获取的列名称列表,如果为空,则获取所有列</param> /// <param name="condition">过滤条件</param> public GetRowRequest(string tableName, PrimaryKey primaryKey, HashSet <string> columnsToGet = null, IColumnCondition condition = null, TimeRange timeRange = null, int?maxVersion = null, bool?cacheBlocks = null, string startColumn = null, string endColumn = null, byte[] token = null ) { QueryCriteria = new SingleRowQueryCriteria(tableName) { RowPrimaryKey = primaryKey }; if (columnsToGet != null) { QueryCriteria.SetColumnsToGet(columnsToGet); } if (condition != null) { QueryCriteria.Filter = condition.ToFilter(); } if (timeRange != null) { QueryCriteria.TimeRange = timeRange; } QueryCriteria.MaxVersions = maxVersion; QueryCriteria.CacheBlocks = cacheBlocks; QueryCriteria.StartColumn = startColumn; QueryCriteria.EndColumn = endColumn; QueryCriteria.Token = token; }
private bool DeleteRow(String tableName, Int64 pk, IColumnCondition cond) { var primaryKey = new PrimaryKey { { "PK0", new ColumnValue(pk) } }; Condition c = new Condition(RowExistenceExpectation.IGNORE) { ColumnCondition = cond }; var request = new DeleteRowRequest(tableName, c, primaryKey); bool success = true; try { OTSClient.DeleteRow(request); } catch (OTSServerException e) { Console.WriteLine("DeleteRow fail:{0}", e.ErrorMessage); success = false; } return(success); }
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); }
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 CompositeCondition AddCondition(IColumnCondition condition) { subConditions.Add(condition); return(this); }