본문 바로가기

데이터 시각화/superset

Apache Superset(아파치 슈퍼셋) - jinja template 사용

Superset에서는 jinja template을 사용하여 쿼리를 작성할 수 있다. 

jinja template은 default가 사용하지 않음으로 처리되어 있기 때문에 superset_config.py 파일에서 사용하도록 ("ENABLE_TEMPLATE_PROCESSING": True) 셋팅을 해줘야 한다.

 

superset_config.py 파일을 셋팅하는 방법은 아래 글을 참고... 

2021.11.20 - [open source BI/superset] - Apache Superset(아파치 슈퍼셋) 설정파일(superset_config.py)

 

Apache Superset(아파치 슈퍼셋) 설정 파일(superset_config.py)

Superset에서는 다양한 기능을 제공하고 있고 이를 설정 파일을 통해 관리하고 있다. default 값들은 config.py 파일에 정의되어 있다.(자세한 내용은 아래 링크 참고) https://github.com/apache/superset/blob/m..

tedparkdev.tistory.com

 

FEATURE_FLAGS = {
	"ENABLE_TEMPLATE_REMOVE_FILTERS" : True,
	"ENABLE_TEMPLATE_PROCESSING": True,	
}

개인적으로는 "ENABLE_TEMPLATE_REMOVE_FILTERS" : True 를 같이 설정을 해주는 것을 추천한다.

Dashboard의 filter 조건과 jinja template으로 작성한 조건이 동일한 경우 중복으로 적용이 되어 원하는대로 결과가 리턴되지 않는 경우가 있다. 이런 것을 방지하기 위해 remove_filter=True로 filter 조건이 적용되지 않도록 명시하는 기능이 있는데 이 기능을 사용하기 위해서는 "ENABLE_TEMPLATE_REMOVE_FILTERS" : True로 설정을 해야한다. 

자세한 내용은 아래를 참고

https://github.com/apache/superset/pull/14507

 

feat: Add a remove_filter flag to the jinja filter_values function and add a new get_filters function (see issue 13943 for more

SUMMARY Implementation of issue #13943. Feature must be enabled with feature flag ENABLE_TEMPLATE_REMOVE_FILTERS. SCREENSHOT

github.com

 

Superset에서 미리 정의되어 있는 변수들은 다음과 같다.

  • columns: columns which to group by in the query
  • filter: filters applied in the query
  • from_dttm: start datetime value from the selected time range (None if undefined)
  • to_dttm: end datetime value from the selected time range (None if undefined)
  • groupby: columns which to group by in the query (deprecated)
  • metrics: aggregate expressions in the query
  • row_limit: row limit of the query
  • row_offset: row offset of the query
  • table_columns: columns available in the dataset
  • time_column: temporal column of the query (None if undefined)
  • time_grain: selected time grain (None if undefined)

jinja template을 이용하여 아래와 같이 쿼리를 작성할 수 있다.

여기서 주의해야 할 점은 우측 하단의 ... 아이콘을 클릭하여 Parameters에 jinja template 변수들의 값을 지정해줘야 된다. 그냥 쿼리를 Run 하게 되면 오류가 발생한다. 

 

 

그런데 이렇게 Parameters를 셋팅해놓은 상태에서 Chart를 만들게 되면 Chart의 Dataset에도 이 Parameter가 계속 유지되게 되고 Dashboard의 필터 항목과 jinja template 변수가 겹치는 경우에는 Dashboard의 filter 값을 변경해도 filter에서 선택한 값이 적용되지 않는다. 그러므로 개인적으로는 Chart의 Dataset에서 Template parameters의 값은 지워놓는 것을 추천한다.

Chart의 Dataset을 수정하는 방법은 아래 이미지를 참고

 


참고자료

https://medium.com/geekculture/interactive-dashboards-in-apache-superset-ec4a3bf9bc82

 

Interactive Dashboards in Apache Superset

What if you want to make your dashboard more interactive? What if you have a complex SQL query for your dataset and you have to implement…

medium.com

https://github.com/apache/superset/pull/14507

 

feat: Add a remove_filter flag to the jinja filter_values function and add a new get_filters function (see issue 13943 for more

SUMMARY Implementation of issue #13943. Feature must be enabled with feature flag ENABLE_TEMPLATE_REMOVE_FILTERS. SCREENSHOT

github.com

 

반응형