跳转到主内容
跳转到主内容

用于处理 URL 的函数

概述

注意

本节中提到的函数为了获得尽可能高的性能而进行了优化,因此在大多数情况下不遵循 RFC 3986 标准。 实现 RFC 3986 的函数会在函数名后附加 RFC,通常性能会较低。

在处理不包含用户信息字符串或 @ 符号的公共注册域名时,一般可以使用非 RFC 版本的函数。 下表详细说明了 URL 中的哪些符号可以()或不可以()被对应的 RFC 和非 RFC 版本解析:

Symbolnon-RFCRFC
' '
\t
<
>
%✔*
{
}
\
^
~✔*
[
]
;✔*
=✔*
&✔*

带有 * 标记的符号是 RFC 3986 中的子分隔符,并且在 @ 符号之后的用户信息中是允许的。

URL 函数分为两类:

  • 提取 URL 某一部分的函数。若 URL 中不存在相关部分,则返回空字符串。
  • 移除 URL 某一部分的函数。若 URL 中不存在类似部分,则 URL 保持不变。
注意

下面列出的函数是从 system.functions 系统表中生成的。

URLHierarchy

引入于:v1.1

返回一个数组,包含在路径和查询字符串中以符号 /?# 为结尾截断得到的 URL 片段。连续的分隔符字符被视为一个。结果的第一个元素为仅包含协议和主机名的 URL,此后各元素为路径逐步延长所形成的层级结构。

语法

URLHierarchy(url)

参数

  • url — 要处理的 URL。String

返回值

返回一个数组,包含依次递增长度、构成层级结构的 URL。Array(String)

示例

基本用法

SELECT URLHierarchy('https://example.com/a/b?c=1')
['https://example.com/','https://example.com/a/','https://example.com/a/b','https://example.com/a/b?c=1']

URLPathHierarchy

引入版本:v1.1

返回一个数组,其中包含 URL 的路径部分,在遇到符号 /?# 时进行截断。与 URLHierarchy 不同,结果不包含协议和主机——它从路径开始。连续的分隔符字符会被视作一个分隔符。

语法

URLPathHierarchy(url)

参数

  • url — 要处理的 URL。String

返回值

返回一个由逐渐变长的 URL 路径段组成的数组,构成一个层级结构。Array(String)

示例

基本用法

SELECT URLPathHierarchy('https://example.com/a/b?c=1')
['/a/','/a/b','/a/b?c=1']

cutFragment

引入版本:v1.1

