Search code examples
pythonclickhouse

How to get data with JSON format in Clickhouse-driver


I'm trying to get my Clickhouse data in my Django project. I'm using clickhouse_driver and :

client.execute('SELECT * FROM myTable LIMIT 5 FORMAT JSON')

When I execute this command in my Clickhouse server SELECT * FROM myTable LIMIT 5 FORMAT JSON it outputs in JSON format. But in python, when I try it with clickhouse_driver it outputs only fields like:

[('2020213','qwerty','asdfg'),('2030103','qweasd','asdxv')]

But I want key-value json format..like

{"logdate":"2020213","host":"qwerty","cef":"asdfg"}

Any suggestions to resolve this problem? Or maybe I have to search for an alternate clickhouse_driver..

Thx.


Solution

  • I did not try Vladimir solution but here is the my solution :

    client.execute commands gives us " with_column_types=True " parameters.. it gives us metadata for table. after :

    result , columns = client.execute('SELECT * FROM myTbl LIMIT 5',with_column_types=True)
    df=pandas.DataFrame(result,columns=[tuple[0] for tuple in columns])
    dfJson=df.to_json(orient='records')
    

    and this gives us our what we want.

    Thx for suggestions :)