前言
函数计算(Function Compute)是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。Table Store Stream是用于获取Table Store表中增量数据的一个数据通道,通过创建Table Store触发器,能够实现Table Store Stream和函数计算的自动对接,从而实现表格存储中的表数据发生变更时候定制化的自动处理。具体可以查看表格存储触发函数计算官方教程,但是该官方教程只有python版的代码示例,本教程展示C#版的代码示例。
具体过程
表格存储传给函数event参数是cbor格式,格式如下:
{
"Version": "string",
"Records": [
{
"Type": "string",
"Info": {
"Timestamp": int64
},
"PrimaryKey": [
{
"ColumnName": "string",
"Value": formated_value
}
],
"Columns": [
{
"Type": "string",
"ColumnName": "string",
"Value": formated_value,
"Timestamp": int64
}
]
}
]
}
- 参考函数计算C# runtime教程, 创建一个dotnetcore2.1的函数, 本教程使用的是Dahomey.Cbor库, 其中有关cbor处理;
下面展示是相关配置和代码:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dahomey.Cbor" Version="1.5.0" />
<PackageReference Include="Dahomey.Cbor.AspNetCore" Version="1.4.9" />
<PackageReference Include="Aliyun.Serverless.Core" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>
using System;
using System.IO;
using Dahomey.Cbor;
using Dahomey.Cbor.ObjectModel;
using Aliyun.Serverless.Core;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Dotnet
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class fcFileHandler
{
public async Task<Stream> Echo(Stream input, IFcContext context)
{
ILogger logger = context.Logger;
try{
CborObject cborObject = await Cbor.DeserializeAsync<CborObject>(input);
logger.LogInformation("Handle input: {0}", cborObject.ToString());
var result = JsonConvert.DeserializeObject<CBORRootInfo>(cborObject.ToString());
logger.LogInformation("Version: {0}", result.Version);
logger.LogInformation("Records: {0}", JsonConvert.SerializeObject(result.Records));
}catch (Exception e){
Console.WriteLine("{0} Second exception.", e.Message);
}
MemoryStream copy = new MemoryStream();
await input.CopyToAsync(copy);
copy.Seek(0, SeekOrigin.Begin);
return copy;
}
}
}
using System.Collections.Generic;
namespace Dotnet
{
public class ColumnsItem
{
/// <summary>
///
/// </summary>
public string ColumnName { get; set; }
/// <summary>
///
/// </summary>
public string Timestamp { get; set; }
/// <summary>
///
/// </summary>
public string Type { get; set; }
/// <summary>
///
/// </summary>
public string Value { get; set; }
}
public class Info
{
/// <summary>
///
/// </summary>
public string Timestamp { get; set; }
}
public class PrimaryKeyItem
{
/// <summary>
///
/// </summary>
public string ColumnName { get; set; }
/// <summary>
///
/// </summary>
public string Value { get; set; }
}
public class RecordsItem
{
/// <summary>
///
/// </summary>
public List<ColumnsItem> Columns { get; set; }
/// <summary>
///
/// </summary>
public Info Info { get; set; }
/// <summary>
///
/// </summary>
public List<PrimaryKeyItem> PrimaryKey { get; set; }
/// <summary>
///
/// </summary>
public string Type { get; set; }
}
public class CBORRootInfo
{
/// <summary>
///
/// </summary>
public string Version { get; set; }
/// <summary>
///
/// </summary>
public List<RecordsItem> Records { get; set; }
}
}
2.参考表格存储触发函数计算官方教程 创建对应的ots实例和表,并且配置该表的触发器是一个1中创建的函数,最后效果如下图所示:
3.使用表格存储客户端或者表格存储操作表代码往对应的表中进行数据操作;
本人最后显示效果如下:
注意:需要配置函数的service日志能写入logstore,具体参考函数计算访问日志服务