How to get pd.Dataframe from ClickHouseHook()?

I got this code in my DAG for Airflow:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import datetime
import io
import httpx
from airflow.decorators import dag, task
from airflow.models import Variable
from clickhouse_driver import Client
from airflow_clickhouse_plugin.hooks.clickhouse import ClickHouseHook
from airflow.models import Connection
from airflow.utils.db import create_session
default_args = {
'owner': 'd-chernovol',
'depends_on_past': False,
'retries': 2,
'retry_delay': datetime.timedelta(minutes=5),
'start_date': datetime.datetime(2024, 6, 20)
}
schedule_interval = '*/20 * * * *'
host = Variable.get('host')
database_name = Variable.get('database_name')
user_name = Variable.get('user_name')
password_for_db = Variable.get('password_for_db')
bot_token = Variable.get('bot_token')
chat_id = Variable.get('chat_id')
connections_name = 'clickhouse_default'
try:
# create Connection
clickhouse_conn = Connection(
conn_id=connections_name,
conn_type='ClickHouse',
host=host,
schema=database_name,
login=user_name,
password=password_for_db,
port=8443
)
# create connection
with create_session() as session:
session.add(clickhouse_conn)
session.commit()
# make ClickHouseHook
ch_hook = ClickHouseHook(clickhouse_conn_id=connections_name)
except:
ch_hook = ClickHouseHook(clickhouse_conn_id=connections_name)
@dag(default_args=default_args, schedule_interval=schedule_interval, catchup=False, concurrency=3)
def test_dag_4():
@task
def get_table_from_db(sql_query):
result = ch_hook.execute(sql_query)
return result
@task
def send_msg(bot_token: str, chat_id: str, message: str):
# send_message
url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={message}'
client = httpx.Client()
return client.post(url)
get_table_from_db_task >> send_msg_task
st_dag = test_dag_4()
</code>
<code>import pandas as pd import datetime import io import httpx from airflow.decorators import dag, task from airflow.models import Variable from clickhouse_driver import Client from airflow_clickhouse_plugin.hooks.clickhouse import ClickHouseHook from airflow.models import Connection from airflow.utils.db import create_session default_args = { 'owner': 'd-chernovol', 'depends_on_past': False, 'retries': 2, 'retry_delay': datetime.timedelta(minutes=5), 'start_date': datetime.datetime(2024, 6, 20) } schedule_interval = '*/20 * * * *' host = Variable.get('host') database_name = Variable.get('database_name') user_name = Variable.get('user_name') password_for_db = Variable.get('password_for_db') bot_token = Variable.get('bot_token') chat_id = Variable.get('chat_id') connections_name = 'clickhouse_default' try: # create Connection clickhouse_conn = Connection( conn_id=connections_name, conn_type='ClickHouse', host=host, schema=database_name, login=user_name, password=password_for_db, port=8443 ) # create connection with create_session() as session: session.add(clickhouse_conn) session.commit() # make ClickHouseHook ch_hook = ClickHouseHook(clickhouse_conn_id=connections_name) except: ch_hook = ClickHouseHook(clickhouse_conn_id=connections_name) @dag(default_args=default_args, schedule_interval=schedule_interval, catchup=False, concurrency=3) def test_dag_4(): @task def get_table_from_db(sql_query): result = ch_hook.execute(sql_query) return result @task def send_msg(bot_token: str, chat_id: str, message: str): # send_message url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={message}' client = httpx.Client() return client.post(url) get_table_from_db_task >> send_msg_task st_dag = test_dag_4() </code>
import pandas as pd
import datetime
import io
import httpx
from airflow.decorators import dag, task
from airflow.models import Variable
from clickhouse_driver import Client
from airflow_clickhouse_plugin.hooks.clickhouse import ClickHouseHook
from airflow.models import Connection
from airflow.utils.db import create_session


default_args = {
    'owner': 'd-chernovol',
    'depends_on_past': False,
    'retries': 2,
    'retry_delay': datetime.timedelta(minutes=5),
    'start_date': datetime.datetime(2024, 6, 20)
}
schedule_interval = '*/20 * * * *'
host = Variable.get('host')
database_name = Variable.get('database_name')
user_name = Variable.get('user_name')
password_for_db = Variable.get('password_for_db')
bot_token = Variable.get('bot_token')
chat_id = Variable.get('chat_id')
connections_name = 'clickhouse_default'
try:
    # create Connection
    clickhouse_conn = Connection(
        conn_id=connections_name,
        conn_type='ClickHouse',
        host=host,
        schema=database_name,
        login=user_name,
        password=password_for_db,
        port=8443
    )
    # create connection
    with create_session() as session:
        session.add(clickhouse_conn)
        session.commit()
    # make ClickHouseHook
    ch_hook = ClickHouseHook(clickhouse_conn_id=connections_name)
except:
    ch_hook = ClickHouseHook(clickhouse_conn_id=connections_name)

@dag(default_args=default_args, schedule_interval=schedule_interval, catchup=False, concurrency=3)
def test_dag_4():
    @task
    def get_table_from_db(sql_query):
        result = ch_hook.execute(sql_query)
        return result

    @task
    def send_msg(bot_token: str, chat_id: str, message: str):
        # send_message
        url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={message}'
        client = httpx.Client()
        return client.post(url)

get_table_from_db_task >> send_msg_task
st_dag = test_dag_4()

My code is working, but get_table_from_db(sql_query) returns:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class 'airflow.models.xcom_arg.PlainXComArg'>
</code>
<code>class 'airflow.models.xcom_arg.PlainXComArg'> </code>
class 'airflow.models.xcom_arg.PlainXComArg'>

If I convert result to string I get list with values from table:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[[111, 'name_1'], [222, 'name_2'], [222, 'name_3']]
</code>
<code>[[111, 'name_1'], [222, 'name_2'], [222, 'name_3']] </code>
[[111, 'name_1'], [222, 'name_2'], [222, 'name_3']]

But I need to get table from my database as pandas.Dataframe.
How I can achieve it? Or I should write function for this?
The main problem, that I get only values without column names.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật