clickhouse pythonで外部データテーブルを接続する

1236 ワード

https://clickhouse-driver.readthedocs.io/en/latest/features.html
https://github.com/mymarilyn/clickhouse-driver
from clickhouse_driver import Client
client = Client('172.xx.x.xx')
res= client.execute(
'''
 select XXXXXXXXXXXX from xxx
''',external_tables=tables, with_column_types=True
)
res[0],res[1]

引用符には通常のsqlと書いて、外部テーブルを書くこともデータベーステーブルを書くこともできます.
そしてdataframeに移動
df = pd.DataFrame(data=res[0], columns=[i[0] for i in res[1]])
************************************************************************************************************************************************
df.to_pickle('a.pkl')
xxx = pd.read_pickle('a.pkl') xxx.head()
これはdfをpklファイルに保存するもので、csvよりも速く、読むときも正常にdfを読み出すそうです.
 
************************************************************************************************************************************************
shop連clickhouse(自分の)直接出力df、mark牛迫!
from shop.clickhouse import select,Client
import random
hosts=['172.xx.xx.xx']
ch=Client(random.choice(hosts),database='xxx   ')
sql='select xxx from xxx'  #  select    
select(ch,sql)