Clickhouse 时区
Clickhouse 時(shí)區(qū)
?
默認(rèn)設(shè)置
Clickhouse默認(rèn)是讀取操作系統(tǒng)的時(shí)區(qū) 我們可以通過操作系統(tǒng)命令和clickhouse的命令查看驗(yàn)證:
Clickhouse> select now();
?
SELECT now()
?
┌───────────────now()─┐
│ 2020-07-11 23:47:56 │
└─────────────────────┘
?
1 rows in set. Elapsed: 0.003 sec.?
?
Clickhouse> exit;
Bye.
[root@hadoop ~]# date
Sat Jul 11 23:48:01 CST 2020
?
?
?
此時(shí)操作系統(tǒng)的時(shí)區(qū)和時(shí)間是:
# timedatectl
? ? ? Local time: Sat 2020-07-11 23:49:06 CST
? Universal time: Sat 2020-07-11 15:49:06 UTC
? ? ? ? RTC time: Sat 2020-07-11 15:49:05
? ? ? ?Time zone: Asia/Shanghai (CST, +0800)
? ? ?NTP enabled: n/a
NTP synchronized: no
?RTC in local TZ: no
? ? ? DST active: n/a
?
操作系統(tǒng)的命令:
# timedatectl list-timezones
ist-timezones 列出系統(tǒng)上支持的時(shí)區(qū)
set-timezone 設(shè)定時(shí)區(qū)
set-time 設(shè)置時(shí)間
set-btp 設(shè)置同步ntp
?
示例:設(shè)置時(shí)區(qū)示例:
timedatec修改時(shí)區(qū)
timedatectl set-timezone "America/New_York"
?
# timedatectl set-timezone Asia/Shanghai
ntp設(shè)置:
yum -y install ntp?
systemctl enable ntpd?
systemctl start ntpd
?同步時(shí)間
ntpdate -u cn.pool.ntp.org
clickhouse提供了配置的參數(shù)選型:
1.修改設(shè)置
sudo vim /etc/clickhouse-server/config.xml
?
<timezone>Asia/Shanghai</timezone>
?由于clickhouse是俄羅斯人主導(dǎo)開發(fā)的,默認(rèn)設(shè)置為Europe/Moscow
2.重啟服務(wù)器:
sudo service clickhouse-server restart
?
?
我們可以看到選型的說(shuō)明如下:
?<!-- Server time zone could be set here.
? ? ? ? ?Time zone is used when converting between String and DateTime types,
? ? ? ? ? when printing DateTime in text formats and parsing DateTime from text,
? ? ? ? ? it is used in date and time related functions, if specific time zone was not passed as an argument.
? ? ? ? ?Time zone is specified as identifier from IANA time zone database, like UTC or Africa/Abidjan.
? ? ? ? ?If not specified, system time zone at server startup is used.
? ? ? ? ?Please note, that server could display time zone alias instead of specified name.
? ? ? ? ?Example: W-SU is an alias for Europe/Moscow and Zulu is an alias for UTC.
? ? -->
? ? <!-- <timezone>Europe/Moscow</timezone> -->
?
時(shí)區(qū)在日期時(shí)間相關(guān)的函數(shù),若指定時(shí)區(qū)作為參數(shù)。在Datetime和String類型之間進(jìn)行轉(zhuǎn)換。
時(shí)區(qū)的指定是按照IANA標(biāo)準(zhǔn)的時(shí)區(qū)庫(kù)指定的,可以在Linux系統(tǒng)中通過命令查詢
若不指定則使用系統(tǒng)啟動(dòng)的時(shí)區(qū)。
?clickhouse相關(guān)的時(shí)區(qū)函數(shù):
Clickhouse> select formatDateTime(now(),'%F %T') as dt,toString(toDateTime(dt),'Asia/Shanghai') as BJ_time,toString(toDateTime(dt),'America/New_York') as NY_time;
?
SELECT?
? ? formatDateTime(now(), '%F %T') AS dt,
? ? toString(toDateTime(dt), 'Asia/Shanghai') AS BJ_time,
? ? toString(toDateTime(dt), 'America/New_York') AS NY_time
?
┌─dt──────────────────┬─BJ_time─────────────┬─NY_time─────────────┐
│ 2020-07-12 00:13:29 │ 2020-07-12 00:13:29 │ 2020-07-11 12:13:29 │
└─────────────────────┴─────────────────────┴─────────────────────┘
?
1 rows in set. Elapsed: 0.006 sec.?
?
?
可以看到Clickhouse默認(rèn)采用的系統(tǒng)的UTC
?
?
Clickhouse> select formatDateTime(now(),'%F %T') as dt,toString(toDateTime(dt,'UTC'),'Asia/Shanghai') as BJ_time,toTimeZone(toDateTime(dt,'UTC'),'Asia/Shanghai') as random_time,toString(toDateTime(dt),'Asia/Shanghai') SH_TIME,toTimeZone(toDateTime(dt),'Asia/Shanghai') SH_time,toTimeZone(toDateTime(dt,'America/New_York'), 'Asia/Hong_Kong') HK_time;
?
SELECT?
? ? formatDateTime(now(), '%F %T') AS dt,
? ? toString(toDateTime(dt, 'UTC'), 'Asia/Shanghai') AS BJ_time,
? ? toTimeZone(toDateTime(dt, 'UTC'), 'Asia/Shanghai') AS random_time,
? ? toString(toDateTime(dt), 'Asia/Shanghai') AS SH_TIME,
? ? toTimeZone(toDateTime(dt), 'Asia/Shanghai') AS SH_time,
? ? toTimeZone(toDateTime(dt, 'America/New_York'), 'Asia/Hong_Kong') AS HK_time
?
┌─dt──────────────────┬─BJ_time─────────────┬─────────random_time─┬─SH_TIME─────────────┬─────────────SH_time─┬─────────────HK_time─┐
│ 2020-07-12 00:27:25 │ 2020-07-12 08:27:25 │ 2020-07-12 08:27:25 │ 2020-07-12 00:27:25 │ 2020-07-12 00:27:25 │ 2020-07-12 12:27:25 │
└─────────────────────┴─────────────────────┴─────────────────────┴─────────────────────┴─────────────────────┴─────────────────────┘
?
1 rows in set. Elapsed: 0.007 sec.?
?
可以看到toTimeZone,與toString 的功能很像
?
?
Clickhouse> select formatDateTime(now(),'%F %T') as dt,toTypeName(toString(toDateTime(dt),'Asia/Shanghai')) SH_TIME,toTypeName(toTimeZone(toDateTime(dt),'Asia/Shanghai')) SH_time,toTypeName(toTimeZone(toDateTime(dt,'America/New_York'), 'Asia/Hong_Kong')) HK_time;
?
SELECT?
? ? formatDateTime(now(), '%F %T') AS dt,
? ? toTypeName(toString(toDateTime(dt), 'Asia/Shanghai')) AS SH_TIME,
? ? toTypeName(toTimeZone(toDateTime(dt), 'Asia/Shanghai')) AS SH_time,
? ? toTypeName(toTimeZone(toDateTime(dt, 'America/New_York'), 'Asia/Hong_Kong')) AS HK_time
?
┌─dt──────────────────┬─SH_TIME─┬─SH_time───────────────────┬─HK_time────────────────────┐
│ 2020-07-12 00:29:43 │ String ?│ DateTime('Asia/Shanghai') │ DateTime('Asia/Hong_Kong') │
└─────────────────────┴─────────┴───────────────────────────┴────────────────────────────┘
?
1 rows in set. Elapsed: 0.002 sec.?
toTimeZone函數(shù)可以實(shí)現(xiàn)時(shí)區(qū)轉(zhuǎn)換,通過toTypeName還可以獲知字段類型,以及該字段對(duì)應(yīng)的時(shí)區(qū)。
相關(guān)的函數(shù)
————————————————
版權(quán)聲明:本文為CSDN博主「vkingnew」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/vkingnew/article/details/107227037
總結(jié)
以上是生活随笔為你收集整理的Clickhouse 时区的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Radio单选框
- 下一篇: Zip文件的解析与生成