一、導入數據
1、同步mysql庫中表
CREATE TABLE tab1 ENGINE = MergeTree ORDER BY id AS SELECT * FROM mysql('hostip:3306', 'db', 'table', 'user', 'passwd') ;
注:id mysql中的主鍵
2、csv文件導入clickhouse
2.1、創建表
CREATE TABLE tab1(
eventDate Date,
impid UInt64,
uid String,
idfa String,
imei String
) ENGINE=MergeTree(eventDate, (impid, eventDate), 8192)
一般情況下, 都建議使用 MergeTree 引擎. 這個引擎必須要有一個 Date 的列來作為索引, 即上面的 eventDate.
2.2、導入CSV數據
cat test.csv | clickhouse-client -u user --password password --query="INSERT INTO db.tab1 FORMAT CSV";
指定分隔符
cat test.csv | clickhouse-client -u user --password password --format_csv_delimiter="|" --query="INSERT INTO db.tab1 FORMAT CSV";
導入數據時忽略錯誤
clickhouse-client --input_format_allow_errors_num=100000 --input_format_allow_errors_ratio=0.2
--input_format_allow_errors_num : 是允許的錯誤數
--input_format_allow_errors_ratio : 是允許的錯誤率, 范圍是 [0-1]
3、采用remote函數
insert into db.tab1 select * from remote('目標IP',db.table,'user','passwd')
4、clickhouse-copier 工具
5、hdfs導入數據
ClickHouse與Hadoop的兼容性不好,數據交互還是依靠將數據導出為固定格式的文件,然后將文件導入到ClickHouse中。
准備
創建student.csv文件,
添加內容如下:
1,lis
2,wangw
3,zhaos
上傳到HDFS
[hadoop01@localhost webapps]# hadoop fs -put student.csv /
5.1、從HDFS讀取數據
從HDFS上讀取數據類似於將HDFS作為外部存儲,然后去拉取HDFS上的數據。
需要用到一個新的引擎HDFS:
CREATE TABLE hdfs_student_csv(
id Int8,
name String
)
Engine=HDFS('hdfs://hadoop01:9000/student.csv','csv');
但是數據實際上還是在HDFS上,如果關掉HDFS,他就會報錯。
5.2、從HDFS導入數據
我們想要將讀取到的數據保存到本地,只需要將讀取數據的表導入其他的本地表。
創建一張表結構和hdfs_student_csv完全一致,但是表引擎無所謂。
CREATE TABLE student_local(
id Int8,
name String
)
Engine=TinyLog;
導入數據:
insert into student_local select * from hdfs_student_csv;
6、INSERT INTO插入數據
主要用於向表中添加數據,基本格式如下:
INSERT INTO [db.]table [(c1, c2, c3)] VALUES (v11, v12, v13), (v21, v22, v23), ...
還可以使用select來寫入數據:
INSERT INTO [db.]table [(c1, c2, c3)] SELECT ...
insert into t2 select * from t3
不嚴格插入數據,沒有出現的列自動填充為默認值
INSERT INTO [db.]table [(c1, c2, c3)] VALUES (v11, v12, v13), (v21, v22)
嚴格插入數據,每一列都必須出現在上面
INSERT INTO [db.]table [(c1, c2, c3)] FORMAT Values (v11, v12, v13), (v21, v22, v23)
ClickHouse不支持的修改數據的查詢:UPDATE, DELETE, REPLACE, MERGE, UPSERT, INSERT UPDATE。
二、導出數據
1、導出 CSV 數據
clickhouse-client --query="select uid, idfa, imei from (select impid, uid from tab1 where impid >= 15289903030261609347 and impid <= 15289904230261609347) any inner join (select impid, idfa, imei from tab1 where impid >= 15289903030261609347 and impid <= 15289904230261609347) using(impid) format CSV" > 9c9dc608-269b-4f02-b122-ef5dffb2669d.log
三、刪除庫、表、數據、分區
1、刪除庫
2、刪除表
drop table tabl;
刪除集群多個節點同一張表
drop table tabl on cluster clickhouse_cluster;
3、刪除數據
刪除表的數據,對主鍵支持的可以,非主鍵有時數據刪除有問題
刪除指定數據
ALTER TABLE <table> DELETE WHERE <filter expression>
全部刪除
您始終必須指定過濾器表達式。如果要通過Mutation刪除所有數據,請指定始終為true的內容,例如:
ALTER TABLE <table> DELETE WHERE 1=1
truncate table tabl;
清理集群表數據
truncate table lmmbase.user_label_uid on cluster crm_4shards_1replicas;
4、刪除分區
按時間分區:
toYYYYMM(EventDate):按月分區
toMonday(EventDate):按周分區
toDate(EventDate):按天分區
按指定列分區:
PARTITION BY cloumn_name
對分區的操作:
alter table tab1 DROP PARTITION [partition] #刪除分區
alter table tab1 DETACH PARTITION [partition]#下線分區
alter table tab1 ATTACH PARTITION [partition]#恢復分區
MergeTree 引擎中刪除分區
注意, 默認情況下 mergeTree 引擎是按月分區的, 刪除分區的格式為 201808
如果想修改為按日分區, 則在建表時加上:
ENGINE = MergeTree PARTITION BY eventDate ORDER BY imp_id SETTINGS index_granularity = 8192;
然后就可以:
alter table xxx drop partition '2018-08-08';
默認情況下, Clickhouse 不允許刪除分區或表的大小大於 50GB 的分區或表. 可以通過修改server的配置文件來永久配置. 也可以臨時設置一下來刪除而不用重啟服務.
永久配置
sudo vim /etc/clickhouse-server/config.xml
然后注釋掉下面兩行
<!-- <max_table_size_to_drop>0</max_table_size_to_drop> -->
<!-- <max_partition_size_to_drop>0</max_partition_size_to_drop> -->
0表示不限制. 或者你可以設置為你想限制的最大的大小.
臨時設置
創建個標志文件:
sudo touch '/home/username/clickhouse/flags/force_drop_table' && sudo chmod 666 '/home/username/clickhouse/flags/force_drop_table'
創建好之后, 就可以執行上面的刪除分區或表的命令了.
四、更新
ALTER只支持MergeTree系列,Merge和Distributed引擎的表,基本語法:
ALTER TABLE [db].name [ON CLUSTER cluster] ADD|DROP|MODIFY COLUMN ...
參數解析:
ADD COLUMN – 向表中添加新列
DROP COLUMN – 在表中刪除列
MODIFY COLUMN – 更改列的類型
1、更新數據
也可以用類似的方法進行變異(UPDATE)
ALTER TABLE <table> UPDATE column1 = expr1 [, ...] WHERE <filter expression>
2、變更表名
rename table tabl1 to tabl2;
3、添加新列
alter table tab1 add column age UInt32 default 0;
4、更改列的類型
alter table tab1 modify column age UInt16
5、刪除age列
alter table tab1 drop column age
6、查看表結構
desc tabl;
五、表創建
1、創建本地表 drop TABLE if exists idc.web_element_detail_dist on CLUSTER idc_cluster ; drop TABLE if exists idc.web_element_detail on CLUSTER idc_cluster ; CREATE TABLE if not exists idc.web_element_detail on CLUSTER idc_cluster ( `task_id` UInt64 COMMENT '撥測任務id', `target` String COMMENT '域名/網址', `target_name` String COMMENT '網址名稱', `element` String COMMENT '元素名稱', `report_time` DateTime COMMENT '上報時間', `net_type` String COMMENT '網絡接入方式', `probe_id` String COMMENT '探針id', `opt_type` String COMMENT '運營商類型', `opt_name` String COMMENT '運營商名稱', `province_id` UInt32 COMMENT '省份編碼', `province_name` String COMMENT '省份名稱', `city_id` UInt32 COMMENT '地市編碼', `city_name` String COMMENT '地市名稱', `area_id` UInt32 COMMENT '區縣編碼', `area_name` String COMMENT '區縣名稱', `busi_type` String COMMENT '業務類型', `element_num` String COMMENT '元素個數', `idc_ip` String COMMENT '目標ip地址', `idc_delay` Float32 COMMENT 'idc延遲', `idc_size` Float32 COMMENT 'idc大小' , `ip_opt_type` String COMMENT '目標運營商類型', `ip_opt_name` String COMMENT '目標運營商名稱', `ip_province_id` UInt32 COMMENT '目標IP省份編碼', `ip_province_name` String COMMENT '目標IP省份名稱', `ip_city_id` UInt32 COMMENT '目標IP地市編碼', `ip_city_name` String COMMENT '目標IP地市名稱', `ip_area_id` UInt32 COMMENT '目標IP區縣編碼', `ip_area_name` String COMMENT '目標IP區縣名稱', `five_min` UInt32, `ten_min` UInt32, `half_hour` UInt32, `one_hour` UInt32, `four_hour` UInt32, `half_day` UInt32 ) ENGINE = MergeTree() PARTITION BY (task_id, toYYYYMMDD(report_time)) ORDER BY (target, report_time) SETTINGS index_granularity = 8192; 2、創建分布式表 CREATE TABLE idc.web_element_detail_dist on CLUSTER idc_cluster AS idc.web_element_detail ENGINE = Distributed(idc_cluster, idc, web_element_detail, rand());
六、檢查表數據損壞
CHECK TABLE
檢查表中的數據是否損壞,他會返回兩種結果:
0 – 數據已損壞
1 – 數據完整
該命令只支持Log,TinyLog和StripeLog引擎。
七、join 表性能
join 表性能
切記, 要用大表 join 小表. (不知道具體為什么, 從經驗上看, 用大表作為驅動表, 性能遠遠快於用小表作為驅動表). (MySQL 里的話, 則是小表驅動大表).
優化 distinct count
之前
select yob, count(), count(distinct uid, idfa, imei) from nginx_bid_log where eventDate='2018-9-1' group by yob;
之后
select yob, count(), count(distinct(sipHash64(concat(uid, idfa, imei)))) from nginx_bid_log where eventDate='2018-9-1' group by yob;
查看數據分布
select histogram(100)(upstream_resp_time) from (select upstream_resp_time from nginx_bid_log where eventDate = '2018-12-13') format CSV;
histogram(100) 表示組距100 (即分成100等份的的分布) , 后面的 upstream_resp_time 是你的列名, 即按這個列的數據來進行統計.
select upstream_resp_time, bar(列名, 最小值, 最大, step) from tableXX;
顯示簡單的圖形.
hex 十六進制 轉換為 十進制
SELECT reinterpretAsInt64(reverse(unhex('123')));
md5 分區
# 一
SELECT reinterpretAsInt64(reverse(unhex(substring(md5_field, 1, 1))));
# 二, md5 => hex => 十進制 => 取模
SELECT modulo(reinterpretAsInt64(reverse(unhex(substring(md5_field, 1, 1)))), 5);