问题描述
我想在 Azure 函数中读取 Azure 表。表的 PartitionKey 是一个日期。如果日期是一年,例如。 “2020”,我知道我可以使用表格输入绑定读取PartitionKey等于当前年份的表格部分如下:
{
"type": "table","direction": "in","name": "inputTable","tableName": "inTable","partitionKey": "{datetime: yyyy}","connection": "AzureWebJobsstorage"
}
我怎样才能实现类似的东西: PartitionKey 是一个由年和周数组成的日期,例如(“2020-20”、“2020-48”等),我想用PartitionKey 等于今天的年-周?
如果有帮助,python 中的日期格式是 %Y-%U。
我检查了 Azure page on custom date and time format,但找不到答案。
另外,尝试 {datetime: yyyy-w},{datetime: yyyy-u},{datetime: yyyy}-{datetime: u} 也没有用。
谢谢。
解决方法
没有获取当前周数的本机函数,仅提供 current time,但您可以将此值作为参数传递到表绑定中。我做了一些测试,这是我的function.json
:
{
"scriptFile": "__init__.py","bindings": [
{
"name": "messageJSON","type": "table","tableName": "testt1","partitionKey": "{week}","rowKey": "{id}","connection": "AzureWebJobsStorage","direction": "in"
},{
"authLevel": "anonymous","type": "httpTrigger","direction": "in","name": "req","methods": [
"get","post"
],"route": "{week}/{id}"
},{
"type": "http","direction": "out","name": "$return"
}
]
}
我的演示代码:
import json
import azure.functions as func
def main(req: func.HttpRequest,messageJSON) -> func.HttpResponse:
return func.HttpResponse(f"Table row: {messageJSON}")
测试数据:
测试结果:
如果您使用的是 python,请在请求函数时just refer to this to get a specific week number。
如果您还有任何问题,请随时告诉我。