URL 相关函数
本节中提到的函数针对最大性能进行了优化,并且在大多数情况下不遵循 RFC-3986 标准。实现 RFC-3986 的函数在其函数名后附加了 RFC
,并且通常较慢。
当处理不包含用户字符串或 `@` 符号的公共注册域名时,通常可以使用非 RFC
函数变体。下表详细说明了 URL 中的哪些符号可以 (✔
) 或不能 (✗
) 被相应的 RFC
和非 RFC
变体解析
符号 | 非 RFC | RFC |
---|---|---|
' ' | ✗ | ✗ |
\t | ✗ | ✗ |
< | ✗ | ✗ |
> | ✗ | ✗ |
% | ✗ | ✔* |
{ | ✗ | ✗ |
} | ✗ | ✗ |
| | ✗ | ✗ |
\\ | ✗ | ✗ |
^ | ✗ | ✗ |
~ | ✗ | ✔* |
[ | ✗ | ✗ |
] | ✗ | ✔ |
; | ✗ | ✔* |
= | ✗ | ✔* |
& | ✗ | ✔* |
标记为 `*` 的符号是 RFC 3986 中的子分隔符,并允许在 `@` 符号后的用户信息中使用。
提取 URL 部分的函数
如果 URL 中不存在相关部分,则返回空字符串。
protocol
从 URL 中提取协议。
典型返回值示例:http、https、ftp、mailto、tel、magnet。
domain
从 URL 中提取主机名。
语法
domain(url)
参数
url
— URL。字符串。
URL 可以指定带协议或不带协议。示例
svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.ac.cn/time/
对于这些示例,domain
函数返回以下结果
some.svn-hosting.com
some.svn-hosting.com
clickhouse.com
返回值
- 如果输入字符串可以解析为 URL,则返回主机名,否则返回空字符串。字符串。
示例
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
— URL。字符串。
返回值
- 如果输入字符串可以解析为 URL,则返回主机名,否则返回空字符串。字符串。
示例
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
— URL。字符串。
返回值
- 如果输入字符串可以解析为 URL(不带前导
www.
),则返回域名,否则返回空字符串。字符串。
示例
SELECT domainWithoutWWW('http://[email protected]:80/');
┌─domainWithoutWWW('http://[email protected]:80/')─┐
│ example.com │
└─────────────────────────────────────────────────────┘
domainWithoutWWWRFC
如果存在,则返回不带前导 www.
的域名。类似于 domainWithoutWWW,但符合 RFC 3986 标准。
语法
domainWithoutWWWRFC(url)
参数
url
— URL。字符串。
返回值
- 如果输入字符串可以解析为 URL(不带前导
www.
),则返回域名,否则返回空字符串。字符串。
示例
查询
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
— URL。字符串。
URL 可以指定带协议或不带协议。示例
svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.ac.cn/time/
返回值
- 如果输入字符串可以解析为 URL,则返回域名。否则,返回空字符串。字符串。
示例
查询
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
— URL。字符串。
URL 可以指定带协议或不带协议。示例
svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.ac.cn/time/
返回值
- 如果输入字符串可以解析为 URL,则返回域名。否则,返回空字符串。字符串。
示例
查询
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
返回“第一个重要子域名”。对于 com
、net
、org
或 co
,第一个重要子域名是二级域名,否则为三级域名。例如,firstSignificantSubdomain (‘https://news.clickhouse.com/’) = ‘clickhouse’
,firstSignificantSubdomain (‘https://news.clickhouse.com.tr/’) = ‘clickhouse’
。“不重要”的二级域名列表和其他实现细节将来可能会更改。
语法
firstSignificantSubdomain(url)
参数
url
— URL。字符串。
返回值
- 第一个重要子域名。字符串。
示例
查询
SELECT firstSignificantSubdomain('http://www.example.com/a/b/c?a=b')
结果
┌─firstSignificantSubdomain('http://www.example.com/a/b/c?a=b')─┐
│ example │
└───────────────────────────────────────────────────────────────┘
firstSignificantSubdomainRFC
返回“第一个重要子域名”。对于 com
、net
、org
或 co
,第一个重要子域名是二级域名,否则为三级域名。例如,firstSignificantSubdomain (‘https://news.clickhouse.com/’) = ‘clickhouse’
,firstSignificantSubdomain (‘https://news.clickhouse.com.tr/’) = ‘clickhouse’
。“不重要”的二级域名列表和其他实现细节将来可能会更改。类似于 firstSignficantSubdomain,但符合 RFC 1034 标准。
语法
firstSignificantSubdomainRFC(url)
参数
url
— URL。字符串。
返回值
- 第一个重要子域名。字符串。
示例
查询
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)
参数
url
— URL。字符串。
返回值
- 如果可能,则返回域名的一部分,其中包括顶级子域名,直到第一个重要子域名,否则返回空字符串。字符串。
示例
查询
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)
参数
url
— URL。字符串。
返回值
- 如果可能,则返回域名的一部分,其中包括顶级子域名,直到第一个重要子域名,否则返回空字符串。字符串。
示例
查询
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)
参数
url
— URL。字符串。
返回值
- 如果可能,则返回域名的一部分,其中包括顶级子域名,直到第一个重要子域名(带
www
),否则返回空字符串。字符串。
示例
查询
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)
参数
url
— URL。字符串。
返回值
- 如果可能,则返回域名的一部分,其中包括顶级子域名,直到第一个重要子域名(带 “www”),否则返回空字符串。字符串。
示例
查询
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)
参数
返回值
- 域名的一部分,其中包括顶级子域名,直到第一个重要子域名。字符串。
示例
查询
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)
参数
返回值
- 域名的一部分,其中包括顶级子域名,直到第一个重要子域名。字符串。
另请参阅
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)
参数
返回值
- 域名的一部分,其中包括顶级子域名,直到第一个重要子域名,但不剥离
www
。字符串。
示例
查询
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)
参数
返回值
- 域名的一部分,其中包括顶级子域名,直到第一个重要子域名,但不剥离
www
。字符串。
另请参阅
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)
参数
返回值
- 第一个重要子域名。字符串。
示例
查询
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)
参数
返回值
- 第一个重要子域名。字符串。
另请参阅
port
如果 URL 不包含端口或无法解析,则返回端口或 default_port
。
语法
port(url [, default_port = 0])
参数
返回值
- 如果 URL 中没有端口,或者在验证错误的情况下,返回端口或默认端口。UInt16。
示例
查询
SELECT port('http://[email protected]:80/');
结果
┌─port('http://[email protected]:80/')─┐
│ 80 │
└─────────────────────────────────────────┘
portRFC
如果 URL 不包含端口或无法解析,则返回端口或 default_port
。类似于 port,但符合 RFC 3986 标准。
语法
portRFC(url [, default_port = 0])
参数
返回值
- 如果 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。字符串。
返回值
- 协议,如果无法确定,则返回空字符串。字符串。
示例
查询
SELECT protocol('https://clickhouse.ac.cn/');
结果
┌─protocol('https://clickhouse.ac.cn/')─┐
│ 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,该 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)
参数
url
— URL。字符串。
返回值
username:password@host:port
。字符串。
示例
查询
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 ID
和 Client%20ID
被视为不同的参数名称。
语法
cutURLParameter(url, name)
参数
返回值
- 删除了
name
URL 参数的 URL。字符串。
示例
查询
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 │
└──────────────────────────────┴──────────────────────────┘