0

我正在尝试查询我使用 Amazon 文档中的代码创建的 DynamoDB,并进行了一些简单的修改。我正在尝试获取我获得的数据并将其作为字符串写入日志文件。但我似乎能得到的是:

2013-02-22 20:21:37.9268|跟踪|[System.Collections.Generic.Dictionary 2+KeyCollection[System.String,Amazon.DynamoDB.Model.AttributeValue] System.Collections.Generic.Dictionary2+ValueCollection[System.String,Amazon.DynamoDB.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);
     }

  }
4

1 回答 1

2

您将需要遍历response.QueryResult.Items. 您可以像这样重写循环(取自 Amazon DynamoDB 文档):

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) 的“类型”,然后将其输出到您的日志中。

我希望这会有所帮助!

于 2013-02-25T23:50:23.697 回答