如何排除从DynamoDB QueryRequest返回的数据类型描述符?

时间:2019-06-20 17:35:41

标签: c# amazon-web-services amazon-dynamodb aws-sdk dynamodb-queries

我正在学习DynamoDB,并试图使用适用于AWS的.NET SDK运行查询。我用定义的QueryRequest设置了KeyConditionExpression,以便可以搜索分区键。

        var request = new QueryRequest
        {
            TableName = "MyTable",
            KeyConditionExpression = "ControlNumber = :cn",
            ExpressionAttributeValues =
                new Dictionary<string, AttributeValue> {{":cn", new AttributeValue { S = "123456789" } }}
        };

        var response = await client.QueryAsync(request);

我已返回所有属性的确切记录。我的DynamoDB表中的属性都定义为字符串。我正在获取这样的数据:

{
  "scannedCount": 5,
  "count": 2,
  "lastEvaluatedKey": null,
  "consumedCapacity": null,
  "items": [
    {
      "Status": {
        "nullvalue": null,
        "ss": null,
        "b": null,
        "bool": null,
        "ns": null,
        "l": null,
        "m": null,
        "n": null,
        "bs": null,
        "s": "READY"
      },
      "Type": {
        "nullvalue": null,
        "ss": null,
        "b": null,
        "bool": null,
        "ns": null,
        "l": null,
        "m": null,
        "n": null,
        "bs": null,
        "s": "OrderStatus"
      },  ......

除了字符串“ s”外,似乎我为每种可能返回的数据类型都获得了一个值。我认为这些被称为数据类型描述符,它们都为null,但字符串数据类型除外,该数据类型包含我实际想要查看的数据。为什么是这样?以及如何只获取数据,而没有我不关心的所有空值。

例如,我希望看到:

{ "Status": "Ready", "Type":"OrderStatus"....}

更新:我可以通过AWS CLI运行类似的查询,甚至运行一个get-item,然后按我期望的那样获取json。因此,似乎.NET SDK正在添加我不希望看到的其他数据类型描述符。另外,我正在使用Mulesoft 4,它与.NET SDK的功能相同。

1 个答案:

答案 0 :(得分:1)

这是使用文档模型时的工作方式。

如果查看他们的示例here,您会看到他们正在通过调用“ PrintDocument”打印所有值。看着这段代码,它正在遍历并打印每个属性和值。

    private static void PrintDocument(Document document)
    {
        //   count++;
        Console.WriteLine();
        foreach (var attribute in document.GetAttributeNames())
        {
            string stringValue = null;
            var value = document[attribute];
            if (value is Primitive)
                stringValue = value.AsPrimitive().Value.ToString();
            else if (value is PrimitiveList)
                stringValue = string.Join(",", (from primitive
                                in value.AsPrimitiveList().Entries
                                                select primitive.Value).ToArray());
            Console.WriteLine("{0} - {1}", attribute, stringValue);
        }
    }

如果您希望继续走这条路,则需要构建自己的映射来处理此问题。因此,假设我们有一个由状态和类型(为简单起见为字符串)组成的InventoryItem对象,则最终将具有如下映射功能:

private InventoryItem Map(Dictionary<string, AttributeValue> result)
{
    return new InventoryItem
    {
        Id = Convert.ToInt32(result["Id"].N),
        Status = result["Status"].S,
        Type = result["Type"].S
    };
}

听起来像您想要的是使用Object Persistence Model。这样,您就可以创建映射到DynamoDB表的类,并且可以轻松执行导致对象的查询。

因此,以他们的示例为例,您将创建一个这样的模型:

[DynamoDBTable("Inventory")]
  public class InventoryItem
  {
    [DynamoDBHashKey]   
    public int Id { get; set; }

    public string Status{ get; set; }
    public string Type{ get; set; }   
  }

要得到那个物品?好吧,您只需要这样做:

 InventoryItem inventoryItem = context.Load<InventoryItem>("123456789");

我更喜欢这种方法。您无法对其进行太多操作,但它更像是我惯用的Django ORM。

希望这会有所帮助!