azure 如何在Synapse中动态设置数组类型变量

yjghlzjz  于 2023-08-07  发布在  其他
关注(0)|答案(3)|浏览(93)

我想创建一个数组类型的变量在管道中使用,但不知道如何做到这一点。我想把这个月到12月存储在一个数组类型的变量中。例如,我想在本月要执行的管道中创建以下变量;

["07","08","09","10","11","12"]

字符串
还有,下个月是这样的;

["08","09","10","11","12"]


有没有办法使用Synapse的管道函数来创建这个变量?任何答案都会有所帮助。- 谢谢你-谢谢

r8xiu3jd

r8xiu3jd1#

您可以在其中使用for-each activityappend variable activity来创建一个数组变量,其中包含Synapse管道中从当前月份到12月的值。

  • 在synapse流水线中定义了三个变量

x1c 0d1x的数据
1.起始值:此变量存储当前月份。
1.TotalIterationValue:此变量存储一年中剩余的月数。
1.Array_variable:此变量是一个数组,用于存储从当前月份到12月的月份。

  • 变量StartValue和TotalIterationValue是使用集合变量活动定义的。

1.设置变量-起始值:


此活动将StartValue变量的值设置为当前月份。它使用子字符串函数从UTC时间中提取月份,并将其设置为变量的值。表达式:@substring(utcnow(),5,2)的值。
1.设置变量- TotalIterationValue:


此活动将TotalIterationValue变量的值设置为一年中剩余的月数。它使用sub函数从13(一年中的总月数)中减去当前月份,并将其设置为变量的值。表达式:@string(sub(13,int(variables('StartValue'))))

  • 然后执行For-each活动,并执行insideAppend variable activity



项目中的表达式:@range(0, int(variables('TotalIterationValue')))
x1c4d 1x的
Array_variable的表达式:@add(int(variables('StartValue')),item())
此活动循环遍历从0到TotalIterationValue值的数字范围,并将相应的月份追加到Array_variable变量。它使用add函数将当前迭代次数添加到StartValue变量中,并将其设置为Array_variable变量的值。

