【问题标题】:AWS Step function And Athena : can I configure query string from inputpath?AWS Step 函数和 Athena:我可以从输入路径配置查询字符串吗?
【发布时间】:2022-01-13 04:06:34
【问题描述】:

在我的步进函数中,我想执行一个 Athena 查询。我能够定义一个步骤并成功执行查询。但是,我想将一些参数作为输入传递并在查询字符串中使用它们。例如。

假设我的查询字符串是:

select * from <Data Source>.<database>.<tablename> where partition_0 = '2021';

我希望能够将年份作为输入 json 传递给 step 函数,例如:

{
"YYYY": 2021
}

是否可以在查询字符串中插入输入“YYYY”?如果有,怎么做?

示例步骤功能配置:

{
  "Comment": "Start athena exececution",
  "StartAt": "athena",
  "States": {
    "athena": {
      "Type": "Task",
      "InputPath": "$",
      "Resource": "arn:aws:states:::athena:startQueryExecution.sync",
      "Parameters": {
        "QueryString": "select * from mycatalog.mydatabase.mytable where partition_0 = '2021'",
        "WorkGroup": "primary",
        "ResultConfiguration": {
          "OutputLocation": "s3://mys3bucket"
        }
      },
      "Next": "Pass"
    },
    "Pass": {
      "Comment": "A Pass state passes its input to its output, without performing work. Pass states are useful when constructing and debugging state machines.",
      "Type": "Pass",
      "End": true
    }
  }
}

【问题讨论】:

    标签: amazon-web-services aws-step-functions athena


    【解决方案1】:

    使用 Pass State 将输入 YYYY 插入查询字符串:

    "QueryPassTask": {
      "Type": "Pass",
      "ResultPath": "$.athena",
      "Parameters": {
        "query.$": "States.Format('select * from mycatalog.mydatabase.mytable where partition_0 = \\'{}\\'', $.YYYY)"
      },
    

    通过任务输出为:

    {
      "YYYY": 2021,
      "athena": {
        "query": "select * from mycatalog.mydatabase.mytable where partition_0 = '2021'"
      }
    }
    

    接下来,将查询字符串提供给 Athena 任务。不要忘记键上的“.$”后缀。这告诉 Step Functions 键的值包含替换。

     "Parameters": {
            "QueryString.$": "$.athena.query",
    

    【讨论】:

      【解决方案2】:

      除了fedenov 的回答:我认为您不一定需要中间Pass 状态。 Pass 状态也不允许您使用 ResultSelector(这可能很烦人)。您可以直接在 Athena Query 任务中进行字符串格式化。

      下面是一个例子。

      {
          "athena": {
              "catalog": "my_catalog",
              "database": "my_database",
              "outputLocation": "S3://my_bucket...",
              "queryString": "SELECT ... {}",
              "year": "2022"
          }
      }
      
          "queryData": {
            "Type": "Task",
            "Resource": "arn:aws:states:::athena:startQueryExecution.sync",
            "Parameters": {
              "QueryExecutionContext": {
                "Catalog.$": "$.athena.catalog",
                "Database.$": "$.athena.database"
              },
              "ResultConfiguration": {
                "OutputLocation.$": "$.athena.outputLocation"
              },
              "QueryString.$": "States.Format($.athena.queryString, $.athena.year)",
              "WorkGroup": "primary"
            },
            "ResultPath": "$.query",
            "Next": "nextStep"
          },
      

      【讨论】:

        猜你喜欢
        • 2023-02-23
        • 1970-01-01
        • 1970-01-01
        • 2011-09-04
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多