1

I want to read an Azure table in an Azure function. The PartitionKey of the table is a date. If the date is a year, eg. "2020", I know I can use the table input binding to read the part of the table with the PartitionKey equal to the current year as follows:

{
  "type": "table",
  "direction": "in",
  "name": "inputTable",
  "tableName": "inTable",
  "partitionKey": "{datetime: yyyy}",
  "connection": "AzureWebJobsStorage"
 }

How can I achieve something similar when: the PartitionKey is a date composed of a year and a week number, eg ("2020-20", "2020-48", etc.) and I want to read the data with the PartitionKey equal to today's year-week?

If it helps, the date format in python is %Y-%U.

I've checked the Azure page on custom date and time format but I could't find an answer.

Also, trying {datetime: yyyy-w},{datetime: yyyy-u}, {datetime: yyyy}-{datetime: u} did not work.

Thanks.

Tuk31n
  • 33
  • 3

1 Answers1

0

There is no native function to get the current week number, current time is provided only, but you can pass this value as a param into table banding. I did some test on my side and this is my 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"
    }
  ]
}

My demo code :

import json

import azure.functions as func

def main(req: func.HttpRequest, messageJSON) -> func.HttpResponse:
   
    return func.HttpResponse(f"Table row: {messageJSON}")

Test data:

enter image description here

Test Result:

enter image description here

If you are using python, just refer to this to get a specific week number while you request the function.

Free to let me know if you have any more questions.

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • Many thanks for the answer, Stanley. Unfortunately, the function is time triggered and part of a data cleaning pipeline. So I cannot use the httpTrigger to pass the value. If there is another way to pass the value, maybe it can do the trick. – Tuk31n May 04 '21 at 12:55
  • @Tuk31n, I see , so you are using Azure function time trigger to trigger it? If so, how about querying data by storage table sdk in function code directly instead of using table binding? It's much more flexible. Let me know if you have any more questions :) – Stanley Gong May 05 '21 at 01:55
  • @Tuk31n, this is a sample about using table sdk to query table directly, hope it could be helpful: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/tables/azure-data-tables/samples/sample_query_tables.py – Stanley Gong May 06 '21 at 01:48
  • @Tuk31n Let me know if you need any help – Stanley Gong May 06 '21 at 10:32
  • Thanks a lot for the lead Stanley. It really seems there's no way to do it via table binding. But I've been toying around with the sdk in the last few days and yes, that's helpful since I can now select the rows I wanted using .query_tables() Thank you! – Tuk31n May 09 '21 at 20:17
  • @Tuk31n, Glad to know that my solution is helpful. Please click on the checkmark beside the answer to toggle it from greyed out to fill in to accept it as an answer, it will help others and close this question : ) – Stanley Gong May 10 '21 at 01:17