{
    "name": "PipelineX",
    "properties": {
        "activities": [
            {
                "name": "Set variable- Start Value",
                "type": "SetVariable",
                "dependsOn": [],
                "policy": {
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "variableName": "StartValue",
                    "value": {
                        "value": "@substring(utcnow(),5,2)",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "Set variable - TotalIterationValue",
                "type": "SetVariable",
                "dependsOn": [
                    {
                        "activity": "Set variable- Start Value",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "policy": {
                    "secureOutput": false,
                    "secureInput": false
                },
                "userProperties": [],
                "typeProperties": {
                    "variableName": "TotalIterationValue",
                    "value": {
                        "value": "@string(sub(13,int(variables('StartValue'))))",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "ForEach1",
                "type": "ForEach",
                "dependsOn": [
                    {
                        "activity": "Set variable - TotalIterationValue",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "items": {
                        "value": "@range(0, int(variables('TotalIterationValue')))",
                        "type": "Expression"
                    },
                    "isSequential": true,
                    "activities": [
                        {
                            "name": "Append variable1",
                            "type": "AppendVariable",
                            "dependsOn": [],
                            "userProperties": [],
                            "typeProperties": {
                                "variableName": "Array_variable",
                                "value": {
                                    "value": "@add(int(variables('StartValue')),item())",
                                    "type": "Expression"
                                }
                            }
                        }
                    ]
                }
            }
        ],
        "variables": {
            "StartValue": {
                "type": "String"
            },
            "TotalIterationValue": {
                "type": "String"
            },
            "Array_variable": {
                "type": "Array"
            }
        },
        "annotations": []
    }
}

字符串
Array_variable将具有从当前月份到12月的值。

csbfibhn

csbfibhn2#

除了Aswin的方法之外,作为替代方案,您也可以尝试以下方法:

{
"name": "pipeline38",
"properties": {
    "activities": [
        {
            "name": "FindCurrentMonth",
            "type": "SetVariable",
            "dependsOn": [],
            "policy": {
                "secureOutput": false,
                "secureInput": false
            },
            "userProperties": [],
            "typeProperties": {
                "variableName": "FindCurrentMonth",
                "value": {
                    "value": "@substring(formatDateTime(utcnow(),'yyyy-MM-dd'),5,2)",
                    "type": "Expression"
                }
            }
        },
        {
            "name": "Until1",
            "type": "Until",
            "dependsOn": [
                {
                    "activity": "FindCurrentMonth",
                    "dependencyConditions": [
                        "Succeeded"
                    ]
                }
            ],
            "userProperties": [],
            "typeProperties": {
                "expression": {
                    "value": "@equals(variables('TempVar'),'12')",
                    "type": "Expression"
                },
                "activities": [
                    {
                        "name": "Append variable1",
                        "type": "AppendVariable",
                        "dependsOn": [
                            {
                                "activity": "TempVar",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "appendvariable",
                            "value": {
                                "value": "@variables('TempVar')",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "TempVar",
                        "type": "SetVariable",
                        "dependsOn": [],
                        "policy": {
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "TempVar",
                            "value": {
                                "value": "@variables('FindCurrentMonth')",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "Update month value",
                        "type": "SetVariable",
                        "dependsOn": [
                            {
                                "activity": "Append variable1",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "FindCurrentMonth",
                            "value": {
                                "value": "@{add(int(variables('TempVar')),1)}",
                                "type": "Expression"
                            }
                        }
                    }
                ],
                "timeout": "0.12:00:00"
            }
        },
        {
            "name": "Final Output",
            "type": "SetVariable",
            "dependsOn": [
                {
                    "activity": "Until1",
                    "dependencyConditions": [
                        "Succeeded"
                    ]
                }
            ],
            "policy": {
                "secureOutput": false,
                "secureInput": false
            },
            "userProperties": [],
            "typeProperties": {
                "variableName": "finaloutput",
                "value": {
                    "value": "@variables('appendvariable')",
                    "type": "Expression"
                }
            }
        }
    ],
    "variables": {
        "FindCurrentMonth": {
            "type": "String"
        },
        "month": {
            "type": "String"
        },
        "appendvariable": {
            "type": "Array"
        },
        "TempVar": {
            "type": "String"
        },
        "finaloutput": {
            "type": "Array"
        }
    },
    "annotations": []
}
}

字符串
创建以下字符串变量:FindCurrentMonthmonthTempVar创建以下Array变量:appendvariablefinaloutput

  • 使用set variable activity通过使用以下表达式对当前月份进行微调:@substring(formatDateTime(utcnow(),'yyyy-MM-dd'),5,2)
  • 使用until语句块和表达式:@equals(variables('TempVar'),'12')
  • 在until中,你可以用表达式设置一个临时变量:@variables('FindCurrentMonth')
  • 使用带有exp的追加变量:@variables('TempVar')
  • 用exp更新月份值:@{add(int(variables('TempVar')),1)}
  • 在until之外,使用另一个set变量来获得最终输出:@variables('appendvariable')


的数据

up9lanfz

up9lanfz3#

我们可以在until循环中使用set变量和append变量。


的数据
不需要设置变量4。我创建它只是为了打印值,因为在调试模式下无法看到附加值,您可以忽略month2变量并设置变量4活动。


{
"name": "Pipeline 1",
"properties": {
    "activities": [
        {
            "name": "Set variable1",
            "type": "SetVariable",
            "dependsOn": [],
            "policy": {
                "secureOutput": false,
                "secureInput": false
            },
            "userProperties": [],
            "typeProperties": {
                "variableName": "start",
                "value": {
                    "value": "@substring(formatDateTime(utcNow(),'yyyy-MM-dd'),5,2)",
                    "type": "Expression"
                }
            }
        },
        {
            "name": "Until1",
            "type": "Until",
            "dependsOn": [
                {
                    "activity": "Set variable1",
                    "dependencyConditions": [
                        "Succeeded"
                    ]
                }
            ],
            "userProperties": [],
            "typeProperties": {
                "expression": {
                    "value": "@equals(variables('start'),'13')",
                    "type": "Expression"
                },
                "activities": [
                    {
                        "name": "Append variable1",
                        "type": "AppendVariable",
                        "dependsOn": [],
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "months",
                            "value": {
                                "value": "@variables('start')",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "Set variable2",
                        "type": "SetVariable",
                        "dependsOn": [
                            {
                                "activity": "Append variable1",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "start2",
                            "value": {
                                "value": "@string(add(int(variables('start')),1))",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "Set variable3",
                        "type": "SetVariable",
                        "dependsOn": [
                            {
                                "activity": "Set variable2",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "start",
                            "value": {
                                "value": "@variables('start2')",
                                "type": "Expression"
                            }
                        }
                    },
                    {
                        "name": "Set variable4",
                        "type": "SetVariable",
                        "dependsOn": [
                            {
                                "activity": "Append variable1",
                                "dependencyConditions": [
                                    "Succeeded"
                                ]
                            }
                        ],
                        "policy": {
                            "secureOutput": false,
                            "secureInput": false
                        },
                        "userProperties": [],
                        "typeProperties": {
                            "variableName": "months2",
                            "value": {
                                "value": "@variables('months')",
                                "type": "Expression"
                            }
                        }
                    }
                ],
                "timeout": "0.12:00:00"
            }
        }
    ],
    "variables": {
        "start": {
            "type": "String"
        },
        "months": {
            "type": "Array"
        },
        "start2": {
            "type": "String"
        },
        "months2": {
            "type": "Array"
        }
    },
    "annotations": []
}

字符串
}

相关问题