[go: up one dir, main page]

跳到主要内容
跳到主要内容

处理 URL 的函数

备注

本节中提到的函数经过优化以实现最大性能,并且在大多数情况下不遵循 RFC-3986 标准。实现 RFC-3986 的函数名称后会附加 RFC,通常速度较慢。

在处理不包含用户字符串或 @ 符号的公共注册域时,通常可以使用非 RFC 函数变体。下表详细列出了在 URL 中各个符号可以 () 或不能 () 被相应的 RFC 和非 RFC 变体解析的情况:

符号RFCRFC
' '
\t
<
>
%✔*
{
}
|
\\
^
~✔*
[
]
;✔*
=✔*
&✔*

标记为 * 的符号是 RFC 3986 中的子分隔符,允许在 @ 符号后进行用户信息。

提取 URL 部分的函数

如果 URL 中不存在相关部分,将返回空字符串。

protocol

从 URL 中提取协议。

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

domain

从 URL 中提取主机名。

语法

domain(url)

参数

URL 可指定带有或不带协议。示例:

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

在这些示例中,domain 函数返回以下结果:

some.svn-hosting.com
some.svn-hosting.com
clickhouse.com

返回值

  • 如果输入字符串可以解析为 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

从 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

如果存在,则返回没有前缀 www. 的域名。

语法

domainWithoutWWW(url)

参数

返回值

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

示例

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

domainWithoutWWWRFC

如果存在,则返回没有前缀 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                                                                                │
└─────────────────────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────────────┘

topLevelDomain

从 URL 中提取顶级域名。

topLevelDomain(url)

参数

备注

URL 可指定带有或不带协议。示例:

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

返回值

  • 如果输入字符串可以解析为 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

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

topLevelDomainRFC(url)

参数

备注

URL 可指定带有或不带协议。示例:

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

返回值

  • 如果输入字符串可以解析为 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                                               │
└────────────────────────────────────────────────┴───────────────────────────────────────────────────┘

firstSignificantSubdomain

返回“第一个显著子域”。 第一个显著子域是 comnetorgco 的二级域名,否则是三级域名。 例如,firstSignificantSubdomain ('https://news.clickhouse.com/') = 'clickhouse', firstSignificantSubdomain ('https://news.clickhouse.com.tr/') = 'clickhouse'。 “非显著”二级域名的列表和其他实现细节可能在未来更改。

语法

firstSignificantSubdomain(url)

参数

返回值

  • 第一个显著子域。 String

示例

查询:

SELECT firstSignificantSubdomain('http://www.example.com/a/b/c?a=b')

结果:

┌─firstSignificantSubdomain('http://www.example.com/a/b/c?a=b')─┐
│ example                                                       │
└───────────────────────────────────────────────────────────────┘

firstSignificantSubdomainRFC

返回“第一个显著子域”。 第一个显著子域是 comnetorgco 的二级域名,否则是三级域名。 例如,firstSignificantSubdomain ('https://news.clickhouse.com/') = 'clickhouse', firstSignificantSubdomain ('https://news.clickhouse.com.tr/') = 'clickhouse'。 “非显著”二级域名的列表和其他实现细节可能在未来更改。 与 firstSignficantSubdomain 类似,但符合 RFC 1034。

语法

firstSignificantSubdomainRFC(url)

参数

返回值

  • 第一个显著子域。 String

示例

查询:

SELECT
    firstSignificantSubdomain('http://user:[email protected]:8080/path?query=value#fragment'),
    firstSignificantSubdomainRFC('http://user:[email protected]:8080/path?query=value#fragment');

结果:

┌─firstSignificantSubdomain('http://user:[email protected]:8080/path?query=value#fragment')─┬─firstSignificantSubdomainRFC('http://user:[email protected]:8080/path?query=value#fragment')─┐
│                                                                                              │ example                                                                                         │
└──────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomain

返回包括顶级子域到“第一个显著子域”的域的部分。

语法

cutToFirstSignificantSubdomain(url)

参数

返回值

  • 包括顶级子域到第一个显著子域的域的部分(如果可能),否则返回空字符串。 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                                       │                                      │
