parsing dynamo db queryresponse to object

Viewed 7002

I'm using DynamoDB to query a table with the following commands

QueryRequest request = new QueryRequest
{
   TableName = "Events",
   ExclusiveStartKey = startKey,
   KeyConditions = keyConditions,
   IndexName = "Title-index" // Specify the index to query against
};
// Issue request
QueryResponse result = client.Query(request);

The ExclusiveStartKey and Keyconditions are predefined

The issue is that the QueryResult result variable is not parsed to my native object, when I use the DynamoDB.Context you cast the method with the expected type, but in this case I need to parse the QueryResult... Is there any other way to do this? Or should I parse the object?

4 Answers

I was stuck on the same issue, and found that by using Table.Query with a QueryOperationConfig I could specify the index that I wanted to use. Because the Document object returned from this query has a "ToJson()" method on it, I was able to convert the results of the query to Json, and then use Json.Net to Deserialize it back into my object which worked flawlessly for my totally flat object.

var queryFilter = new QueryFilter(indexedColumnName, QueryOperator.Equal, targetValue);
Table table = Table.LoadTable(client, Configuration.Instance.DynamoTable);
var search = table.Query(new QueryOperationConfig { IndexName = indexName, Filter = queryFilter });

List<MyObject> objects = new List<MyObject>();
List<Document> documentSet = new List<Document>();
do
{
    documentSet = search.GetNextSetAsync().Result;
    foreach (var document in documentSet)
    {
        var record = JsonConvert.DeserializeObject<MyObject>(document.ToJson());
        objects .Add(record);
    }
} while (!search.IsDone);

Hope it helps you out!

Related