Clickhouse(6)---Clickhouse函数

日期,字符串,类型转换,条件语句,舍入函数

本文为转载博客,防止原博客丢失,自己保存一份!
ClickHouse函数整理(详细)

1、日期类函数

1.1 时间或日期截取函数(to)—— 返回非日期

函数用途举例结果
toYear()取日期或时间日期的年份toYear(toDateTime(‘2018-12-11 11:12:13’)) toYear(toDate(‘2018-12-11’))返回 2018 返回 2018
toMonth()取日期或时间日期的月份toMonth(toDateTime(‘2018-12-11 11:12:13’)) toMonth(toDate(‘2018-12-11’))返回 12返回 12
toDayOfMonth()取日期或时间日期的天(1-31)toMonth(toDayOfMonth(‘2018-12-11 11:12:13’)) toMonth(toDayOfMonth(‘2018-12-11’))返回 11返回 11
toDayOfWeek()取日期或时间日期的星期(星期一为1,星期日为7)。toDayOfWeek(toDateTime(‘2018-12-11 11:12:13’)) toDayOfWeek(toDate(‘2018-12-11’))返回 2返回 2
toHour()取时间日期的小时toHour(toDateTime(‘2018-12-11 11:12:13’))返回 11
toMinute()取时间日期的分钟toMinute(toDateTime(‘2018-12-11 11:12:13’))返回 12
toSecond()取时间日期的秒toSecond(toDateTime(‘2018-12-11 11:12:13’))返回 13
toMonday()取时间日期最近的周一(返回日期)toMonday(toDate(‘2018-12-11’)) toMonday(toDateTime(‘2018-12-11 11:12:13’))返回 2018-12-10返回 2018-12-10
toTime()将时间日期的日期固定到某一天,保留原始时间toTime(toDateTime(‘2018-12-11 11:12:13’))返回 1970-01-02 11:12:13

1.2 时间或日期截取函数(toStartOf)—— 返回日期

函数用途举例结果
toStartOfMonth()取日期或时间日期的月份的第一天,返回日期toStartOfMonth(toDateTime(‘2018-12-11 11:12:13’))toStartOfMonth(toDate(‘2018-12-11’))返回 2018-12-01返回 2018-12-01
toStartOfQuarter()取日期或时间日期的季度的第一天,返回日期toStartOfQuarter(toDateTime(‘2018-12-11 11:12:13’))toStartOfQuarter(toDate(‘2018-12-11’))返回 2018-10-01返回 2018-10-01
toStartOfYear()取日期或时间日期的年份的第一天,返回日期toStartOfYear(toDateTime(‘2018-12-11 11:12:13’))toStartOfYear(toDate(‘2018-12-11’))返回 2018-01-01返回 2018-01-01
toStartOfMinute()截取时间日期到分钟(之后归零),返回日期toStartOfMinute(toDateTime(‘2018-12-11 11:12:13’))返回 2018-12-11 11:12:00
toStartOfFiveMinute()截取时间日期到最近的5的倍数分钟(之后归零),返回日期toStartOfFiveMinute(toDateTime(‘2018-12-11 11:12:13’))返回 2018-12-11 11:10:00
toStartOfFifteenMinutes()截取时间日期到最近的15的倍数分钟(之后归零),返回日期toStartOfFifteenMinutes(toDateTime(‘2018-12-11 11:12:13’))返回 2018-12-11 11:00:00
toStartOfHour()截取时间日期到小时(之后归零),返回日期toStartOfHour(toDateTime(‘2018-12-11 11:12:13’))返回 2018-12-11 11:00:00
toStartOfDay()截取时间日期到天(之后归零),返回日期toStartOfDay(toDateTime(‘2018-12-11 11:12:13’))返回 2018-12-11 00:00:00
timeSlot()将时间日期中,分钟大于等于30的归于30,分钟数小于30的归为00timeSlot(toDateTime(‘2018-12-11 11:33:13’))timeSlot(toDateTime(‘2018-12-11 11:33:13’))返回 2018-12-11 11:00:00返回 2018-12-11 11:30:00

1.3 日期或时间日期生成函数

函数用途举例结果
now()生成当前时间日期now()返回 2018-12-13 10:10:12
today()生成今天的日期today()返回 2018-12-13
yesterday()生成昨天的日期yesterday()返回 2018-12-12

2、类型转化类函数

2.1 精度保留(非四舍五入)

