반응형
airflow쪽에 dag정보를 얻어오기위해 postgre db를 연결하는 코드를 만든이후에 column에 대한 정보값을 얻으려면 아래와같이 작업해주면된다.
- column정보값을 얻는 코드는 sql 쿼리를 실행한 이후에 cur.description 을 실행해주면된다
import psycopg2
try:
# db connect
conn = psycopg2.connect(
host = "",
dbname = "",
user = "",
password = ""
)
cur = conn.cursor()
# run sql code
cur.execute("""
select *
from (
select dag_id, count(distinct id) as CountId
from dag_run
group by 1) A
where CountId = 2
limit 10
""")
# get column name
print(cur.description)
rows = cur.fetchall()
for i in rows:
print(i)
except Exception as e:
print(e)
반응형
'Data > Python' 카테고리의 다른 글
[Python] ssh로 원격 conda python실행시 주의할 점 (0) | 2022.07.21 |
---|---|
[Python] splitlines, pandas (0) | 2022.03.27 |
[Python] 리스트 일정 구간 통째로 바꾸기 (0) | 2020.05.07 |
[Python] relativedelta함수 (timedelta엔 한달빼는게 왜없을까) (0) | 2020.02.18 |
[Python] python 모듈 PEFile사용해서 API 목록 가져오기 (0) | 2018.09.27 |