1 | from clickhouse_driver import Client |
#####账号和密码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22192.168.0.246
root
密码abc.com123
popo
密码popo.com123
clickhouse
ip:192.168.0.246
tcp端口8123
http端口9000
超管账号:default
密码:123456
读:user_r
密码:1q2w3e4r
读写:user_rw
密码:1a2s3d4f
读写删:user_rwd
密码:1z2x3c4v
方法
1 | def save_clickhouse(data,tb_name): |
1 | t1 = datetime.now() |
SQL语法
增
1 | tuple_data = [tuple(i) for i in df_indus.values] |
改
1 | 1.添加新字段 |
删
1 |
查
1 | 读全部字段数据 |
#####dataframe写入clickhouse1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36========================================1===================================
from clickhouse_driver import Client
client = Client('localhost')
df = pandas.DataFrame.from_records([
{'year': 1994, 'first_name': 'Vova'},
{'year': 1995, 'first_name': 'Anja'},
{'year': 1996, 'first_name': 'Vasja'},
{'year': 1997, 'first_name': 'Petja'},
])
# df processing blablabla...
client.execute("INSERT INTO your_table VALUES", df.to_dict('records'))
========================================2===================================
import pandahouse as ph
pdf = pd.DataFrame.from_records([
{'year': '1994', 'first_name': 'Vova'},
{'year': '1995', 'first_name': 'Anja'},
{'year': '1996', 'first_name': 'Vasja'},
{'year': '1997', 'first_name': 'Petja'},
])
connection = dict(database='pre_formal_1',
host='http://192.168.0.246:8123',
user='user_rwd',
password='1z2x3c4v',
)
ph.to_clickhouse(pdf, 'test_humans2', index=False, chunksize=100000, connection=connection)
======================================3======================================
client = Client(host='192.168.0.170', port='9000', user='algorithm', password='1a2s3d4f', database='algorithm_dis',settings={'use_numpy': True})
sql_insert = 'insert into industryid_name_20211208 values'
client.insert_dataframe(sql_insert,df)
1 | 分区键:通常按照日期,存在不同的文件.我们这用不到,当版本数据存在一个表的时候可以用到 |
##client.query_dataframe(sql) 读成dataframe
####写dataframe到ch
1 | client = Client(host='192.168.0.170', port='9000', user='algorithm', password='1a2s3d4f', database='algorithm_dis',settings={'use_numpy': True}) |
1 | systemctl start clickhouse-server |