函数用途举例结果
toDecimal32(‘whdwjfew’,8)将数值型或者含有非数字的字符串进行精度保留toDecimal32(23.12291, 3)toDecimal32(’_23.12291’, 3)返回 23.122返回 0.000
toDecimal64(‘whdwjfew’,8)将数值型或者含有非数字的字符串进行精度保留toDecimal64(23.12291, 3)toDecimal64(’_23.12291’, 3)返回 23.122返回 0.000
toDecimal128(‘whdwjfew’,8)将数值型或者含有非数字的字符串进行精度保留toDecimal128(23.12291, 3)toDecimal128(’_23.12291’, 3)返回 23.122返回 0.000

2.2 字符串转化为整数(非整数的字符串返回0)

函数用途举例结果
toUInt8OrZero()将无符号整数字符型转化为整数型,否则返回0toUInt8OrZero(‘123’)toUInt8OrZero(‘123.12’)返回 123返回 0
toInt8OrZero()将整数字符型转化为整数型,否则返回0toInt8OrZero(‘123’)toInt8OrZero(’-123’)返回 123返回 -123
toFloat32OrZero()将数值字符串型转化为数值型,注意:从toFloat32OrZero开始,丢32的没有对应的函数toFloat32OrZero(‘-123’)toFloat32OrZero(‘123.123’)返回 -123返回 123.123

2.3 日期与时间日期转化

函数用途举例结果
toDate()将字符型日期转化为日期型toDate(‘2018-12-24’)返回 2018-12-24
toDateTime()将字符型时间日期转化为时间日期型toDateTime(‘2018-12-24 10:10:00’)返回 2018-12-24 10:10:00

2.4 转化为字符型

函数用途举例结果
toString()将数值型、字符型、日期等转化为字符型toString(‘2018-12-24’)toString(‘123’)返回 2018-12-24返回 123

2.5 查看数据类型

函数用途举例结果
toTypeName()返回数据的类型toTypeName(toString(‘123’))toTypeName(toDate(‘2018-12-24’))返回 String返回 Date

3、字符串操作

3.1 基本字符串操作

函数用途举例结果
empty()判断字符串是空为1,否则为0empty(’’)empty(‘123a’)返回 1返回 0
notEmpty()判断字符串是非空为1,否则为0notEmpty(’’)notEmpty(‘123a’)返回 0返回 1
length()返回字符串的长度length(’’)length(‘123a’)返回 0返回 4
lower()将字符串转为小写lower(‘aBc’)返回 abc
upper()将字符串转为大写upper(‘aBc’)返回 ABC
reverse()将字符串反转reverse(‘abc’)返回 cba
substring(s, offset, length)字符串截取substring(‘123abcABC’, 2, 3)返回 23a
appendTrailingCharIfAbsent(s, c)如果字符串s非空,则将s后追加一个字符c(s最后一个字符与c不同),否则不处理appendTrailingCharIfAbsent(‘123abc’, ‘b’)appendTrailingCharIfAbsent(‘123abc’, ‘c’)返回 123abcb返回 123abc

3.2 字符串查找