└───────────────────────────────────────────────────────────────────┴──────────────────────────────────────────┴──────────────────────────────────────┘

cutToFirstSignificantSubdomainRFC

返回包括顶级子域到“第一个显著子域”的域的部分。 与 cutToFirstSignificantSubdomain 类似,但符合 RFC 3986。

语法

cutToFirstSignificantSubdomainRFC(url)

参数

返回值

  • 包括顶级子域到第一个显著子域的域的部分(如果可能),否则返回空字符串。 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

返回包括顶级子域到“第一个显著子域”的域的部分,保留 www

语法

cutToFirstSignificantSubdomainWithWWW(url)

参数

返回值

  • 包括顶级子域到第一个显著子域的域的部分(带 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

返回包括顶级子域到“第一个显著子域”的域的部分,保留 www。 与 cutToFirstSignificantSubdomainWithWWW 类似,但符合 RFC 3986。

语法

cutToFirstSignificantSubdomainWithWWW(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                                                                                  │
└───────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomainCustom

返回包括顶级子域到第一个显著子域的域的部分。 接受自定义 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>

语法

cutToFirstSignificantSubdomain(url, tld)

参数

  • url — URL。 String
  • tld — 自定义 TLD 列表名称。 String

返回值

  • 返回包括顶级子域到第一个显著子域的域的部分。 String

示例

查询:

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

结果:

┌─cutToFirstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list')─┐
│ foo.there-is-no-such-domain                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

参见

cutToFirstSignificantSubdomainCustomRFC

返回包括顶级子域到第一个显著子域的域的部分。 接受自定义 TLD 列表 名称。 如果需要新的 TLD 列表或具有自定义列表,此函数可能会很有用。 与 cutToFirstSignificantSubdomainCustom 类似,但符合 RFC 3986。

语法

cutToFirstSignificantSubdomainRFC(url, tld)

参数

  • url — URL。 String
  • tld — 自定义 TLD 列表名称。 String

返回值

  • 返回包括顶级子域到第一个显著子域的域的部分。 String

参见

cutToFirstSignificantSubdomainCustomWithWWW

返回包括顶级子域到第一个显著子域的域的部分而不去掉 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>

语法

cutToFirstSignificantSubdomainCustomWithWWW(url, tld)

参数

  • url — URL。 String
  • tld — 自定义 TLD 列表名称。 String

返回值

  • 返回包括顶级子域到第一个显著子域的域的部分而不去掉 wwwString

示例

查询:

SELECT cutToFirstSignificantSubdomainCustomWithWWW('www.foo', 'public_suffix_list');

结果:

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

参见

cutToFirstSignificantSubdomainCustomWithWWWRFC

返回包括顶级子域到第一个显著子域的域的部分而不去掉 www。 接受自定义 TLD 列表名称。 如果需要新的 TLD 列表或具有自定义列表,此函数可能会很有用。 与 cutToFirstSignificantSubdomainCustomWithWWW 类似,但符合 RFC 3986。

语法

cutToFirstSignificantSubdomainCustomWithWWWRFC(url, tld)

参数

  • url — URL。 String
  • tld — 自定义 TLD 列表名称。 String

返回值

  • 返回包括顶级子域到第一个显著子域的域的部分而不去掉 wwwString

参见

firstSignificantSubdomainCustom

返回第一个显著子域。 接受自定义 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>

语法

firstSignificantSubdomainCustom(url, tld)

参数

  • url — URL。 String
  • tld — 自定义 TLD 列表名称。 String

返回值

  • 第一个显著子域。 String

示例

查询:

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

结果:

┌─firstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list')─┐
│ foo                                                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────┘

参见

firstSignificantSubdomainCustomRFC

返回第一个显著子域。 接受自定义 TLD 列表名称。 如果需要新 TLD 列表或具有自定义列表,此功能可能会很有用。 与 firstSignificantSubdomainCustom 类似,但符合 RFC 3986。

语法

firstSignificantSubdomainCustomRFC(url, tld)

参数

  • url — URL。 String
  • tld — 自定义 TLD 列表名称。 String

返回值

  • 第一个显著子域。 String

参见

port

返回端口或者 default_port,如果 URL 不包含端口或无法解析。

语法

port(url [, default_port = 0])

参数

  • url — URL。 String
  • default_port — 要返回的默认端口号。 UInt16

返回值

  • 端口或默认端口,如果 URL 中没有端口或发生验证错误。 UInt16

示例

查询:

SELECT port('http://[email protected]:80/');

结果:

┌─port('http://[email protected]:80/')─┐
│                                      80 │
└─────────────────────────────────────────┘

portRFC

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

语法

portRFC(url [, default_port = 0])

参数

  • url — URL。 String
  • default_port — 要返回的默认端口号。 UInt16

返回值

  • 端口或默认端口,如果 URL 中没有端口或发生验证错误。 UInt16

示例

查询:

SELECT
    port('http://user:[email protected]:8080'),
    portRFC('http://user:[email protected]:8080');

结果:

┌─port('http://user:[email protected]:8080')─┬─portRFC('http://user:[email protected]:8080')─┐
│                                             0 │                                             8080 │
└───────────────────────────────────────────────┴──────────────────────────────────────────────────┘

path

返回没有查询字符串的路径。

示例: /top/news.html

pathFull

与上面相同,但包括查询字符串和片段。

示例: /top/news.html?page=2#comments

protocol

从 URL 中提取协议。

语法

protocol(url)

参数

  • url — 要提取协议的 URL。 String

返回值

  • 协议,如果无法确定则返回空字符串。 String

示例

查询:

SELECT protocol('https://clickhouse.com/');

结果:

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

queryString

返回查询字符串,不包括开头的问号,## 后的所有内容。

示例: page=1&lr=213

fragment

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

queryStringAndFragment

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

示例: page=1#29390

extractURLParameter(url, name)

返回 URL 中 name 参数的值(如果存在),否则返回空字符串。 如果该名称存在多个参数,则返回第一个出现的值。 该函数假设 url 参数中的参数以与 name 参数中的相同方式编码。

extractURLParameters(url)

返回与 URL 参数对应的 name=value 字符串数组。 值未解码。

extractURLParameterNames(url)

返回与 URL 参数名称对应的名称字符串数组。 值未解码。

URLHierarchy(url)

返回一个数组,包含 URL,路径和查询字符串末尾截断的符号 / 和 ?。 连续的分隔符字符计为一个。 切割是在所有连续分隔符字符后进行的。

URLPathHierarchy(url)

与上面相同,但结果中不包含协议和主机。根元素 / 不包括在内。

URLPathHierarchy('https://example.com/browse/CONV-6788') =
[
    '/browse/',
    '/browse/CONV-6788'
]

encodeURLComponent(url)

返回编码后的 URL。

示例:

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 │
└──────────────────────────────────────────────────────────┘

decodeURLComponent(url)

返回解码后的 URL。

示例:

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

encodeURLFormComponent(url)

返回编码后的 URL。遵循 rfc-1866,空格( )编码为加号(+)。

示例:

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 │
└───────────────────────────────────────────────────────────┘

decodeURLFormComponent(url)

返回解码后的 URL。遵循 rfc-1866,普通的加号(+)解码为空格( )。

示例:

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 │
└───────────────────────────────────────────┘

netloc

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

语法

netloc(url)

参数

返回值

  • username:password@host:portString

示例

查询:

SELECT netloc('http://[email protected]:80/');

结果:

┌─netloc('http://[email protected]:80/')─┐
│ [email protected]:80                   │
└───────────────────────────────────────────┘

删除 URL 部分的函数

如果 URL 不具有任何相似的内容,则 URL 保持不变。

cutWWW

从 URL 的域中删除前缀 www.(如果存在)。

cutQueryString

删除查询字符串,包括问号。

cutFragment

删除片段标识符,包括数字符号。

cutQueryStringAndFragment

删除查询字符串和片段标识符,包括问号和数字符号。

cutURLParameter(url, name)

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

语法

cutURLParameter(url, name)

参数

  • url — URL。 String
  • name — URL 参数名称。 StringArray 的字符串。

返回值

  • 删除了 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 │
└──────────────────────────────┴──────────────────────────┘