GetItem() public method

The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data.

GetItem provides an eventually consistent read by default. If your application requires a strongly consistent read, set ConsistentRead to true. Although a strongly consistent read might take more time than an eventually consistent read, it always returns the last updated value.

/// An error occurred on the server side. /// /// Your request rate is too high. The AWS SDKs for DynamoDB automatically retry requests /// that receive this exception. Your request is eventually successful, unless your retry /// queue is too large to finish. Reduce the frequency of requests and use exponential /// backoff. For more information, go to Error /// Retries and Exponential Backoff in the Amazon DynamoDB Developer Guide. /// /// The operation tried to access a nonexistent table or index. The resource might not /// be specified correctly, or its status might not be ACTIVE. ///
public GetItem ( GetItemRequest request ) : GetItemResponse
request Amazon.DynamoDBv2.Model.GetItemRequest Container for the necessary parameters to execute the GetItem service method.
return Amazon.DynamoDBv2.Model.GetItemResponse
Beispiel #1
0
        public bool CheckUserIsExist(string userID)
        {
            var config = new AmazonDynamoDBConfig();
            GetItemResponse response;
            config.ServiceURL = System.Configuration.ConfigurationManager.AppSettings["ServiceURL"];
            client = new AmazonDynamoDBClient(config);
            bool retval = false;
            try
            {
                GetItemRequest request = new GetItemRequest
                {
                    TableName = "User",
                    Key = new Dictionary<string, AttributeValue>() { { "UserID", new AttributeValue { S = userID } } },
                    ReturnConsumedCapacity = "TOTAL"
                };
                response = client.GetItem(request);
                retval = response.Item.Count > 0;
            }
            catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
            catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }

            return retval;
        }
        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
            }
        }
Beispiel #3
0
        public Dictionary<string, object> GetUser(string userID)
        {
            Dictionary<string, object> retval = new Dictionary<string, object>();
            GetItemResponse response;
            var config = new AmazonDynamoDBConfig();
            config.ServiceURL = System.Configuration.ConfigurationManager.AppSettings["ServiceURL"];
            client = new AmazonDynamoDBClient(config);
            try
            {
                GetItemRequest request = new GetItemRequest
                {
                    TableName = "User",
                    Key = new Dictionary<string, AttributeValue>() { { "UserID", new AttributeValue { S = userID } } },
                    ReturnConsumedCapacity = "TOTAL"
                };
                response = client.GetItem(request);
                retval.Add("UserID", response.Item["UserID"].S);
                retval.Add("HeadshotURL", response.Item["HeadshotURL"].S);
                retval.Add("IsPayUser", Convert.ToBoolean(response.Item["IsPayUser"].S));
                retval.Add("UserName", response.Item["UserName"].S);
                retval.Add("RegistDate", response.Item["RegistDate"].N);
                retval.Add("LastLoginDate", response.Item["LastLoginDate"].N);
                retval.Add("LastSyncDate", response.Item["LastSyncDate"].N);
            }
            catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
            catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }

            return retval;
        }
        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);
        }