How can I store arbitrary key pairs in Azure table storage?

Background

I get CSV data files from clients that contain a large amount of data that I do not need, and a small amount of data that I do. In the future, I may need access to this data, and although I am archiving the original data files, I was hoping for something easier to query. I was hoping for a solution that did not mean that the data files remained in the same format, that is, the client can add / remove columns, and I do not want my implementation to run with downhole data or not archive additional data.

As I build the application in Azure, the Azure table store looked right for me - I could read the data files and then store any key / value pairs that I read in the data store.

Upshot

I would like to know how to keep in Azure Dictionary<K, V>or Hashtableor some other key / value pairs.

+5
source share
1 answer

By overriding the ReadEntity and WriteEntity methods for classes derived from TableEntity, additional properties can be saved.

Here is my naive implementation

public class TestEntity : TableEntity {

    public TestEntity(string a, string b) {
        PartitionKey = a;
        RowKey = b;
        DataItems = new Dictionary<string, string>();
    }

    public Dictionary<string, string> DataItems { get; set; }

    public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext) {
        var results = base.WriteEntity(operationContext);
        foreach (var item in DataItems) {
            results.Add("D_" + item.Key, new EntityProperty(item.Value));
        }
        return results;
    }

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext) {
        base.ReadEntity(properties, operationContext);

        DataItems = new Dictionary<string, string>();

        foreach (var item in properties) {
            if (item.Key.StartsWith("D_")) {
                string realKey = item.Key.Substring(2);
                ItemData[realKey] = item.Value.StringValue;
            }
        }
    }
}

I noticed that Azure table storage can only store 255 key / value pairs - or 252 custom ones when PartitionKey, etc. are taken into account, so this also needs to be handled.

+9
source

All Articles