1. clickhouse修改字段类型
当别人往表中插入数据,也就是对字段进行操作时,是不能修改字段类型的
此时若要修改字段类型,需要先drop掉该字段,再进行add,如果单纯使用modify是不会修改成功的。
🌰:
原始 col1 类型是 string,此时若要修改为int32:
alter table base.table ON CLUSTER *** modify column col1 Int32;
but:如果此列一直在进行被修改,比如插入数据,则会报错:
Exception: Received from 10.2.29.1:9000 . DB::Exception: There was an error on [10.2.29.2:9000] : Code: 439, e.displayText () = DB::Exception: Cannot schedule a task (version 19.9.2.4 (official build)).
此时应该先删除此列,再重新add列及数据类型
ALTER TABLE base.table ON CLUSTER *** DROP COLUMN col1; --drop列
alter table base.table ON CLUSTER *** add column col1 Int32; --add列
2. clickhouse建表,导入CSV数据
如果表中没有时间戳,也没有key,则可以使用 ENGINE = TinyLog;
CREATE TABLE defult.industry_tag (`industry` String, `tag` String) ENGINE = TinyLog;
导入CSV数据时,如果csv表中有相应的列明,则需要指明使用 FORMAT CSVWithNames,如果csv表没有列明,则使用 FORMAT CSV
🌰:
1. 倘若CSV长这样:
industry,tag
社交,QQ
社交,WeChat
社交,陌陌
使用如下代码导入到clickhouse
cat industry-tag.csv | clickhouse-client -h 10.2.**.* --password *** -m --query="INSERT INTO defult.industry_tag FORMAT CSVWithNames";
2. 倘若CSV长这样:
社交,QQ
社交,WeChat
社交,陌陌
使用如下代码导入到clickhouse
cat industry-tag.csv | clickhouse-client -h 10.2.**.* --password *** -m --query="INSERT INTO defult.industry_tag FORMAT CSV";
3. 要改字段类型,需先改底层表,再改分布式表,否则报错
4. 建分布式表
CREATE TABLE IF NOT EXISTS database.test_dist_table( event_date Date MATERIALIZED toDate(now()) , pv Int32 , click Int32 , download Int32 , install Int32 , active Int32 , request Int32 ) ENGINE = Distributed(cluster_name***, database, test_table, rand()); --test_table为非分布式底层表