如何查询DynomoDB并将键值对作为字符串返回



我正试图查询一个DynamoDB,该数据库是我使用亚马逊文档中的代码创建的,只需进行一些简单的修改。我正在尝试将我获得的数据作为字符串写入日志文件。但我所能得到的似乎是:

2013-02-22 20:21:37.9268|Trace|[System.Collections.Generic.Dictionary 2+KeyCollection[System.String,Amazon.DynamoDB.Model.AttributeValue] System.Collections.Generic.Dictionary 2+ValueCollection[System.String,Amazon.DynmoDB.Model.AttributeValue]]|

我尝试了一些不同的东西,但都返回了相同的东西,或者非常相似的东西。

我使用的代码:

  private static void GetCallsForRange()
  {
     AmazonDynamoDBConfig config = new AmazonDynamoDBConfig();
     config.ServiceURL = "http://dynamodb.us-west-2.amazonaws.com";
     AmazonDynamoDBClient client = new AmazonDynamoDBClient(config);

     DateTime startDate = DateTime.Today.AddDays(-21);
     string start = startDate.ToString("G", DateTimeFormatInfo.InvariantInfo);
     DateTime endDate = DateTime.Today;
     string end = endDate.ToString("G", DateTimeFormatInfo.InvariantInfo);
     QueryRequest request = new QueryRequest
     {
        TableName = "Inquiry",
        HashKeyValue = new AttributeValue { S = "+15555555555" },
        RangeKeyCondition = new Condition
        {
           ComparisonOperator = "BETWEEN",
           AttributeValueList = new List<AttributeValue>()
           {
              new AttributeValue { S = start },
              new AttributeValue { S = end }
           }
        }
     };

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

     foreach (Dictionary<string, AttributeValue> item in response.QueryResult.Items)
     {
        string logMsg = String.Format("[{0} {1}]", item.Keys, item.Values);
        Logging.LogTrace(logMsg);
     }
  }

您需要迭代response.QueryResult.Items中的每个项。你可以这样重写你的循环(取自AmazonDynamoDB文档):

foreach (Dictionary<string, AttributeValue> item in response.QueryResult.Items) 
{
     LogItem(item);
}
private void LogItem(Dictionary<string, AttributeValue> attributeList)
{
     foreach (KeyValuePair<string, AttributeValue> kvp in attributeList)
     {
          string attributeName = kvp.Key;
          AttributeValue value = kvp.Value;
          string logValue = 
               (value.S == null ? "" : value.S) +
               (value.N == null ? "" : value.N.ToString()) +
               (value.B == null ? "" : value.B.ToString()) +
               (value.SS == null ? "" : string.Join(",", value.SS.ToArray())) +
               (value.NS == null ? "" : string.Join(",", value.NS.ToArray())) +
               (value.BS == null ? "" : string.Join(",", value.BS.ToArray()));
          string logMsg = string.Format("[{0} {1}]", attributeName, logValue);
          Logging.LogTrace(logMsg);
     }
}

本质上,您需要发现AttributeValue的"类型"(String、Number、Binary、StringSet、NumberSet、BinarySet),然后将其输出到日志中。

我希望这能有所帮助!

相关内容

  • 没有找到相关文章

最新更新