访问 Airflow 中的 {{ts_nodash}} 值

huangapple go评论113阅读模式
英文:

Accessing value of {{ts_nodash}} in airflow

问题

我需要访问{{ts_nodash}}的值,以将其格式化为日期时间并减去一小时。但这在Dag上给了我一个错误,说:“ValueError: time data '{{ ts_nodash }}' does not match format '%Y%m%dT%H%M%S'”
我的方法如下所示:

  1. ts_nodash = datetime.strptime("{{ ts_nodash }}", "%Y%m%dT%H%M%S")
  2. get_numbers = gke_wrapper.execute_gke_operator(
  3. task_id=f"get_numbers_{ctype_name}",
  4. labels=get_cost_labels(
  5. pod=Pod.pod,
  6. service="service",
  7. id="mmoe",
  8. environment="dev",
  9. component="airflow",
  10. ),
  11. cmd=["sh"],
  12. args=[
  13. "get_numbers.sh",
  14. output_table_name,
  15. dump_table_name,
  16. model_path,
  17. model_path,
  18. ctype_name,
  19. config.variants[ctype_name],
  20. (ts_nodash - timedelta(hours=config.delay_interval)).strftime(
  21. "%Y%m%dT%H%M%S"
  22. ),
  23. ],
  24. nodepool=_NODE_POOL,
  25. image=_IMAGE,
  26. resource=_RESOURCES,
  27. )

有人能提到如何访问模板变量的值吗?
我尝试了许多不同的方法来在args=[]中传递{{ts_nodash}},但显然在模板值可用之前会执行日期时间解析操作。任何帮助都将不胜感激。

英文:

I need to access the value of {{ts_nodash}} to format it as a datetime and subtract an hour from it. But this gives me an error on dag saying: ValueError: time data '{{ ts_nodash }}' does not match format '%Y%m%dT%H%M%S'
This is how my method looks like:

  1. ts_nodash = datetime.strptime("{{ ts_nodash }}", "%Y%m%dT%H%M%S")
  2. get_numbers = gke_wrapper.execute_gke_operator(
  3. task_id=f"get_numbers_{ctype_name}",
  4. labels=get_cost_labels(
  5. pod=Pod.pod,
  6. service="service",
  7. id="mmoe",
  8. environment="dev",
  9. component="airflow",
  10. ),
  11. cmd=["sh"],
  12. args=[
  13. "get_numbers.sh",
  14. output_table_name,
  15. dump_table_name,
  16. model_path,
  17. model_path,
  18. ctype_name,
  19. config.variants[ctype_name],
  20. (ts_nodash - timedelta(hours=config.delay_interval)).strftime(
  21. "%Y%m%dT%H%M%S"
  22. ),
  23. ],
  24. nodepool=_NODE_POOL,
  25. image=_IMAGE,
  26. resource=_RESOURCES,
  27. )

Can someone mention how can I access the template variable value?
I tried many different ways of passing {{ts_nodash}} within args=[] but apparently the datetime parsing operation occurs before the template value is made available. Any help will be much appreciated.

答案1

得分: 1

模板在Dag运行期间评估(即在操作员的execute方法内部)。看起来你正试图在Dag的运行时之外获取ts_nodash

以下是使用模板的示例:

  1. op = BashOperator(
  2. task_id="pre-validation",
  3. bash_command="echo 'this is ts_nodash {{ ts_nodash }}'\n"
  4. "echo 'this is dag_run.logical_date {{ dag_run.logical_date }}'",
  5. dag=dag,
  6. )

因此,你需要像下面这样将模板本身传递给操作员,它将在运行时进行评估:

  1. ...
  2. args = [
  3. "get_numbers.sh",
  4. output_table_name,
  5. dump_table_name,
  6. model_path,
  7. model_path,
  8. ctype_name,
  9. config.variants[ctype_name],
  10. "{{ ts_nodash }}",
  11. ],
  12. ...
英文:

Templates evaluated during the Dag run (i.e. inside the execute method of the operator). Looks like you are trying to fetch ts_nodash outside the run time of the dag.

Below is an example of using templates:

  1. op = BashOperator(
  2. task_id="pre-validation",
  3. bash_command="echo 'this is ts_nodash {{ ts_nodash }}'\n"
  4. "echo 'this is dag_run.logical_date {{ dag_run.logical_date }}'",
  5. dag=dag,
  6. )

So you will have to pass the template itself to the operator like below and it will be evaluated during the run time:

  1. ...
  2. args = [
  3. "get_numbers.sh",
  4. output_table_name,
  5. dump_table_name,
  6. model_path,
  7. model_path,
  8. ctype_name,
  9. config.variants[ctype_name],
  10. "{{ ts_nodash }}"
  11. ],
  12. ...

huangapple
  • 本文由 发表于 2023年7月20日 21:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定