Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查询DynomoDB并以字符串形式返回键值对_C#_Logging_Amazon Dynamodb - Fatal编程技术网

C# 如何查询DynomoDB并以字符串形式返回键值对

C# 如何查询DynomoDB并以字符串形式返回键值对,c#,logging,amazon-dynamodb,C#,Logging,Amazon Dynamodb,我试图查询一个DynamoDB,它是我使用Amazon文档中的代码创建的,只做了一些简单的修改。我试图获取我得到的数据,并将其作为字符串写入日志文件。但我能得到的似乎是: 2013-02-22 20:21:37.9268 | Trace |[System.Collections.Generic.Dictionary2+KeyCollection[System.String,Amazon.DynamoDB.Model.AttributeValue]System.Collections.Generi

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

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.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);
     }

  }
private static void GetCallsForRange()
{
AmazonDynamoDBConfig=新的AmazonDynamoDBConfig();
config.ServiceURL=”http://dynamodb.us-west-2.amazonaws.com";
AmazonDynamoDBClient=新的AmazonDynamoDBClient(配置);
DateTime startDate=DateTime.Today.AddDays(-21);
字符串start=startDate.ToString(“G”,DateTimeFormatInfo.InvariantInfo);
DateTime endDate=DateTime.Today;
字符串end=endDate.ToString(“G”,DateTimeFormatInfo.InvariantInfo);
QueryRequest请求=新建QueryRequest
{
TableName=“查询”,
HashKeyValue=new AttributeValue{S=“+1555555”},
RangeKeyCondition=新条件
{
ComparisonOperator=“BETWEEN”,
AttributeValueList=新列表()
{
新属性值{S=start},
新属性值{S=end}
}
}
};
QueryResponse response=client.Query(请求);
QueryResult结果=response.QueryResult;
foreach(响应中的字典项.QueryResult.Items)
{
string logMsg=string.Format(“[{0}{1}]”,item.Keys,item.Values);
Logging.LogTrace(logMsg);
}
}

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

foreach(响应中的字典项.QueryResult.Items)
{
后勤项目(项目);
}
私有void登录项(字典属性列表)
{
foreach(属性列表中的KeyValuePair kvp)
{
字符串attributeName=kvp.Key;
AttributeValue值=千伏值;
字符串日志值=
(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());
字符串logMsg=string.Format(“[{0}{1}]”,attributeName,logValue);
Logging.LogTrace(logMsg);
}
}
本质上,您需要发现
属性值的“类型”(String、Number、Binary、StringSet、NumberSet、BinarySet),然后将其输出到日志中


我希望这有帮助

是的,这很有帮助!现在就这么近了。我对这个还是有点陌生,所以我不太明白。我似乎遇到的唯一问题是LogItem(item)抛出一个错误,说“非静态字段需要对象引用”。LogItem应该是一个静态方法吗?这一行有一个错误:(value.BS==null?”:string.Join(“,”,value.BS.ToArray());“以下方法或属性之间的调用不明确:'string.Join(string,System.Collections.Generic.IEnumerable)'和'string.Join(string,params object[])”。抱歉,是的,该方法应该是静态的。”。我忽略了你原来的方法是静态的这一事实。
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);
     }
}