从 URL 中移除片段标识符(包括 # 号)。

语法

cutFragment(url)

参数

返回值

返回去除片段标识符的 URL。String

示例

用法示例

SELECT cutFragment('http://example.com/path?query=value#fragment123');
┌─cutFragment('http://example.com/path?query=value#fragment123')─┐
│ http://example.com/path?query=value                            │
└────────────────────────────────────────────────────────────────┘

cutQueryString

自 v1.1 版本引入

从 URL 中移除查询字符串(query string),包括问号本身。

语法

cutQueryString(url)

参数

返回值

返回移除了查询字符串的 URL。String

示例

用法示例

SELECT cutQueryString('http://example.com/path?query=value&param=123#fragment');
┌─cutQueryString('http://example.com/path?query=value&param=123#fragment')─┐
│ http://example.com/path#fragment                                         │
└──────────────────────────────────────────────────────────────────────────┘

cutQueryStringAndFragment

自 v1.1 引入

从 URL 中移除查询字符串和片段标识符,包括问号(?)和井号(#)。

语法

cutQueryStringAndFragment(url)

参数

返回值

返回去除查询字符串和片段标识符后的 URL。String

示例

用法示例

SELECT cutQueryStringAndFragment('http://example.com/path?query=value&param=123#fragment');
┌─cutQueryStringAndFragment('http://example.com/path?query=value&param=123#fragment')─┐
│ http://example.com/path                                                             │
└─────────────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomain

引入版本:v1.1

返回域名中,包含从顶级子域到第一个显著子域这一部分的内容。

语法

cutToFirstSignificantSubdomain(url)

参数

  • url — 要处理的 URL 或域名字符串。String

返回值

返回域名中,从顶级后缀起向左直至第一个重要子域名(含)为止的那一部分;如果无法确定,则返回空字符串。String

示例

用法示例

SELECT
    cutToFirstSignificantSubdomain('https://news.clickhouse.com.tr/'),
    cutToFirstSignificantSubdomain('www.tr'),
    cutToFirstSignificantSubdomain('tr');
┌─cutToFirstSignificantSubdomain('https://news.clickhouse.com.tr/')─┬─cutToFirstSignificantSubdomain('www.tr')─┬─cutToFirstSignificantSubdomain('tr')─┐
│ clickhouse.com.tr                                                 │ tr                                       │                                      │
└───────────────────────────────────────────────────────────────────┴──────────────────────────────────────────┴──────────────────────────────────────┘

cutToFirstSignificantSubdomainCustom

引入版本:v21.1

返回域名中从顶级域开始一直到第一个重要子域(包含在内)的部分。接受自定义的 TLD 列表 名称。如果需要最新的 TLD 列表或使用自定义列表时,该函数会很有用。

配置示例

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>

语法

cutToFirstSignificantSubdomainCustom(url, tld_list_name)

参数

  • url — 要处理的 URL 或域名字符串。String
  • tld_list_name — 在 ClickHouse 中配置的自定义 TLD 列表名称。const String

返回值

返回域名的一部分,该部分包含自顶级域名起直到第一个重要子域的所有子域。String

示例

为非标准域名使用自定义 TLD 列表

SELECT cutToFirstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list')
foo.there-is-no-such-domain

cutToFirstSignificantSubdomainCustomRFC

引入版本:v22.10

返回域名中从顶级子域开始直至第一个重要子域的那一部分。 接受自定义的 TLD 列表名称。 当需要一份最新的 TLD 列表或使用自定义列表时,此函数会很有用。 类似于 cutToFirstSignificantSubdomainCustom,但符合 RFC 3986。

配置示例

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>

语法

cutToFirstSignificantSubdomainCustomRFC(url, tld_list_name)

参数

  • url — 需要根据 RFC 3986 进行处理的 URL 或域名字符串。- tld_list_name — 在 ClickHouse 中配置的自定义 TLD 列表名称。

返回值

返回域名中从顶级子域到第一个重要子域(含)这一部分。String

示例

用法示例

SELECT cutToFirstSignificantSubdomainCustomRFC('www.foo', 'public_suffix_list');
┌─cutToFirstSignificantSubdomainCustomRFC('www.foo', 'public_suffix_list')─────┐
│ www.foo                                                                      │
└──────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomainCustomWithWWW

引入版本:v21.1

返回域名中从顶级子域开始,一直到第一个重要子域之间的部分,但不会去除前缀 'www'。接受自定义的 TLD 列表名称。如果需要使用最新的 TLD 列表或自定义列表,可以使用此函数。

配置示例

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>
    

**Syntax**

```sql
cutToFirstSignificantSubdomainCustomWithWWW(url, tld_list_name)

参数

  • url — 要处理的 URL 或域名字符串。- tld_list_name — 在 ClickHouse 中配置的自定义 TLD 列表名称。

返回值

包含从最顶层子域名到第一个有意义子域名的域名部分,且不会去除 'www'。String

示例

使用示例

SELECT cutToFirstSignificantSubdomainCustomWithWWW('www.foo', 'public_suffix_list');
┌─cutToFirstSignificantSubdomainCustomWithWWW('www.foo', 'public_suffix_list')─┐
│ www.foo                                                                      │
└──────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomainCustomWithWWWRFC

引入于:v22.10

返回域名中包含顶级域及其子域、直到第一个有意义子域的部分,但不会去除 www。 接受自定义 TLD 列表的名称。 当需要更新的 TLD 列表或使用自定义列表时,这会非常有用。 类似于 cutToFirstSignificantSubdomainCustomWithWWW,但符合 RFC 3986

配置示例

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>
    

**Syntax**

```sql
cutToFirstSignificantSubdomainCustomWithWWWRFC(url, tld_list_name)

参数

  • url — 待根据 RFC 3986 解析的 URL 或域名字符串。- tld_list_name — 在 ClickHouse 中配置的自定义 TLD 列表名称。

返回值

返回包含顶级子域名直到第一个重要子域名的域名部分,并且保留 wwwString

示例

使用自定义 TLD 列表的 RFC 3986 解析(保留 www)

SELECT cutToFirstSignificantSubdomainCustomWithWWWRFC('https://www.subdomain.example.custom', 'public_suffix_list')
www.example.custom

cutToFirstSignificantSubdomainRFC

引入于:v22.10

返回域名中自顶级子域起,直至“first significant subdomain”(第一个重要子域)之间的部分。与 cutToFirstSignificantSubdomain 类似,但符合 RFC 3986

语法

cutToFirstSignificantSubdomainRFC(url)

参数

  • url — 按照 RFC 3986 进行解析的 URL 或域名字符串。String

返回值

返回域名中从顶级子域到第一个重要子域的这部分(如果可能),否则返回空字符串。String

示例

使用示例

SELECT
    cutToFirstSignificantSubdomain('http://user:[email protected]:8080'),
    cutToFirstSignificantSubdomainRFC('http://user:[email protected]:8080');
┌─cutToFirstSignificantSubdomain('http://user:[email protected]:8080')─┬─cutToFirstSignificantSubdomainRFC('http://user:[email protected]:8080')─┐
│                                                                         │ example.com                                                                │
└─────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomainWithWWW

引入于:v20.12

返回域名中从顶级子域到“第一个有意义子域名”的部分,并且不去除 'www.'。

类似于 cutToFirstSignificantSubdomain,但如果存在则保留 'www.' 前缀。

语法

cutToFirstSignificantSubdomainWithWWW(url)

参数

  • url — 要处理的 URL 或域名字符串。String

返回值

返回域名中从顶级子域开始直到第一个重要子域(含 www)这一部分;若无法确定,则返回空字符串。String

示例

用法示例

SELECT
    cutToFirstSignificantSubdomainWithWWW('https://news.clickhouse.com.tr/'),
    cutToFirstSignificantSubdomainWithWWW('www.tr'),
    cutToFirstSignificantSubdomainWithWWW('tr');
┌─cutToFirstSignificantSubdomainWithWWW('https://news.clickhouse.com.tr/')─┬─cutToFirstSignificantSubdomainWithWWW('www.tr')─┬─cutToFirstSignificantSubdomainWithWWW('tr')─┐
│ clickhouse.com.tr                                                        │ www.tr                                          │                                             │
└──────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────┴─────────────────────────────────────────────┘

cutToFirstSignificantSubdomainWithWWWRFC

引入版本:v22.10

返回域名中,从顶级子域开始直到“第一个重要子域”的那一部分,但不会去除 'www'。类似于 cutToFirstSignificantSubdomainWithWWW,但符合 RFC 3986

语法

cutToFirstSignificantSubdomainWithWWWRFC(url)

参数

  • url — 需要根据 RFC 3986 进行处理的 URL 或域名字符串。

返回值

返回域名中从顶级子域名开始(在可能的情况下包含 'www'),一直到第一个重要子域名为止的部分;否则返回空字符串 String

示例

使用示例

SELECT
    cutToFirstSignificantSubdomainWithWWW('http:%2F%[email protected]/economicheskiy'),
    cutToFirstSignificantSubdomainWithWWWRFC('http:%2F%[email protected]/economicheskiy');
┌─cutToFirstSignificantSubdomainWithWWW('http:%2F%[email protected]/economicheskiy')─┬─cutToFirstSignificantSubdomainWithWWWRFC('http:%2F%[email protected]/economicheskiy')─┐
│                                                                                       │ mail.ru                                                                                  │
└───────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────┘

cutURLParameter

自 v1.1 起引入

从 URL 中移除名为 name 的参数(如果存在)。 此函数不会对参数名中的字符进行编码或解码,例如 Client IDClient%20ID 会被视为不同的参数名。

语法

cutURLParameter(url, name)

参数

返回值

移除了名为 name 的 URL 参数的 URL。String

示例

使用示例

SELECT
    cutURLParameter('http://bigmir.net/?a=b&c=d&e=f#g', 'a') AS url_without_a,
    cutURLParameter('http://bigmir.net/?a=b&c=d&e=f#g', ['c', 'e']) AS url_without_c_and_e;
┌─url_without_a────────────────┬─url_without_c_and_e──────┐
│ http://bigmir.net/?c=d&e=f#g │ http://bigmir.net/?a=b#g │
└──────────────────────────────┴──────────────────────────┘

cutWWW

引入于:v1.1

从 URL 的域名中移除开头的 www.(如果存在)。

语法

cutWWW(url)

参数

返回值

返回去掉域名开头 www. 的 URL。String

示例

用法示例

SELECT cutWWW('http://www.example.com/path?query=value#fragment');
┌─cutWWW('http://www.example.com/path?query=value#fragment')─┐
│ http://example.com/path?query=value#fragment               │
└────────────────────────────────────────────────────────────┘

decodeURLComponent

自 v1.1 引入

接受一个经过 URL 编码的字符串作为输入,并将其解码为原始的可读形式。

语法

decodeURLComponent(url)

参数

返回值

返回解码后的 URL。String

示例

使用示例

SELECT decodeURLComponent('http://127.0.0.1:8123/?query=SELECT%201%3B') AS DecodedURL;
┌─DecodedURL─────────────────────────────┐
│ http://127.0.0.1:8123/?query=SELECT 1; │
└────────────────────────────────────────┘

decodeURLFormComponent

引入于:v1.1

使用表单编码规则(RFC-1866)对 URL 编码的字符串进行解码,其中将 + 号还原为空格,并解码百分号编码的字符。

语法

decodeURLFormComponent(url)

参数

返回值

返回解码后的 URL。String

示例

使用示例

SELECT decodeURLFormComponent('http://127.0.0.1:8123/?query=SELECT%201+2%2B3') AS DecodedURL;
┌─DecodedURL────────────────────────────────┐
│ http://127.0.0.1:8123/?query=SELECT 1 2+3 │
└───────────────────────────────────────────┘

domain

自 v1.1 起引入

从 URL 中提取主机名。

URL 可以在包含或不包含协议的情况下指定。

语法

domain(url)

参数

返回值

如果输入字符串能够解析为 URL,则返回主机名,否则返回空字符串。String

示例

使用示例

SELECT domain('svn+ssh://some.svn-hosting.com:80/repo/trunk');
┌─domain('svn+ssh://some.svn-hosting.com:80/repo/trunk')─┐
│ some.svn-hosting.com                                   │
└────────────────────────────────────────────────────────┘

domainRFC

自 v22.10 起引入

从 URL 中提取主机名。 类似于 domain,但符合 RFC 3986 标准。

语法

domainRFC(url)

参数

返回值

如果输入字符串可以解析为 URL,则返回主机名,否则返回空字符串。String

示例

使用示例

SELECT
    domain('http://user:[email protected]:8080/path?query=value#fragment'),
    domainRFC('http://user:[email protected]:8080/path?query=value#fragment');
┌─domain('http://user:[email protected]:8080/path?query=value#fragment')─┬─domainRFC('http://user:[email protected]:8080/path?query=value#fragment')─┐
│                                                                           │ example.com                                                                  │
└───────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────┘

domainWithoutWWW

引入版本:v1.1

返回 URL 的域名;如果存在前缀 www.,则将其去除。

语法

domainWithoutWWW(url)

参数

返回值

如果输入字符串可以解析为 URL,则返回域名(不包含前缀 www.),否则返回空字符串。String

示例

使用示例

SELECT domainWithoutWWW('http://[email protected]:80/');
┌─domainWithoutWWW('http://[email protected]:80/')─┐
│ example.com                                         │
└─────────────────────────────────────────────────────┘

domainWithoutWWWRFC

自 v1.1 引入

返回去掉前缀 www. 的域名(如果存在)。类似于 domainWithoutWWW,但遵循 RFC 3986

语法

domainWithoutWWWRFC(url)

参数

返回值

如果输入字符串可以解析为 URL,则返回域名(不带前缀 www.),否则返回空字符串。String

示例

用法示例

SELECT
    domainWithoutWWW('http://user:[email protected]:8080/path?query=value#fragment'),
    domainWithoutWWWRFC('http://user:[email protected]:8080/path?query=value#fragment');
┌─domainWithoutWWW('http://user:[email protected]:8080/path?query=value#fragment')─┬─domainWithoutWWWRFC('http://user:[email protected]:8080/path?query=value#fragment')─┐
│                                                                                         │ example.com                                                                                │
└─────────────────────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────────────┘

encodeURLComponent

自 v22.3 起提供

接收一个普通字符串,并将其转换为 URL 编码(百分号编码)格式,其中特殊字符会被替换为相应的百分号编码序列。

语法

encodeURLComponent(url)

参数

返回值

返回编码后的 URL。String

示例

用法示例

SELECT encodeURLComponent('http://127.0.0.1:8123/?query=SELECT 1;') AS EncodedURL;
┌─EncodedURL───────────────────────────────────────────────┐
│ http%3A%2F%2F127.0.0.1%3A8123%2F%3Fquery%3DSELECT%201%3B │
└──────────────────────────────────────────────────────────┘

encodeURLFormComponent

自 v22.3 起引入

使用表单编码规则(RFC-1866)对字符串进行编码,其中空格会被转换为 + 号,特殊字符会被进行百分号编码。

语法

encodeURLFormComponent(url)

参数

返回值

返回编码后的 URL。String

示例

用法示例

SELECT encodeURLFormComponent('http://127.0.0.1:8123/?query=SELECT 1 2+3') AS EncodedURL;
┌─EncodedURL────────────────────────────────────────────────┐
│ http%3A%2F%2F127.0.0.1%3A8123%2F%3Fquery%3DSELECT+1+2%2B3 │
└───────────────────────────────────────────────────────────┘

extractURLParameter

引入版本:v1.1

返回 URL 中名为 name 的参数值(如果存在),否则返回空字符串。 如果存在多个同名参数,则返回首次出现的值。 该函数假定 url 参数中的参数采用与参数 name 相同的编码方式。

语法

extractURLParameter(url, name)

参数

返回值

返回指定名称的 URL 参数值。String

示例

使用示例

SELECT extractURLParameter('http://example.com/?param1=value1&param2=value2', 'param1');
┌─extractURLPa⋯, 'param1')─┐
│ value1                   │
└──────────────────────────┘

extractURLParameterNames

自 v1.1 版本引入

返回一个字符串数组,其中的字符串对应 URL 参数的名称。 返回值不会被解码。

语法

extractURLParameterNames(url)

参数

返回值

返回一个字符串数组,包含 URL 参数的名称。Array(String)

示例

用法示例

SELECT extractURLParameterNames('http://example.com/?param1=value1&param2=value2');
┌─extractURLPa⋯m2=value2')─┐
│ ['param1','param2']      │
└──────────────────────────┘

extractURLParameters

自 v1.1 引入

返回一个由 name=value 字符串组成的数组,对应 URL 参数。 这些值不会被解码。

语法

extractURLParameters(url)

参数

返回值

返回一个字符串数组,元素为与 URL 参数对应的 name=value 字符串。Array(String)

示例

用法示例

SELECT extractURLParameters('http://example.com/?param1=value1&param2=value2');
┌─extractURLParame⋯&param2=value2')─┐
│ ['param1=value1','param2=value2'] │
└───────────────────────────────────┘

firstSignificantSubdomain

引入自:v1.1

返回“首个重要子域名”。

如果二级域名是 'com'、'net'、'org' 或 'co',则首个重要子域名就是该二级域名。 否则,首个重要子域名就是三级域名。

例如,firstSignificantSubdomain('https://news.clickhouse.com/&#39;) = 'clickhouse',firstSignificantSubdomain ('https://news.clickhouse.com.tr/&#39;) = 'clickhouse'。

“无关紧要”的二级域名列表和其他实现细节未来可能会发生变化。

语法

firstSignificantSubdomain(url)

参数

  • 无。

返回值

示例

firstSignificantSubdomain

SELECT firstSignificantSubdomain('https://news.clickhouse.com/')

firstSignificantSubdomainCustom

引入版本:v21.1

使用自定义 TLD(顶级域名)列表,返回 URL 的第一个重要子域名。自定义 TLD 列表名称对应于一个配置,该配置定义哪些域名后缀应被视为顶级域名。这对于非标准的 TLD 层级结构非常有用。该函数使用简化的 URL 解析算法,并假定 URL 中的协议及其后的所有内容已被去掉。

语法

firstSignificantSubdomainCustom(url, tld_list_name)

参数

  • url — 要从中提取子域名的 URL。String
  • tld_list_name — 配置中自定义 TLD 列表的名称。String

返回值

返回第一个有效子域名。String

示例

基本用法

SELECT firstSignificantSubdomainCustom('https://news.example.com', 'public_suffix_list')
example

firstSignificantSubdomainCustomRFC

引入于 v22.10

firstSignificantSubdomainCustom 类似,但使用符合 RFC 3986 的 URL 解析方式,而非简化算法。

语法

firstSignificantSubdomainCustomRFC(url, tld_list_name)

参数

  • url — 要从中提取子域名的 URL。String
  • tld_list_name — 配置中自定义的 TLD 列表名称。String

返回值

返回第一个有意义的子域名。String

示例

基本用法

SELECT firstSignificantSubdomainCustomRFC('https://news.example.com', 'public_suffix_list')
example

firstSignificantSubdomainRFC

引入于:v22.10

返回根据 RFC 1034 定义的“首个有效子域名”。

语法

firstSignificantSubdomainRFC(url)

参数

  • 无。

返回值

示例

fragment

引入版本:v1.1

返回不包含开头井号符号的片段标识符。

语法

fragment(url)

参数

返回值

返回不包含开头井号(#)的 fragment 标识符。String

示例

用法示例

SELECT fragment('https://clickhouse.com/docs/getting-started/quick-start/cloud#1-create-a-clickhouse-service');
┌─fragment('http⋯ouse-service')─┐
│ 1-create-a-clickhouse-service │
└───────────────────────────────┘

netloc

引入版本:v20.5

从 URL 中提取网络位置部分(username:password@host:port)。

语法

netloc(url)

参数

返回值

从给定的 URL 中提取并返回 username:password@host:portString

示例

使用示例

SELECT netloc('http://[email protected]:80/');
┌─netloc('http⋯e.com:80/')─┐
│ [email protected]:80  │
└──────────────────────────┘

path

自 v1.1 起引入

返回 URL 中去除查询字符串后的路径部分。

语法

path(url)

参数

返回值

返回去掉查询字符串后的 URL 路径部分。String

示例

用法示例

SELECT path('https://clickhouse.com/docs/sql-reference/functions/url-functions/?query=value');
┌─path('https://clickhouse.com/en/sql-reference/functions/url-functions/?query=value')─┐
│ /docs/sql-reference/functions/url-functions/                                         │
└──────────────────────────────────────────────────────────────────────────────────────┘

pathFull

引入版本:v1.1

path 相同,但还包含 URL 的查询字符串和片段。

语法

pathFull(url)

参数

返回值

返回 URL 的路径,包括查询字符串和片段(fragment)。String

示例

使用示例

SELECT pathFull('https://clickhouse.com/docs/sql-reference/functions/url-functions/?query=value#section');
┌─pathFull('https://clickhouse.com⋯unctions/?query=value#section')─┐
│ /docs/sql-reference/functions/url-functions/?query=value#section │
└──────────────────────────────────────────────────────────────────┘

port

自 v20.5 起提供

返回 URL 的端口号;如果 URL 不包含端口或无法解析,则返回 default_port

语法

port(url[, default_port])

参数

  • url — URL。String
  • default_port — 可选。要返回的默认端口号。默认值为 0UInt16

返回值

返回 URL 中的端口;如果 URL 中未指定端口,或发生验证错误,则返回默认端口。UInt16

示例

使用示例

SELECT port('https://clickhouse.com:8443/docs'), port('https://clickhouse.com/docs', 443);
┌─port('https://clickhouse.com:8443/docs')─┬─port('https://clickhouse.com/docs', 443)─┐
│                                     8443 │                                      443 │
└──────────────────────────────────────────┴──────────────────────────────────────────┘

portRFC

引入版本:v22.10

返回端口;如果 URL 不包含端口或无法解析,则返回 default_port。 与 port 类似,但符合 RFC 3986

语法

portRFC(url[, default_port])

参数

  • url — URL。String
  • default_port — 可选。要返回的默认端口号。默认值为 0UInt16

返回值

返回端口号;如果 URL 中未包含端口,或发生校验错误,则返回默认端口号。UInt16

示例

用法示例

SELECT port('http://user:[email protected]:8080/'), portRFC('http://user:[email protected]:8080/');
┌─port('http:/⋯com:8080/')─┬─portRFC('htt⋯com:8080/')─┐
│                        0 │                     8080 │
└──────────────────────────┴──────────────────────────┘

protocol

自 v1.1 起引入

从 URL 中提取协议。

典型返回值示例:http、https、ftp、mailto、tel、magnet。

语法

protocol(url)

参数

返回值

返回 URL 的协议,若无法确定则返回空字符串。String

示例

用法示例

SELECT protocol('https://clickhouse.com/');
┌─protocol('https://clickhouse.com/')─┐
│ https                               │
└─────────────────────────────────────┘

queryString

自 v1.1 起提供

返回 URL 中的查询字符串,不包括开头的问号,以及 # 及其之后的所有内容。

语法

queryString(url)

参数

返回值

返回 URL 的查询字符串(不包括开头的问号和片段部分)。String

示例

使用示例

SELECT queryString('https://clickhouse.com/docs?query=value&param=123#section');
┌─queryString(⋯3#section')─┐
│ query=value&param=123    │
└──────────────────────────┘

queryStringAndFragment

首次引入:v1.1

返回 URL 的查询字符串和片段标识符。

语法

queryStringAndFragment(url)

参数

返回值

返回 URL 的查询字符串和片段标识符。String

示例

使用示例

SELECT queryStringAndFragment('https://clickhouse.com/docs?query=value&param=123#section');
┌─queryStringAnd⋯=123#section')─┐
│ query=value&param=123#section │
└───────────────────────────────┘

topLevelDomain

自 v1.1 引入

从 URL 中提取顶级域名。

注意

URL 可以包含协议或不包含协议。 例如:

svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.com/time/

语法

topLevelDomain(url)

参数

返回值

如果输入字符串能被解析为 URL,则返回域名;否则返回空字符串。String

示例

使用示例

SELECT topLevelDomain('svn+ssh://www.some.svn-hosting.com:80/repo/trunk');
┌─topLevelDomain('svn+ssh://www.some.svn-hosting.com:80/repo/trunk')─┐
│ com                                                                │
└────────────────────────────────────────────────────────────────────┘

topLevelDomainRFC

自 v22.10 起引入

从 URL 中提取顶级域名。 类似于 topLevelDomain,但符合 RFC 3986 规范。

语法

topLevelDomainRFC(url)

参数

返回值

如果输入字符串可以解析为 URL,则返回其域名,否则返回空字符串。String

示例

使用示例

SELECT topLevelDomain('http://foo:foo%[email protected]'), topLevelDomainRFC('http://foo:foo%[email protected]');
┌─topLevelDomain('http://foo:foo%[email protected]')─┬─topLevelDomainRFC('http://foo:foo%[email protected]')─┐
│                                                │ com                                               │
└────────────────────────────────────────────────┴───────────────────────────────────────────────────┘