作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景
我从客户端收到 CSV 数据文件,其中包含大量我不需要的数据和少量我需要的数据。将来我可能需要访问这些数据,虽然我正在存档原始数据文件,但我希望有一些更容易查询的东西。我希望有一个解决方案,并不意味着数据文件保持相同的格式 - 即客户端可以添加/删除列,并且我不希望我的实现放弃丢失的数据或无法归档额外的数据。
当我在 Azure 中构建应用程序时,Azure 表存储对我来说看起来是正确的 - 我可以读取数据文件,然后将我读取的任何键/值对存储到数据存储中。
结果
我想知道如何存储Dictionary<K, V>
或Hashtable
或 Azure 中的一些其他键/值对。
最佳答案
通过重写从 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 个自定义键/值对,因此也必须在某个地方进行处理。
关于.net - 如何在 Azure 表存储中存储任意键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595486/
我是一名优秀的程序员,十分优秀!