selected_resources 컨텍스트 변수

selected_resources 컨텍스트 변수

selected_resources 컨텍스트 변수에는 현재 dbt 명령어가 선택한 모든 노드의 리스트가 들어 있어요.

현재 이 변수는 run-operation 명령어를 사용할 때는 접근할 수 없어요.

출처: 문서

본문

주의해요 — dbt는 dbt 프로젝트 실행의 파싱 단계에서 그래프를 적극적으로 만들기 때문에, selected_resources 컨텍스트 변수는 파싱 중에는 비어 있어요. 이 페이지를 읽고 이 변수를 효과적으로 사용하는 방법을 알아두세요.

사용법

selected_resources 컨텍스트 변수는 현재 dbt 명령어 선택자가 선택한 모든 리소스의 리스트예요. 그 값은 (앱: dbt v1.12 이상) --select--exclude 같은 파라미터 사용에 따라 달라져요.

특정 실행에서 다음과 같이 보일 거예요:

["model.my_project.model1", "model.my_project.model2", "snapshot.my_project.my_snapshot"]

각 값은 graph 컨텍스트 변수의 nodes 객체 안의 키에 해당해요.

pre-hook, post-hook, on-run-start, 또는 on-run-end 안의 매크로에서 이 변수를 사용해 어떤 노드가 선택됐는지 평가하고, 특정 노드가 선택됐는지에 따라 다른 로직을 실행할 수 있어요.

check-node-selected.sql


/*
  Check if a given model is selected and trigger a different action, depending on the result
*/

{% if execute %}
  {% if 'model.my_project.model1' in selected_resources %}
  
    {% do log("model1 is included based on the current selection", info=true) %}
  
  {% else %}

    {% do log("model1 is not included based on the current selection", info=true) %}

  {% endif %}
{% endif %}

/*
  Example output when running the code in on-run-start 
  when doing `dbt build`, including all nodels
---------------------------------------------------------------
  model1 is included based on the current selection


  Example output when running the code in on-run-start 
  when doing `dbt run --select model2` 
---------------------------------------------------------------
  model1 is not included based on the current selection
*/

더 알아보기 (Learn more)

  • selected_resources는 선택된 노드 목록이라 훅에서 조건 분기에 유용해요.
  • 관련 개념: graph, execute.