我有一个Azure功能,我想让它从EventHub接收消息(这是相当简单和有效的),然后在运行时使用表绑定将该信息放入表存储。
这是我到目前为止所拥有的:
public static async Task Run(string eventHubMessage, TraceWriter log, Binder binder)
{
var m = JsonConvert.DeserializeObject<Measurement>(eventHubMessage);
var attributes = new Attribute[]
{
new StorageAccountAttribute("AzureWebJobsTest"),
new TableAttribute(tableName, m.PartitionKey, m.RowKey)
};
using(var output = await binder.BindAsync<MyTableEntity>(attributes))
{
if(output == null)
log.Info($"4. output is null");
else
{
output.Minimum = m.Minimum;
output.Maximum = m.Maximum;
output.Average = m.Average;
output.Timestamp = m.Timestamp;
output.ETag = m.ETag;
output.WriteEntity(/* Need an operationContext*/)
}
}
}
public class MyTableEntity : TableEntity, IDisposable
{
public double Average { get; set;}
public double Minimum { get; set;}
public double Maximum { get; set;}
bool disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
}
disposed = true;
}
}
我的问题;
1)输出始终 null。
2)即使输出不为null,我也不知道我对OperationContext的需求,或者调用ITableEntity.Write()甚至是让它写入表存储的正确方法。
ETA Json Binding:
{
"bindings": [
{
"type": "eventHubTrigger",
"name": "eventHubMessage",
"direction": "in",
"path": "measurements",
"connection": "MeasurementsConnectionString"
}
],
"disabled": false
}
答案 0 :(得分:4)
要向Table添加新条目,您应该绑定到IAsyncCollector
而不是实体本身,然后创建一个新实体并调用AddAsync
。以下代码段对我有用:
var attributes = new Attribute[]
{
new StorageAccountAttribute("..."),
new TableAttribute("...")
};
var output = await binder.BindAsync<IAsyncCollector<MyTableEntity>>(attributes);
await output.AddAsync(new MyTableEntity()
{
PartitionKey = "...",
RowKey = "...",
Minimum = ...,
...
});
答案 1 :(得分:0)
如果要使用DynamicTableEntity是因为在编译时您不知道消息中将包含什么数据,并且您意识到表绑定不再适用于DynamicTableEntities,则可以使用以下方法:
private static async Task ProcessMessage(string message, DateTime enqueuedTime)
{
var deviceData = JsonConvert.DeserializeObject<JObject>(message);
var dynamicTableEntity = new DynamicTableEntity();
dynamicTableEntity.RowKey = enqueuedTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
foreach (KeyValuePair<string, JToken> keyValuePair in deviceData)
{
if (keyValuePair.Key.Equals("MyPartitionKey"))
{
dynamicTableEntity.PartitionKey = keyValuePair.Value.ToString();
}
else if (keyValuePair.Key.Equals("Timestamp")) // if you are using a parameter "Timestamp" it has to be stored in a column named differently because the column "Timestamp" will automatically be filled when adding a line to table storage
{
dynamicTableEntity.Properties.Add("MyTimestamp", EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
}
else
{
dynamicTableEntity.Properties.Add(keyValuePair.Key, EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
}
}
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myStorageConnectionString");
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("myTableName");
table.CreateIfNotExists();
var tableOperation = TableOperation.Insert(dynamicTableEntity);
await table.ExecuteAsync(tableOperation);
}