public void GetDataDynamoDbEmployee() { GetItemRequest request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { N = "3" } }, { "EmployeeName", new AttributeValue { S = "Mehmet Karagöz" } } } }; var response = client.GetItem(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { if (response.Item.Count > 0) { foreach (var emp in response.Item) { Console.WriteLine($"Key =>{emp.Key}, Value =>{emp.Value.S} {emp.Value.N}"); } } else { Console.WriteLine("Item Not Found"); } } Console.ReadLine(); }
private bool TryGetValue <T>(string key, out T entry) { Log.InfoFormat("Attempting cache get of key: {0}", key); entry = default(T); var response = _client.GetItem( new GetItemRequest { TableName = CacheTableName, Key = new Key { HashKeyElement = new AttributeValue { S = key } } } ); var item = response.GetItemResult.Item; if (item != null) { DateTime expiresAt = Convert.ToDateTime(item[ExpiresAtName].S); if (DateTime.UtcNow > expiresAt) { Log.InfoFormat("Cache key: {0} has expired, removing from cache!", key); Remove(key); return(false); } string jsonData = item[ValueName].S; entry = jsonData.FromJson <T>(); Log.InfoFormat("Cache hit on key: {0}", key); return(true); } return(false); }
public void TestExceptionHandler() { using (var client = new AmazonDynamoDBClient(new AnonymousAWSCredentials(), RegionEndpoint.USEast1)) { _handler.AddEventHandler(client); CustomResponses.SetResponse(client, (request) => { throw new InvalidOperationException(); }); _recorder.BeginSegment("test", TraceId); var segment = TraceContext.GetEntity(); try { client.GetItem( "test", new Dictionary <string, AttributeValue>() { { "invalid_key", new AttributeValue("1") } }); Assert.Fail(); } catch (InvalidOperationException e) { Assert.ReferenceEquals(e, segment.Subsegments[0].Cause.ExceptionDescriptors[0].Exception); Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("table_name")); Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("consistent_read")); Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("projection_expression")); Assert.IsTrue(segment.Subsegments[0].Aws.ContainsKey("attribute_names_substituted")); } finally { _recorder.EndSegment(); } } }
private static void GetSampleProduct(int id, string tableName) { switch (provider) { case Provider.NOSQL: var request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "Id", new AttributeValue { N = id.ToString() } } }, ReturnConsumedCapacity = "TOTAL" }; var response = client.GetItem(request); //Console.WriteLine("No. of reads used (by get book item) {0}\n", response.ConsumedCapacity.CapacityUnits); //PrintItem(response.Item); break; case Provider.MSSQL: var product = new Product().GetProduct(id); //PrintItem(product); break; } }
private static void RetrieveItem(string partitionKey, string sortKey) { var request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "Id", new AttributeValue { S = partitionKey } }, { "ReplyDateTime", new AttributeValue { S = sortKey } } }, ConsistentRead = true }; var response = client.GetItem(request); // Check the response. var attributeList = response.Item; // attribute list in the response. Console.WriteLine("\nPrinting item after retrieving it ............"); PrintItem(attributeList); }
public void GetItem() { GetItemRequest request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { N = "3" } }, { "Username", new AttributeValue { S = "admin" } } } }; var response = client.GetItem(request); if (response.HttpStatusCode.IsSuccess()) { if (response.Item.Count > 0) { Console.WriteLine("Item(s) retrived successfully \n"); foreach (var item in response.Item) { Console.WriteLine($"Key: {item.Key}, Value: {item.Value.N}, {item.Value.S}"); } } else { Console.WriteLine("Not found! \n"); } } }
private bool TryGetValue <T>(string key, out T entry) { Log.InfoFormat("Attempting cache get of key: {0}", key); entry = default(T); var itemKey = new Dictionary <string, AttributeValue> { { KeyName, new AttributeValue() { S = key } } }; var response = _client.GetItem(CacheTableName, itemKey); var item = response.Item; if (item != null) { DateTime expiresAt = Convert.ToDateTime(item[ExpiresAtName].S); if (DateTime.UtcNow > expiresAt) { Log.InfoFormat("Cache key: {0} has expired, removing from cache!", key); Remove(key); return(false); } string jsonData = item[ValueName].S; entry = jsonData.FromJson <T>(); Log.InfoFormat("Cache hit on key: {0}", key); return(true); } return(false); }
protected void Page_Load(object sender, EventArgs e) { AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig(); clientConfig.RegionEndpoint = RegionEndpoint.USEast1; AmazonDynamoDBClient client = new AmazonDynamoDBClient(clientConfig); //string tableName = "PF_TEAM_DATA_2019"; string tableName = "PF_EVENT_DATA_2019"; var request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "event_key", new AttributeValue { S = "2019code" } } }, ConsistentRead = true }; var response = client.GetItem(request); // Check the response. //var result = response.GetItemResult(); var attributeMap = response.Item; // Attribute list in the response. string test = "itemgrabbed"; //This line is being added just to test commits //another test //// View response //System.Diagnostics.Debug.WriteLine("Item:"); //var JSONObj = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(attributeMap); //string lastTableNameEvaluated = null; //do //{ // var request = new ListTablesRequest // { // Limit = 2, // ExclusiveStartTableName = lastTableNameEvaluated // }; // var response = client.ListTables(request); // foreach (string name in response.TableNames) // System.Diagnostics.Debug.WriteLine(name); // lastTableNameEvaluated = response.LastEvaluatedTableName; //} while (lastTableNameEvaluated != null); }
/* Returns a String containing the JSON object by ID */ public static String GetScore(int id) { var request = new GetItemRequest { TableName = table, Key = new Dictionary <string, AttributeValue>() { { "ScoreID", new AttributeValue { N = id.ToString() } } } }; var response = client.GetItem(request); return(PrintItem(response.Item)); }
public static string GetPharmaInfo(string drugName) { string pharmaInfo = null; var request = new GetItemRequest { TableName = PharmaTableName, Key = new Dictionary <string, AttributeValue>() { { "DrugName", new AttributeValue { S = drugName } } }, }; GetItemResponse response = dynamoDBClient.GetItem(request); pharmaInfo = response.Item["Usage"].S.ToString(); return(pharmaInfo); }
private static void LoadItem(AmazonDynamoDBClient client) { var response = client.GetItem( TargetTableName, new Dictionary <string, AttributeValue> { { JobKey.BatchId, new AttributeValue { S = _requestBatchId } } } ); foreach (var v in response.Item) { Console.WriteLine($"Key: {v.Key}"); Console.WriteLine($"Value: {v.Value.S}{v.Value.N}"); } Console.ReadKey(); }
public Organization Get(Guid id) { AmazonDynamoDBClient client = DynamoUtilities.InitializeClient(); using (client) { GetItemRequest request = CreateGetItemRequest(id); GetItemResponse response = client.GetItem(request); if (response.Item == null) { string errorMessage = string.Format(ErrorMessages.MisingResponseItem, "Get Organization"); throw new MissingFieldException(errorMessage); } Organization returnValue = DynamoUtilities.GetItemFromAttributeStore <Organization>(response.Item); return(returnValue); } }
private static void RetrieveItem() { var request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "Id", new AttributeValue { N = "1000" } } }, ProjectionExpression = "Id, ISBN, Title, Authors", ConsistentRead = true }; var response = client.GetItem(request); // Check the response. var attributeList = response.Item; // attribute list in the response. Console.WriteLine("\nPrinting item after retrieving it ............"); PrintItem(attributeList); }
private static void GetBook(int id, string tableName) { var request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "Id", new AttributeValue { N = id.ToString() } } }, ReturnConsumedCapacity = "TOTAL" }; var response = client.GetItem(request); Console.WriteLine("No. of reads used (by get book item) {0}\n", response.ConsumedCapacity.CapacityUnits); PrintItem(response.Item); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); }
public static string RetrieveItem(string url) { try { var client = new AmazonDynamoDBClient(); string tableName = "WordRankerTable"; var request = new GetItemRequest { TableName = tableName, Key = new Dictionary <string, AttributeValue>() { { "Id", new AttributeValue { N = url } } } }; var response = client.GetItem(request); Dictionary <string, AttributeValue> item = response.Item; string result = ""; foreach (var keyValuePair in item) { result = keyValuePair.Value.S; } return(result); } catch (AmazonDynamoDBException e) { return(""); } }
public void Can_put_item_with_null_value() { var db = new AmazonDynamoDBClient("keyId", "key", new AmazonDynamoDBConfig { ServiceURL = "http://localhost:8000", }); try { db.DeleteTable(new DeleteTableRequest { TableName = "Test", }); Thread.Sleep(1000); } catch (Exception) { /*ignore*/ } db.CreateTable(new CreateTableRequest { TableName = "Test", KeySchema = new List <KeySchemaElement> { new KeySchemaElement { AttributeName = "Id", KeyType = "HASH" } }, AttributeDefinitions = new List <AttributeDefinition> { new AttributeDefinition { AttributeName = "Id", AttributeType = "N", } }, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 10, WriteCapacityUnits = 5, } }); Thread.Sleep(1000); db.PutItem(new PutItemRequest { TableName = "Test", Item = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { N = "1" } }, { "Name", new AttributeValue { S = "Foo" } }, { "Empty", new AttributeValue { NULL = true } }, } }); var response = db.GetItem(new GetItemRequest { TableName = "Test", ConsistentRead = true, Key = new Dictionary <string, AttributeValue> { { "Id", new AttributeValue { N = "1" } } } }); Assert.That(response.IsItemSet); Assert.That(response.Item["Id"].N, Is.EqualTo("1")); Assert.That(response.Item["Name"].S, Is.EqualTo("Foo")); Assert.That(response.Item["Empty"].NULL); }
private static int invalidSequenceNumber = 0; // the number of packets arrived in invalid order public void start_client() { //Loop Flag bool continueLoop = true; String startRunTime = DateTime.Now.ToString(); DateTime startTime = DateTime.Now; int startPacketNum = 0; // to initialize exchange with DynamoDB var request = new PutItemRequest { TableName = tableName, Item = new Dictionary <string, AttributeValue>() { { "SerieID", new AttributeValue { S = "1" } }, { "SnapshotSeqNum", new AttributeValue { N = "1" /*(m_Count + 1).ToString()*/ } }, // short string up to 60 bytes { "Snapshot", new AttributeValue { S = "startup" } } } }; amazonClient.PutItem(request); while (continueLoop) { //Send DataGram //Format: Hostname@Post@Message //System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding(); //string sendString = "localhost@" + port.ToString() + "@Send Date Time"; //byte[] sendData = encode.GetBytes(sendString); //Send to Server //client.Send(sendData, sendData.Length, "localhost", 6767); //Receive DataGram from Server byte[] recData = client.Receive(ref receivePoint); packetNumber++; String recS = Encoding.ASCII.GetString(recData); int packetSeqNum = Int32.Parse(recS.Substring(0, 10)); if (packetSeqNum > lastPacketSecNumber) { if (lastPacketSecNumberInitialized) { lostPacketNumber += (packetSeqNum - lastPacketSecNumber) - 1; } else { startPacketNum = packetSeqNum; } lastPacketSecNumber = packetSeqNum; lastPacketSecNumberInitialized = true; } else { invalidSequenceNumber++; } String sIn = ""; MemoryStream m = new MemoryStream(recData); m.Seek(0, System.IO.SeekOrigin.Begin); for (int i = 0; i < recData.Length; i++) { Int32 b = m.ReadByte(); sIn += b.ToString("X2"); } DateTime begin = DateTime.Now; TimeSpan delta; request = new PutItemRequest { TableName = tableName, Item = new Dictionary <string, AttributeValue>() { { "SerieID", new AttributeValue { S = "1" } }, { "SnapshotSeqNum", new AttributeValue { N = "1" /*(m_Count + 1).ToString()*/ } }, // short string up to 60 bytes { "Snapshot", new AttributeValue { S = sIn } } } }; amazonClient.PutItem(request); var r1 = new GetItemRequest { TableName = tableName, Key = new Key { RangeKeyElement = new AttributeValue { N = "1" /*(m_Count + 1).ToString()*/ }, HashKeyElement = new AttributeValue { S = "1" } }, // Optional parameters. AttributesToGet = new List <string>() { "Snapshot", "SnapshotSeqNum" }, ConsistentRead = true }; var response = amazonClient.GetItem(r1); DateTime end = DateTime.Now; delta = end.Subtract(begin); int replyMs = delta.Hours * 3600 * 1000 + delta.Minutes * 60000 + delta.Seconds * 1000 + delta.Milliseconds; if (replyMs > maxReplyMs) { maxReplyMs = replyMs; } replyMsSum += replyMs; meanReplyMs = replyMsSum / packetNumber; // Check the response. var result = response.GetItemResult; Dictionary <String, AttributeValue> attributeMap = result.Item; AttributeValue a = attributeMap["Snapshot"]; String pic = a.S; Byte[] imgData = new Byte[pic.Length / 2]; Int32 byteIndex = 0; for (int i = 0; i < pic.Length; i += 2) { String hex = pic.Substring(i, 2); Byte b = Byte.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier); imgData[byteIndex++] = b; } // once per second outputs statistical information DateTime now = DateTime.Now; delta = now.Subtract(startTime); if (delta.Seconds != 0) { startTime = now; logBox.Text = "Start time = " + startRunTime + "\r\n" + "Start packet number = " + startPacketNum.ToString() + "\r\n" + "Packet number per sec = " + (packetNumber - prevPacketNumber).ToString() + "\r\n" + "Lost packet number = " + lostPacketNumber.ToString() + "\r\n" + "Max DynamoDB cycle = " + maxReplyMs.ToString() + "\r\n" + "Mean DynamoDB cycle = " + meanReplyMs.ToString() + "\r\n" + "Invalid sequence number = " + invalidSequenceNumber.ToString() + "\r\n"; prevPacketNumber = packetNumber; } } }
public void CRUDSamples() { EnsureTables(); PutSample(); { #region GetItem Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item key // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "The Adventures of Tom Sawyer" Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Author", new AttributeValue { S = "Mark Twain" } }, { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } } }; // Create GetItem request GetItemRequest request = new GetItemRequest { TableName = "SampleTable", Key = key, }; // Issue request var result = client.GetItem(request); // View response Console.WriteLine("Item:"); Dictionary <string, AttributeValue> item = result.Item; foreach (var keyValuePair in item) { Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS ?? new List <string>()), string.Join(", ", keyValuePair.Value.NS ?? new List <string>())); } #endregion } { #region UpdateItem Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item key // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "The Adventures of Tom Sawyer" Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Author", new AttributeValue { S = "Mark Twain" } }, { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } } }; // Define attribute updates Dictionary <string, AttributeValueUpdate> updates = new Dictionary <string, AttributeValueUpdate>(); // Update item's Setting attribute updates["Setting"] = new AttributeValueUpdate() { Action = AttributeAction.PUT, Value = new AttributeValue { S = "St. Petersburg, Missouri" } }; // Remove item's Bibliography attribute updates["Bibliography"] = new AttributeValueUpdate() { Action = AttributeAction.DELETE }; // Add a new string to the item's Genres SS attribute updates["Genres"] = new AttributeValueUpdate() { Action = AttributeAction.ADD, Value = new AttributeValue { SS = new List <string> { "Bildungsroman" } } }; // Create UpdateItem request UpdateItemRequest request = new UpdateItemRequest { TableName = "SampleTable", Key = key, AttributeUpdates = updates }; // Issue request client.UpdateItem(request); #endregion } { #region DeleteItem Sample // Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item key // Hash-key of the target item is string value "Mark Twain" // Range-key of the target item is string value "The Adventures of Tom Sawyer" Dictionary <string, AttributeValue> key = new Dictionary <string, AttributeValue> { { "Author", new AttributeValue { S = "Mark Twain" } }, { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } } }; // Create DeleteItem request DeleteItemRequest request = new DeleteItemRequest { TableName = "SampleTable", Key = key }; // Issue request client.DeleteItem(request); #endregion } }