ClickHouse奇技淫巧系列之SQL查文件

沒有什么是1個SQL解決不了的.jpg

今天的主題,不用寫序言,看上圖就懂

如何用SQL的方式操作一個文件

先舉例

  • 想知道history命令里,敲得最多的是哪個?
  • 當然,sed,awk完全可以做到,但是,如果用SQL怎么做?
history | awk '{print $1"\t"$2}' | clickhouse-client \
--query="SELECT shell, count() AS c FROM history \
GROUP BY shell ORDER BY c DESC limit 10 " \
--external --file=- --name=history \
--structure='id UInt16, shell String'  -h  127.0.0.1  
ls      390
cd      243
clickhouse-client       173
du      67
vim     57
htop    42
cat     28
history 27
tailf   25
mysql   24

原理

  • ClickHouse支持把一個外部文件,加載到內部的一個臨時表中,對這個臨時表進行SQL化操作

格式

--external --file=... [--name=...] [--format=...] [--types=...|--structure=...]
  • --external 表示這個操作是外部文件的
  • --file=... 指定一個文件,如果是標準輸入,則寫-
  • [--name=...] 表名,如果忽略,默認給_data
  • [--format=...] 列分隔符,默認是TabSeparated
  • `[--types=...|--structure=...] 這句不解釋了,看上面的例子就好了

再來一個測試

  • 為了模擬一個有意義的場景,我們選了ClickHouse的system.parts這個表,里面記錄的是ClickHouse的分區(qū)信息,表結構如下
partition:                             201709
name:                                  20170903_20170905_2_2963928_22
replicated:                            0
active:                                1
marks:                                 23372
rows:                                  191456971
bytes:                                 93294984484
modification_time:                     2017-09-05 23:37:33
remove_time:                           0000-00-00 00:00:00
refcount:                              2
min_date:                              2017-09-03
max_date:                              2017-09-05
min_block_number:                      2
max_block_number:                      2963928
level:                                 22
primary_key_bytes_in_memory:           93488
primary_key_bytes_in_memory_allocated: 196608
database:                              xx
table:                                 xx
engine:                                MergeTree
  • 我們導出一份數據,作為測試文件
  • 默認導出的文件是tab分割
clickhouse-client -h  127.0.0.1 -m -d system -q "select * from parts " > test.sql 
  • 目標SQL
  • 找某個表的分區(qū)數據,即有幾個分區(qū),分區(qū)文件多大
SELECT 
    partition, 
    count() AS number_of_parts, 
    formatReadableSize(sum(bytes)) AS sum_size
FROM system.parts 
WHERE active AND (database = 'xxxx') AND (table = 'xxxx_msg')
GROUP BY partition
ORDER BY partition ASC
  • 文件SQL
root@10.xxxx:/root  # wc -l test.sql 
11991 test.sql
root@10.xxxx:/root  # clickhouse-client \
--query="SELECT partition,  count() AS number_of_parts, \
formatReadableSize(sum(bytes)) AS sum_size FROM parts  \
WHERE active AND (database = 'xxxx') AND (table = 'xxxx_msg') \
GROUP BY partition ORDER BY partition ASC ;" \
--external --file=test.sql --name=parts \
--structure='partition UInt16,name String,replicated UInt16,active UInt16,marks UInt16,rows UInt16,bytes UInt16,modification_time String,remove_time String,refcount UInt16,min_date String,max_date String,min_block_number UInt16,max_block_number UInt16,level UInt16,primary_key_bytes_in_memory UInt16,primary_key_bytes_in_memory_allocated UInt16,database String,table String,engine String'  \
-h  127.0.0.1  
201709  36      1.68 TiB
201710  26      1.42 TiB
201711  30      1.42 TiB
201712  31      963.07 GiB

注意事項

  • 文件操作雖然方便,但是官方文檔也提到了,如果是特別大的文件,還是不要這么玩了
  • 另外,這個文件SQL其實還是要依賴ClickHouse-Server的,如果你沒有啟動Server,玩不了的哦~
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容