如何在Azure表存储中存储任意键值对? [英] How can I store arbitrary key value pairs in Azure table storage?
问题描述
背景
我正在从客户端接收CSV数据文件,这些文件包含大量不需要的数据和少量需要的数据.将来,我可能需要访问这些数据,尽管我正在归档原始数据文件,但我希望查询一些更容易的东西.我希望的解决方案不等于数据文件保持相同的格式-即客户端可以添加/删除列,并且我不希望我的实现对丢失的数据保释或无法归档其他数据.>
在Azure中构建应用程序时,Azure表存储对我来说看起来是正确的-我可以读取数据文件,然后将读取的任何键/值对存储到数据存储中.
结果
我想知道如何在Azure中存储Dictionary<K, V>
或Hashtable
或其他一些键/值对.
通过在从TableEntity派生的类上重写ReadEntity和WriteEntity方法,可以存储其他属性.
这是我幼稚的实现方式
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;
}
}
}
}
我注意到,Azure表存储总共只能存储255个键/值对-如果考虑到PartitionKey等,则只能存储252个自定义对,因此也必须在某个地方进行处理.
Background
I am receiving CSV data files from clients that contain a large amount of data that I don't need and a small amount of data that I do. In the future I may need to access that data and although I'm archiving the original data files I was hoping for something a bit easier to query. I was hoping for a solution that didn't mean the data files stayed in the same format - i.e. the client may add/remove columns and I don't want my implementation to bail on missing data or fail to archive additional data.
As I'm building the application in Azure, Azure table storage looked correct to me - I could read the data files and then store whatever key/value pairs I read into the data store.
Upshot
I would like to know how to store a Dictionary<K, V>
or Hashtable
or some other key/value pairs in Azure.
By overriding the ReadEntity and WriteEntity methods on classes derived from TableEntity, additional properties can be stored.
Here's 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've noted that Azure table storage can only store a total of 255 key/value pairs – or 252 custom ones once PartitionKey, etc. is taken into account, so this has to be handled somewhere as well.
这篇关于如何在Azure表存储中存储任意键值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!