Slide 13
Slide 13 text
Sample code - Push model
public class Function1
{
public Function1(CosmosClient cosmosClient)
{
_container = cosmosClient.GetContainer("SampleDB", "MaterializedView");
}
private readonly Container _container;
[FunctionName("Function1")]
public async Task Run([CosmosDBTrigger(
databaseName: "SampleDB",
collectionName: "TodoItems",
LeaseCollectionName = "leases")]
IReadOnlyList input, ILogger log)
{
var tasks = new Task[input.Count];
for (int i = 0; i < input.Count; i++)
{
// Change the partition key and write it back (actually, do advanced conversion)
var partitionKey = new PartitionKey(input[i].GetPropertyValue("anotherKey"));
tasks[i] = _container.UpsertItemStreamAsync(new MemoryStream(input[i].ToByteArray()), partitionKey);
}
await Task.WhenAll(tasks);
}
}
14