函数用途举例结果
match(haystack,pattern)字符串正则匹配,返回0或1match(‘avhsca’,'vh’)返回 1
extract(haystack,pattern)返回匹配到的第一个子串extract(‘iioomAj12123124OOBJB’, ‘\d+’)返回 12123124
extractAll(haystack,pattern)返回匹配到的所有子串,输出列表extractAll(‘iioomAj12123124OOBJ123B’, ‘\d+’)返回 [12123124,123]
like(haystack,pattern)匹配到的数据返回1,否则返回0like(‘avhsca’,’%vh%’)like(‘avhsca’,’%vabjh%’)返回 1返回 0
notLike(haystack, pattern)与like()函数相反notLike(‘avhsca’,’%vh%’)notLike(‘avhsca’,’%vabjh%’)返回 0返回 1

3.3 字符串替换

函数用途举例结果
replaceOne(haystack,pattern,replacement)替换第一个匹配到的patternreplaceOne(‘asd123cbbj464sd’, ‘sd’, ‘-’)返回 a-123cbbj464sd
replaceAll(haystack,pattern,replacement)替换所有匹配到的patternreplaceOne(‘asd123cbbj464sd’, ‘sd’, ‘-’)返回 a-123cbbj464-
replaceRegexpOne(haystack, pattern, replacement)正则匹配替换第一个匹配到的patternreplaceRegexpOne(‘Hello, World!’, ‘o’, '- ')返回 Hell- , World!
replaceRegexpAll(haystack,pattern,replacement)正则匹配替换所有匹配到的patternreplaceRegexpAll(‘Hello, World!’, ‘^’, 'here: ')replaceRegexpAll(‘Hello, World!’, ‘o’, '-- ')返回 here: Hello, World!返回 Hell-- , W-- rld!

3.4 字符串分割

函数用途举例结果
splitByChar(separator, s)以单个字符分割字符串splitByChar(’-’, ‘qw-asaf-asfqw-2312-asd’)返回 [‘qw’,‘asaf’,‘asfqw’,‘2312’,‘asd’]
splitByString(separator, s)以单个或多个字符分割字符串splitByString(’-’, ‘qw-asaf-asfqw-2312-asd’)splitByString(’-a’, ‘qw-asaf-asfqw-2312-asd’)返回 [‘qw’,‘asaf’,‘asfqw’,‘2312’,‘asd’]返回 [‘qw’,‘saf’,‘sfqw-2312’,‘sd’]

3.5 字符串拼接

函数用途举例结果
concat(s1,s2,…)将字符串拼接concat(‘123’, ‘abc’, ‘ABC’)返回 123abcABC

4、条件语句

函数用途举例结果
if(cond,then,else)条件输出if(1 > 2, ‘正确’, ‘错误’)返回 错误
multiIf(cond_1, then_1, cond_2, then_2…else)多条件输出multiIf(1 > 2, ‘正确’, 2 < 0, ‘正确’, ‘错误’)返回 错误

5、数学函数

函数用途举例结果
e()返回e的值e()返回 2.718281828459045
pi()返回pi的值pi()返回 3.141592653589793
exp(x)返回e的x次方exp(1)返回 2.718281828459045
exp2(x)返回2的x次方exp2(2)返回 4
exp10(x)返回10的x次方exp10(1)返回 10
log(x)返回log以e为底的对数值log(e())返回 1
log2(x)返回log以2为底的对数值log2(2)返回 1
log10(x)返回log以10为底的对数值log10(100)返回 2
sqrt(x)对x开平方sqrt(4)返回 2
cbrt(x)对x开立方cbrt(8)返回 2
pow(x, y)返回x的y次方pow(2, 3)返回 8

6、舍入函数

函数用途举例结果
floor(x[, N])向下取数floor(123.883, 1)floor(123.883, -1)返回 123.8返回 120
ceil(x[, N])向上取数ceil(123.883, 1)ceil(123.883, -1)返回 123.9返回 130
round(x[, N])四舍五入round(123.883, 1)round(123.883, -1)返回 123.9返回 120

7、URL操作函数

函数用途举例结果
protocol()返回URL的协议类型protocol(‘http://www.baidu.com.cn’)返回 http
domain()返回URL的域名domain(‘http://www.baidu.com.cn’)返回 www.baidu.com.cn
domainWithoutWWW()返回URL不带www的域名domainWithoutWWW(‘http://www.baidu.com.cn’)返回 baidu.com.cn
topLevelDomain()返回顶级域名topLevelDomain(‘http://www.baidu.com.cn’)返回 cn
firstSignificantSubdomain()Returns the “first significant subdomain”.firstSignificantSubdomain(‘http://www.baidu.com.cn’)返回 baidu
cutToFirstSignificantSubdomain()Returns the part of the domain that includes top-level subdomains up to the “first significant subdomain” (see the explanation above).cutToFirstSignificantSubdomain(‘http://www.baidu.com.cn’)返回 baidu.com.cn
path()返回URL的路径path(‘https://www.baidu.com/s?wd=SQL%E4%B8%AD%E7%9A%84split’)返回 /s
pathFull()返回URL的完整路径pathFull(‘https://www.baidu.com/s?wd=SQL%E4%B8%AD%E7%9A%84split’)返回 /s?wd=SQL%E4%B8%AD%E7%9A%84split
queryString()返回URL的参数(查询字符串)queryString(‘https://www.baidu.com/s?wd=SQL%E4%B8%AD%E7%9A%84split’)返回 wd=SQL%E4%B8%AD%E7%9A%84split
extractURLParameters()以列表的形式返回URL的参数extractURLParameters(‘https://www.baidu.com/s?wd=SQL%E4%B8%AD%E7%9A%84split&ur=qwguq’)返回 [‘wd=SQL%E4%B8%AD%E7%9A%84split’,‘ur=qwguq’]
extractURLParameterNames()以列表的形式返回URL的参数名extractURLParameterNames(‘https://www.baidu.com/s?wd=SQL%E4%B8%AD%E7%9A%84split&ur=qwguq’)返回 [‘wd’,‘ur’]
cutQueryString()返回URL?(参数)前面的内容cutQueryString(‘https://www.baidu.com/s?wd=SQL%E4%B8%AD%E7%9A%84split&ur=qwguq’)返回 https://www.baidu.com/s

8、IP操作函数

函数用途举例结果
IPv4StringToNum(s)将IPV4转为数值,非IPV4的转化为0IPv4StringToNum(‘23.217.198.69’)IPv4StringToNum(‘adwh.124.qwfqw’)返回 400148037返回 0
IPv4NumToString(num)将数值转为IPV4IPv4NumToString(400148037)返回 23.217.198.69
IPv4NumToStringClassC(num)将数值转为IPV4,且最后的段位用xxx代替IPv4NumToStringClassC(400148037)返回 23.217.198.xxx

9、表操作

9.1 表连接操作

函数用途举例结果
INNER JOIN内连接A表 INNER JOIN B表A表与B表的公共部分
LEFT OUTER JOIN左外连接A 表 LEFT OUTER JOIN B表A表不在B表中的部分
RIGHT OUTER JOIN右外连接A 表 RIGHT OUTER JOIN B表B表不在A表中的部分
FULL OUTER JOIN全外连接A 表 FULL OUTER JOIN B表A与B表全部,没有为NULL

9.2 LIMIT操作

函数用途举例结果
LIMIT N查询N条数据,一般跟ORDER BY 连用ORDER BY hit DESC LIMIT 10按照hit列降排取前10
LIMIT N BY Clause按照Clause列查询N条数据,一般跟ORDER BY 连用SELECT date, domain, count(1) AS hit from db.tb where…GROUP BY date, domain,ORDER BY hit DESCLIMIT 10 BY date取每天TOP10的域名

10、字典操作

函数用途举例结果
dictGetString()字典映射dictGetString(‘ck_abc_dic’, ‘ck_value’, tuple(_abc))

坐标函数

-- 获取指定坐标,指定距离的数据
select company_name
from company_info
where greatCircleDistance(lng_gd, lat_gd, 120.594712,31.321582) < 5000;


-- 获取指定区域内的数据
select company_name
from company_info
where pointInPolygon(coordinate,
                     [(116.195864, 39.928876), (116.230359, 39.928876),
                         (116.230359, 39.956756), (116.195864, 39.956756)]) > 0;

在这里插入图片描述
在这里插入图片描述

NULL值处理函数

  • isNull() → 检查参数是否为NULL ,为NULL返回1,否则0
  • isNotNull() → 检查参数是否不为 NULL,为NULL返回0,否则1
  • ifNull() → 如果第一个参数为NULL,则返回第二个参数的值。
  • NullIf() → 如果参数相等,返回NULL,参数不相等,返回第一个值
SELECT 
    isNull('1'),isNull(NULL),
    isNotNull('1'),isNotNull(NULL),
    ifNull(NULL, 2),ifNull(1, 2),
    NullIf(1, 1),NullIf(1, 0)

┌─isNull('1')─┬─isNull(NULL)─┬─isNotNull('1')─┬─isNotNull(NULL)─┬─ifNull(NULL, 2)─┬─ifNull(1, 2)─┬─NullIf(1, 1)─┬─NullIf(1, 0)─┐
│           011021 │         ᴺᵁᴸᴸ │            1 │
└─────────────┴──────────────┴────────────────┴─────────────────┴─────────────────┴──────────────┴──────────────┴──────────────┘
你可以使用 clickhouse-driver 库来将 DataFrame 数据传输到 ClickHouse 库中。具体步骤如下: 1. 首先,安装 clickhouse-driver 库。在终端输入以下命令: ``` pip install clickhouse-driver ``` 2. 在 Python 中导入 clickhouse-driver 库: ``` import clickhouse_driver ``` 3. 创建 ClickHouse 客户端对象,连接到 ClickHouse 服务器: ``` client = clickhouse_driver.Client('localhost') ``` 这里的 localhost 是 ClickHouse 服务器的地址,如果不在本机上,需要填写相应的 IP 地址。 4. 创建数据表。可以使用普通的 SQL 语句来创建数据表,例如: ``` client.execute('CREATE TABLE test (id Int32, name String) ENGINE = Memory') ``` 5. 将 DataFrame 转换为 ClickHouse 中的数据格式。clickhouse-driver 库提供了一个将 DataFrame 转换为 ClickHouse 格式的函数,例如: ``` data = [(1, 'Alice'), (2, 'Bob'), (3, 'Charlie')] columns = ['id', 'name'] df = pd.DataFrame(data, columns=columns) prepared_data = client.prepare_insert('test', df.columns) prepared_data.executemany(df.values) ``` 这里的 df 是一个 Pandas 的 DataFrame,data 是该 DataFrame 中的数据。使用 client.prepare_insert 函数,将 DataFrame 的列名传递给 ClickHouse。然后,使用 prepared_data.executemany 函数,将 DataFrame 中的数据插入到 ClickHouse 表中。 6. 查询数据。可以使用普通的 SQL 语句来查询数据,例如: ``` data = client.execute('SELECT * FROM test') ``` 这里的 data 是一个包含查询结果的列表。 这样,就可以在 Python 中将 DataFrame 数据传输到 ClickHouse 库中了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值