public async Task WhenICheckAndDeleteCellsWithTimeStampAndCellsToDeleteICanAddWithHigherTimestamp() { var client = GetClient(); var cellSets = CreateCellSet(GetCellSet("3", "c1", "1A", 10), GetCellSet("3", "c2", "1A", 20)); await client.StoreCellsAsync(_tableName, cellSets); // Deletes all the ColumnFamily with timestamp less than 10 var rowToDelete = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8("3") }; rowToDelete.Values.AddRange(cellSets.Rows.SelectMany(row => row.Values)); var deleted = await client.CheckAndDeleteAsync(_tableName, cellSets.Rows[0].Values[0], rowToDelete); deleted.ShouldEqual(true); Assert.True(await client.GetCellsAsync(_tableName, "3") == null, "expecting Get '3' to fail as all cells are removed"); client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11))).Wait(); var retrievedCells = await client.GetCellsAsync(_tableName, "3"); Assert.NotNull(retrievedCells); retrievedCells.Rows[0].Values.Count.ShouldEqual(1); retrievedCells.Rows[0].Values[0].Column.ToStringUtf8().ShouldBeEqualOrdinalIgnoreCase("c1"); }
public async Task WhenICheckAndDeleteCellsWithColumnFamilyDeletesAllCells() { var client = GetClient(); var cellSets = CreateCellSet(GetCellSet("3", "c1", "1A", 10), GetCellSet("3", "c2", "1A", 20)); await client.StoreCellsAsync(_tableName, cellSets); // Deletes all the ColumnFamily with timestamp less than 10 var rowToDelete = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8("3") }; rowToDelete.Values.Add(new Cell { Row = rowToDelete.Key, Column = ByteString.CopyFromUtf8(ColumnFamilyName1), Timestamp = 10 }); var deleted = await client.CheckAndDeleteAsync(_tableName, cellSets.Rows[1].Values[0], rowToDelete); deleted.ShouldEqual(true); Assert.True(await client.GetCellsAsync(_tableName, "3") == null, "expecting Get '3' to fail as all cells are removed"); client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11))).Wait(); try { var retrievedCells = client.GetCellsAsync(_tableName, "3").Result; retrievedCells.Rows[0].Values.Count.ShouldEqual(1); } catch (AggregateException ex) when(ex.InnerException is HttpRequestException exception) { exception.Message.ShouldContain("404"); } }
public void TestStoreSingleCell() { const string testKey = "content"; const string testValue = "the force is strong in this column"; var client = CreateClient(); var set = new CellSet(); var row = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8(testKey) }; set.Rows.Add(row); var value = new Cell { Column = ByteString.CopyFromUtf8("d:starwars"), Data = ByteString.CopyFromUtf8(testValue) }; row.Values.Add(value); client.StoreCellsAsync(TestTableName, set).Wait(); var cells = client.GetCellsAsync(TestTableName, testKey).Result; Assert.Single(cells.Rows); Assert.Single(cells.Rows[0].Values); Assert.Equal(testValue, cells.Rows[0].Values[0].Data.ToStringUtf8()); }
public async Task TestCellsDeletion() { const string testKey = "content"; const string testValue = "the force is strong in this column"; var client = CreateClient(); var set = new CellSet(); var row = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8(testKey) }; set.Rows.Add(row); var value = new Cell { Column = ByteString.CopyFromUtf8("d:starwars"), Data = ByteString.CopyFromUtf8(testValue) }; row.Values.Add(value); client.StoreCellsAsync(TestTableName, set).Wait(); var cell = client.GetCellsAsync(TestTableName, testKey).Result; // make sure the cell is in the table Assert.Equal(Encoding.UTF8.GetString(cell.Rows[0].Key.ToByteArray()), testKey); // delete cell client.DeleteCellsAsync(TestTableName, testKey).Wait(); Assert.Null(await client.GetCellsAsync(TestTableName, testKey)); }
/// <summary> /// Automically checks if a row/family/qualifier value matches the expected value and deletes /// </summary> /// <param name="table">the table</param> /// <param name="cellToCheck">cell to check for deleting the row</param> /// <param name="rowToDelete"></param> /// <returns>true if the record was deleted; false if condition failed at check</returns> public Task <bool> CheckAndDeleteAsync(string table, Cell cellToCheck, CellSet.Types.Row rowToDelete = null) { table.ArgumentNotNullNorEmpty(nameof(table)); cellToCheck.ArgumentNotNull(nameof(cellToCheck)); if (rowToDelete == null) { return(CheckAndDeleteAsyncInternal(cellToCheck, cellToCheck.Row)); } rowToDelete.Values.ArgumentNotNullNorEmpty(nameof(rowToDelete)); if (rowToDelete.Values.Count == 1) { return(CheckAndDeleteAsyncInternal(rowToDelete.Values[0], rowToDelete.Key)); } return(Task.WhenAll(rowToDelete.Values.Select(cell => CheckAndDeleteAsyncInternal(cell, rowToDelete.Key))) .ContinueWith(tasks => tasks.GetAwaiter().GetResult().All(_ => _))); Task <bool> CheckAndDeleteAsyncInternal(Cell cell, ByteString key) { var row = new CellSet.Types.Row { Key = key }; row.Values.Add(cell); var cellSet = new CellSet(); cellSet.Rows.Add(row); return(StoreCellsAsyncInternal(table, cellSet, Encoding.UTF8.GetString(row.Key.ToByteArray()), CheckAndDeleteQuery)); } }
/// <summary> /// Atomically checks if a row/family/qualifier value matches the expected value and updates /// </summary> /// <param name="table">the table</param> /// <param name="row">row to update</param> /// <param name="cellToCheck">cell to check</param> /// <returns>true if the record was updated; false if condition failed at check</returns> public Task <bool> CheckAndPutAsync(string table, CellSet.Types.Row row, Cell cellToCheck) { table.ArgumentNotNullNorEmpty(nameof(table)); row.ArgumentNotNull(nameof(row)); row.Values.Add(cellToCheck); var cellSet = new CellSet(); cellSet.Rows.Add(row); return(StoreCellsAsyncInternal(table, cellSet, Encoding.UTF8.GetString(row.Key.ToByteArray()), CheckAndPutQuery)); }
private void PopulateTable() { var client = new HBaseClient(RequestOptionsFactory.GetDefaultOptions()); var cellSet = new CellSet(); var id = Guid.NewGuid().ToString("N"); for (var lineNumber = 0; lineNumber < 10; ++lineNumber) { var rowKey = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", id, lineNumber); // add to expected records var rec = new FilterTestRecord(rowKey, lineNumber, Guid.NewGuid().ToString("N"), Guid.NewGuid().ToString("D")); _allExpectedRecords.Add(rec); // add to row var row = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8(rec.RowKey) }; var lineColumnValue = new Cell { Column = BuildCellColumn(ColumnFamilyName1, LineNumberColumnName), Data = ByteString.CopyFrom(BitConverter.GetBytes(rec.LineNumber)) }; row.Values.Add(lineColumnValue); var paragraphColumnValue = new Cell { Column = BuildCellColumn(ColumnFamilyName1, ColumnNameA), Data = ByteString.CopyFromUtf8(rec.A) }; row.Values.Add(paragraphColumnValue); var columnValueB = new Cell { Column = BuildCellColumn(ColumnFamilyName2, ColumnNameB), Data = ByteString.CopyFromUtf8(rec.B) }; row.Values.Add(columnValueB); cellSet.Rows.Add(row); } client.StoreCellsAsync(_tableName, cellSet).Wait(); }
private void StoreTestData(IHBaseClient hbaseClient) { // we are going to insert the keys 0 to 100 and then do some range queries on that const string testValue = "the force is strong in this column"; var set = new CellSet(); for (var i = 0; i < 100; i++) { var row = new CellSet.Types.Row { Key = ByteString.CopyFrom(BitConverter.GetBytes(i)) }; var value = new Cell { Column = ByteString.CopyFromUtf8("d:starwars"), Data = ByteString.CopyFromUtf8(testValue) }; row.Values.Add(value); set.Rows.Add(row); } hbaseClient.StoreCellsAsync(TestTableName, set).Wait(); }
private CellSet.Types.Row GetCellSet(string key, string columnName, string value, long timestamp) { var row = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8(key) }; var c1 = new Cell { Column = BuildCellColumn(ColumnFamilyName1, columnName), Row = row.Key }; if (value != null) { c1.Data = ByteString.CopyFromUtf8(value); } if (timestamp > 0) { c1.Timestamp = timestamp; } row.Values.Add(c1); return(row); }
public void TestGetCellsWithMultiGetRequest() { var testKey1 = Guid.NewGuid().ToString(); var testKey2 = Guid.NewGuid().ToString(); var testValue1 = "the force is strong in this Column " + testKey1; var testValue2 = "the force is strong in this Column " + testKey2; var client = CreateClient(); var set = new CellSet(); var row1 = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8(testKey1) }; var row2 = new CellSet.Types.Row { Key = ByteString.CopyFromUtf8(testKey2) }; set.Rows.Add(row1); set.Rows.Add(row2); var value1 = new Cell { Column = ByteString.CopyFromUtf8("d:starwars"), Data = ByteString.CopyFromUtf8(testValue1) }; var value2 = new Cell { Column = ByteString.CopyFromUtf8("d:starwars"), Data = ByteString.CopyFromUtf8(testValue2) }; row1.Values.Add(value1); row2.Values.Add(value2); client.StoreCellsAsync(TestTableName, set).Wait(); var cells = client.GetCellsAsync(TestTableName, new[] { testKey1, testKey2 }).Result; Assert.Equal(2, cells.Rows.Count); Assert.Single(cells.Rows[0].Values); Assert.Equal(testValue1, cells.Rows[0].Values[0].Data.ToStringUtf8()); Assert.Single(cells.Rows[1].Values); Assert.Equal(testValue2, cells.Rows[1].Values[0].Data.ToStringUtf8()); }