7

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?

1
  • Did you find a nice way to do this? Using the DynamoDBContext or something similar... Commented Apr 8, 2014 at 16:20

4 Answers 4

33

I ended up using something like:

using System.Linq;

...

// Issue request
QueryResponse result = AmazonDynamoDBClient.Query(request);

var items = result.Items.FirstOrDefault();

var doc = Document.FromAttributeMap(items);
   
var myModel = DynamoDBContext.FromDocument<MyModelType>(doc);
Sign up to request clarification or add additional context in comments.

9 Comments

Awesome, It worked for me. I think it should be the accepted answer.
This should DEFINITELY be marked as the accepted answer! It was so hard to find the answer for something I'd have thought would have been more obvious. Note the only difference to the code above is that .FromDocument is a method on an instance of DynamoDBContext. Otherwise, it's perfect!
Finally found it!! This was real hard to find, thanks so much @lee
This does work but when using the FromDocument method I'm seeing that it also throws up a DescribeTable request, anyone else having this?
Could someone answer my question here: stackoverflow.com/questions/47269542/…. I have read your answer, but QueryResponse.Items.FirstOrDefault() does not appear to exist.
|
1

What you want is some sort of ORM - a nice read is Using Amazon DynamoDB Object Persistence Framework. Also check out the API reference that also shows samples

1 Comment

yeah, but the problem is that i cant use the persistence framework when im querying a indexed table, i use de context.query<myobject>(_HashValue, queryoperator (queryoperator.equal), value); its returning an exception (Index cant not be null) or something like that... thanks for your time
0

Take a look at the examples from Querying Tables Using the AWS SDK for .NET Low-Level API documentation

In your handling method

var response = client.Query(request);
var result = response.QueryResult;

foreach (Dictionary<string, AttributeValue> item in response.QueryResult.Items)
{
  // Process the result.
  PrintItem(item);
} 

PrintItem implementation

private static void PrintItem(Dictionary<string, AttributeValue> attributeList)
    {
      foreach (KeyValuePair<string, AttributeValue> kvp in attributeList)
      {
        string attributeName = kvp.Key;
        AttributeValue value = kvp.Value;

        Console.WriteLine(
            attributeName + " " +
            (value.S == null ? "" : "S=[" + value.S + "]") +
            (value.N == null ? "" : "N=[" + value.N + "]") +
            (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") +
            (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]")
        );
      }
      Console.WriteLine("************************************************");
    }

1 Comment

Yeah, i was thinking making a method to parse it manually, but i wanted to skip it.... the persistent framework parse the items to a native object type
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.