处理具有多种类型的Cosmos DB容器的读取? [英] Handling reads of Cosmos DB container with multiple types?

查看:56
本文介绍了处理具有多种类型的Cosmos DB容器的读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将几种不同的对象类型存储在一个Cosmos DB容器中,因为它们都在逻辑上进行了分组,因此可以通过时间戳进行读取,以避免额外的HTTP调用.

但是,Cosmos DB客户端API似乎没有提供一种简便的方法来进行多种类型的读取.到目前为止,我找到的最好的解决方案是编写自己的CosmosSerializer和JsonConverter,但这感觉很笨拙:解决方案

我这样做的方法是将ItemQueryIterator和FeedResponse对象创建为动态对象,并首先将它们读为非类型,这样我就可以检查"type"对象了.告诉我要反序列化为哪种类型的对象.

在此示例中,我有一个容器,其中包含我的客户数据及其所有销售订单.代码看起来像这样.

 字符串sql ="SELECT * from c WHERE c.customerId = @customerId";FeedIterator< dynamic>resultSet = container.GetItemQueryIterator< dynamic>(新的QueryDefinition(sql).WithParameter("@ customerId",customerId),requestOptions:新的QueryRequestOptions{PartitionKey =新的PartitionKey(customerId)});CustomerV4客户=新的CustomerV4();List< SalesOrder>orders = new List< SalesOrder>();while(resultSet.HasMoreResults){//动态响应.根据类型"反序列化为POCO.财产FeedResponse<动态>响应=等待resultSet.ReadNextAsync();foreach(响应中的可变项){如果(item.type ==客户"){客户= JsonConvert.DeserializeObject< CustomerV4>(item.ToString());}否则,如果(item.type =="salesOrder"){orders.Add(JsonConvert.DeserializeObject< SalesOrder>(item.ToString()));}}} 

I'd like to store several different object types in a single Cosmos DB container, as they are all logically grouped and make sense to read together by timestamp to avoid extra HTTP calls.

However, the Cosmos DB client API doesn't seem to provide an easy way of doing the reads with multiple types. The best solution I've found so far is to write your own CosmosSerializer and JsonConverter, but that feels clunky: https://thomaslevesque.com/2019/10/15/handling-type-hierarchies-in-cosmos-db-part-2/

Is there a more graceful way to read items of different types to a shared base class so I can cast them later, or do I have to take the hit?

Thanks!

解决方案

The way I do this is to create the ItemQueryIterator and FeedResponse objects as dynamic and initially read them untyped so I can inspect a "type" property that tells me what type of object to deserialize into.

In this example I have a single container that contains both my customer data as well as all their sales orders. The code looks like this.

        string sql = "SELECT * from c WHERE c.customerId = @customerId";

        FeedIterator<dynamic> resultSet = container.GetItemQueryIterator<dynamic>(
            new QueryDefinition(sql)
            .WithParameter("@customerId", customerId),
            requestOptions: new QueryRequestOptions 
            { 
                PartitionKey = new PartitionKey(customerId) 
            });

        CustomerV4 customer = new CustomerV4();
        List<SalesOrder> orders = new List<SalesOrder>();

        while (resultSet.HasMoreResults)
        {
            //dynamic response. Deserialize into POCO's based upon "type" property
            FeedResponse<dynamic> response = await resultSet.ReadNextAsync();
            foreach (var item in response)
            {
                if (item.type == "customer")
                {
                    customer = JsonConvert.DeserializeObject<CustomerV4>(item.ToString());
                    
                }
                else if (item.type == "salesOrder")
                {
                    orders.Add(JsonConvert.DeserializeObject<SalesOrder>(item.ToString()));
                }
            }
        }

这篇关于处理具有多种类型的Cosmos DB容器的读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