简体   繁体   中英

What is the right way to use TableEntity.Flatten in Microsoft.Azure.Cosmos.Table?

I am trying to store json object from my API project to CosmosDB using Microsoft.Azure.Cosmos.Table. My Json looks like this:

{
  "age": 0,
  "type": "network-error",
  "url": "https://www.example.com/",
  "body": {
    "sampling_fraction": 0.5,
    "referrer": "http://example.com/",
    "server_ip": "2001:DB8:0:0:0:0:0:42",
    "protocol": "h2",
    "method": "GET",
    "request_headers": {},
    "response_headers": {},
    "status_code": 200,
    "elapsed_time": 823,
    "phase": "application",
    "type": "http.protocol.error"
  }
}

And my Cosmos is of type Table API hence I need to flatten it but couldn't find out a way to do it in a most generic way, MSDN documentation doesn't have much about implementation.

Thanks much, appreciate your time and help.

Create a .Net class for your model and pass that to TableEntity.Flatten . You can then use the returned Dictionary of flattened key and EntityProperty value pairs to create a DynamicTableEntity class by specifying the partition and row keys. And you can insert that to cosmos db table storage.

Usage should be similar to how it was used in Table Storage api. Have a look at the unit tests here: https://github.com/Azure/azure-storage-net/commit/daff940a506b27650901b9b9feb59131ffacce6d

ShapeEntity shapeEntity = new ShapeEntity(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "square", 4, 4);
            OperationContext operationContext = new OperationContext();

            DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(shapeEntity.PartitionKey, shapeEntity.RowKey)
            {
                Properties = TableEntity.Flatten(shapeEntity, operationContext)
            };

and you can write this DynamicTableEntity into Cosmos DB using the SDK.

To read the flattened DynamicTableEntity back into the complex object, when you read it, you need to use TableEntity.ConvertBack api.

Based on the document you posted above, it will be more appropriate to use the SQL API/Mongo API to store the data in cosmosdb as the document looks sort of JSON.

As you mentioned in the comment if other tables are in relational way , i would recommend you to go through data modelling and design your tables

if you still want to store it in table storage, convert your json to a model and serialize it before inserting using Flatten

public class RequestHeaders
{
}

public class ResponseHeaders
{
}

public class Body
{
    public double sampling_fraction { get; set; }
    public string referrer { get; set; }
    public string server_ip { get; set; }
    public string protocol { get; set; }
    public string method { get; set; }
    public RequestHeaders request_headers { get; set; }
    public ResponseHeaders response_headers { get; set; }
    public int status_code { get; set; }
    public int elapsed_time { get; set; }
    public string phase { get; set; }
    public string type { get; set; }
}

public class RootObject
{
    public int age { get; set; }
    public string type { get; set; }
    public string url { get; set; }
    public Body body { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM