这篇说说字符串函数的基本语法和示例。
一、函数说明
| 函数名称 | 说明 | 示例 |
| chr(number) | 返回与输入参数指定的 ASCII 码位值匹配的字符。返回结果为 varchar 类型。 | 返回第77位 ASCII 码中匹配的字符值。 * | SELECT chr(77) |
| codepoint(string) | 把 ASCII 码形式的字段值转换为 BigInt 类型。返回结果为 integer 类型。 | 将 ASCII 码中的字符值转换为对应的位置。 * | SELECT codepoint('M') |
| concat(key1, …, keyN) | 连接字符串 key1,key2,…keyN,与||连接符连接效果一致。返回结果为 varchar 类型。注意:任意字符串为 null 时,返回结果为 null,如需跳过 null,可使用 concat_ws。 | 将多个字符串拼接成一个字符串:* | SELECT concat(remote_addr, host, time_local)将两个字符串拼接为一个字符串,并换行:* | SELECT concat('第一行', chr(10), '第二行')说明:其中chr(10)使用 ASCII 码代表换行符。 |
| concat_ws(split_string,key0, …, keyN) | 以 split_string 作为分隔符连接字符串 key1,key2,…keyN。split_string可以为字符串或变量,如果 split_string 为 null,则结果为 null,且会跳过 key1,key2,…keyN 中的 null 值。返回结果为 varchar 类型。 | 以/作为分隔符进行字符串之间的连接。* | SELECT concat_ws('/', remote_addr,host,time_local) |
| concat_ws(split_string, array(varchar)) | 以 split_string 作为分隔符将数组内的元素连接为一个字符串。如果 split_string 为 null,则结果为 null,且会跳过数组中的 null 值。返回结果为 varchar 类型。 注意:此函数中 array(varchar)参数为一个数组,不是字符串。 |
以#作为分隔符进行字符串之间的连接,此例中 split 函数的输出为array。* | select concat_ws('#',split('cloud.tencent.com/product/cls', '/')) |
| format(format,args…) | 使用格式 format 对参数 args 进行格式化输出。返回结果为 varchar 类型。 | 使用 format 格式 ‘IP 地址: %s, 域名: %s’ 对参数 remote_addr 和参数 host 进行格式化输出。 * | SELECT format('IP地址: %s, 域名: %s', remote_addr, host) |
| hamming_distance(key1, key2) | 返回 key1 字符串与 key2 字符串的汉明距离。注意两个字符串长度必须一致。返回结果为 bigint 类型。 | 返回 remote_addr 字符串与 remote_addr 字符串的汉明距离。 * | SELECT hamming_distance(remote_addr, remote_addr) |
| length(key) | 计算字符串的长度。返回结果为 bigint 类型。 | 返回 http_user_agent 的字符串长度。 * | SELECT length(http_user_agent) |
| levenshtein_distance(key1, key2) | 返回字符串 key1 和 key2 的编辑距离。返回结果为 bigint 类型。 | 返回 remote_addr 字符串和 http_protocol 字符串的编辑距离。 * | SELECT levenshtein_distance(remote_addr, http_protocol) |
| lower(key) | 将字符串转换为小写形式。 返回结果为 varchar 类型,小写形式。 | 将 http_protocol 字符串转换为小写形式。 * | SELECT lower(http_protocol) |
| lpad(key, size, padstring) | 使用 padstring 对字符串进行向左补全到 size 大小。如果 size 小于 key 的长度,则对字符串进行截断到 size 大小。size 必须为非负数,padstring 必须非空。返回结果为 varchar 类型。 | 使用 ‘0’ 对 remote_addr 字符串进行向左补全至32字符。 * | SELECT lpad(remote_addr, 32, '0') |
| ltrim(key) | 移除字符串左侧的空白字符。返回结果为 varchar 类型。 | 移除 http_user_agent 字符串左侧的空白字符。 * | SELECT ltrim(http_user_agent) |
| position(substring IN key) | 返回 substring 在字符串中的位置,从1开始。如果没找到,则返回0。该函数有特殊语法 IN 作为参数。另外参考 strpos()。返回结果为 bigint 类型。 | 返回 'G' 字符在 http_method 中的位置。 * | select position('G' IN http_method) |
| replace(key, substring) | 从字符串 key 中移除所有 substring 字符串。返回结果为 varchar 类型。 | 从 time_local 字符串中移除所有 ‘Oct’。 * | select replace(time_local, 'Oct') |
| replace(key, substring, replace) | 替换字符串中所有 substring 为 replace 的字符串。返回结果为 varchar 类型。 | 替换 time_local 字符串中所有 ‘Oct’ 为‘10’。 * | select replace(time_local,'Oct','10') |
| reverse(key) | 翻转字符串 key。返回结果为 varchar 类型。 | 翻转 host 字符串。 * | select reverse(host) |
| rpad(key, size, padstring) | 使用 padstring 对字符串进行向右补全到 size 大小。如果 size 小于 key 的长度,则对字符串进行截断到 size 大小。size 必须为非负数,padstring 必须非空。返回结果为 varchar 类型。 | 使用 ‘0’ 对 remote_addr 字符串进行向右补全到32位大小字符。 * | select rpad(remote_addr, 32, '0') |
| rtrim(key) | 移除字符串右侧所有空白字符。返回结果为 varchar 类型。 | 移除 http_user_agent 字符串右侧所有空白字符。 * | select rtrim(http_user_agent) |
| split(key, delimiter) | 使用指定的分隔符对字符串进行分割,返回字符串数组。 | 使用指定的 ‘/’ 分隔符对 http_user_agent 字符串进行分割,返回字符串数组。 * | SELECT split(http_user_agent, '/') |
| split(key, delimiter, limit) | 使用指定的分隔符对字符串进行分割,返回字符串数组,数组长度最多为 limit。数组中最后一个元素始终为 key 中剩余的所有部分。limit 必须为正数。 | 使用指定的 ‘/’ 分隔符对 http_user_agent 字符串进行分割,返回数组长度为10的字符串数组。 * | SELECT split(http_user_agent, '/', 10) |
| split_part(key, delimiter, index) | 使用指定的分隔符对字符串进行分割,返回数组中 index 位置的字符串。index 从1开始。如果 index 大于数组长度,则返回 null。返回结果为 varchar 类型。 | 使用指定的 ‘/’ 分隔符对 http_user_agent 字符串进行分割,返回第1位的字符串。 * | SELECT split_part(http_user_agent, '/', 1) |
| strpos(key, substring) | 返回 substring 在字符串中的位置,从1开始。如果没找到,则返回0。返回结果为 bigint 类型。 | 返回 ‘org’ 字符串在 host 字符串中的位置。 * | SELECT strpos(host, 'org') |
| strpos(key, substring, instance) | 返回字符串经 substring 分割后的第 instance 个实例在 string 中的位置。如果 instance 是负数,则从尾到头数。位置结果从1开始,如果没找到,则返回0。返回结果为 bigint 类型。 | 返回 host 字符中经过 ‘g’ 分割后的第1个实例在 host 字符串中的位置。 * | SELECT strpos(host, 'g', 1) |
| substr(key, start) | 返回字符串中从 start 位置开始的剩余所有字符,位置从1开始算。如果 start 为负数,则是相对于尾部开始算,例如:[…]返回结果为 varchar 类型。 | 返回 remote_user 字符串中从第2位开始剩余字符。 * | SELECT substr(remote_user, 2) |
| substr(key, start, length) | 返回字符串从 start 开始的最多 length 长度的子串,位置从1开始算。start 为负数,则相对于尾部开始算。返回结果为 varchar 类型。 | 返回 remote_user 字符串中从第2位开始,总长不超过5位的剩余字符。 * | SELECT substr(remote_user, 2, 5) |
| translate(key, from, to) | 将 key 中出现在 from 中的字符全部替换为 to 中对应位置的字符。如果 from 包含重复字符,则只算第一个字符。如果 source 中没出现 from 中的字符,则 source 直接复制。如果 from 长度超过 to 的长度,则对应字符会被删除。返回结果为 varchar 类型。 | 将 remote 字符串中 ‘123’ 的字符替换为 ‘ABC’ 对应的字符。 * | SELECT translate(remote_user, '123', 'ABC') |
| trim(key) | 删除字符串中前后的空白字符。返回结果为 varchar 类型。 | 删除 http_cookies 字符串中前后的空白字符。 * | SELECT trim(http_cookies) |
| upper(key) | 将字符串转化为大写字符。 返回结果为 varchar 类型,大写形式。 | 将 host 字符串中的小写字符转换为大写。 * | SELECT upper(host) |
| word_stem(word) | 返回 word 的英语单词。返回结果为 varchar 类型。 | 返回 Mozilla 的单词。* | SELECT word_stem('Mozilla') |
| word_stem(word, lang) | 返回 word 的 lang 语言词根。返回结果为 varchar 类型。 | 返回 selects 词根的单词。 * | SELECT word_stem('selects', 'en') |
二、Unicode 函数
| 函数名称 | 说明 | 示例 |
| normalize(string) | 转化 string 为 NFC 标准格式。返回结果为 varchar 类型。 | * | SELECT normalize('ĉ') |
| normalize(string, form) | 转化 string 为 form 格式。该函数 form 参数需要使用关键字(NFD,NFC,NFKD,NFKC),而非字符串。返回结果为 varchar 类型。 | * | SELECT normalize('ĉ',NFC) |
| to_utf8(string) | 将 string 转化为 UTF-8 格式的二进制字符串 varbinary。 | * | SELECT to_utf8('test') |
| from_utf8(binary) | 将二进制字符串转为 UTF-8 格式的字符串。非法的 UTF-8 字符会被替换为 U+FFFD。返回结果为 varchar 类型。 | * | SELECT from_utf8(from_base64('SGVsbG8=')) |
| from_utf8(binary, replace) | 将二进制字符串转为 UTF-8 格式的字符串。非法的 UTF-8 字符会被替换为 replace。返回结果为 varchar 类型。 | * | SELECT from_utf8(from_base64('SGVsbG8='),0) |
三、示例
配置 单行-完全正则采集 模式后,自定义的 key 名称如下所示:
body_bytes_sent: 9703http_host: 127.0.0.1http_protocol: HTTP/1.1http_referer: http://127.0.0.1/course/explore?filter%5Btype%5D=all&filter%5Bprice%5D=all&filter%5BcurrentLevelId%5D=all&orderBy=studentNumhttp_user_agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0remote_addr: 10.135.46.111request_length: 782request_method: GETrequest_time: 0.354request_url: /my/course/1status: 200time_local: [05/Oct/2015:21:14:30 +0800]upstream_response_time: 0.354
分析语句示例:
·使用问号(?)拆分 request_url 字段的值并返回第一个字符串(即文件路径部分),然后统计不同路径对应的请求数量。
* | SELECT count(*) AS pv, split_part(request_url, '?', 1) AS Path GROUP BY Path ORDER BY pv DESC LIMIT 3
·提取字段值 http_protocol 中的前4个字符(即HTTP部分),然后统计 HTTP 协议对应的请求数量。
* | SELECT substr(http_protocol,1,4) AS http_protocol, count(*) AS count group by http_protocol
·将 remote_user 字符串中 123 的字符替换为 ABC 对应的字符,返回结果为 varchar 类型。
* | SELECT translate(remote_user, '123', 'ABC')
·从字段值 remote_user 中提取第2位至第5位的值。
* | SELECT substr(remote_user, 2, 5)
·返回字母 H 在 http_protocol 字段值中的位置。
* | SELECT strpos(http_protocol, 'H')
·使用正斜线(/)将 http_protocol 字段的值拆分成2个子串,并返回子串的集合。
* | SELECT split(http_protocol, '/', 2)
·将 time_local 字段值中的 Oct 替换成10。
* | SELECT replace(time_local, 'Oct', '10')