NET_PUBLIC_SUFFIX

NET_PUBLIC_SUFFIX函数用于从STRING类型的URL字符串中解析出公共后缀(如com、orgnet)。

注意事项

  • 公共后缀是在publicsuffix.org注册的ICANN域。publicsuffix.org上的公共后缀数据也包含私有域,该函数忽略私有域。

命令格式

STRING NET_PUBLIC_SUFFIX(STRING <url>)

参数说明

url:必填。STRING类型。待解析的URL字符串。

说明

为了获得最佳解析结果,URL字符串应符合RFC 3986定义的格式。

返回值说明

返回STRING类型。 如果下列任一条件成立,则该函数返回NULL:

  • 无法从输入中解析主机名(hostname)部分;

  • 解析的主机名(hostname)部分在中间包含相邻的点(不是前导或尾随);

  • 解析后的主机名(hostname)部分不包含任何公共后缀。

说明

公共后缀数据可能会随时间而变化。因此,现在产生NULL结果的输入将来可能会产生非NULL值。

相关解析函数

若需要解析URL中除公共后缀的其他部分,可以使用如下函数:

  • NET_HOST:解析给定的URL中主机名(hostname)部分。

  • NET_REG_DOMAIN: 解析给定的URL中已注册或可注册的域名(即公共后缀加上前一级标签)。

使用示例

SELECT input
,description
,NET_HOST(input) AS HOST
,NET_PUBLIC_SUFFIX(input) AS SUFFIX
,NET_REG_DOMAIN(input) AS DOMAIN  
FROM (
  SELECT "" AS input, "invalid input" AS description
  UNION ALL SELECT "http://abc.xyz", "standard URL"
  UNION ALL SELECT "//user:password@a.b:80/path?query",
                   "standard URL with relative scheme, port, path and query, but no public suffix"
  UNION ALL SELECT "https://[::1]:80", "standard URL with IPv6 host"
  UNION ALL SELECT "http://例子.卷筒纸.中国", "standard URL with internationalized domain name"
  UNION ALL SELECT "    www.Example.Co.UK    ",
                   "non-standard URL with spaces, upper case letters, and without scheme"
  UNION ALL SELECT "mailto:?to=&subject=&body=", "URI rather than URL--unsupported"
);
                         
+-----------------------------------+-------------------------------------------------------------------------------+-------------------+------------+---------------+
| input                             | description                                                                   | host              | suffix     | domain        | 
+-----------------------------------+-------------------------------------------------------------------------------+-------------------+------------+---------------+
|                                   | invalid input                                                                 | NULL              | NULL       | NULL          | 
| http://abc.xyz                    | standard URL                                                                  | abc.xyz           | xyz        | abc.xyz       | 
| //user:password@a.b:80/path?query | standard URL with relative scheme, port, path and query, but no public suffix | a.b               | NULL       | NULL          | 
| https://[::1]:80                  | standard URL with IPv6 host                                                   | [::1]             | NULL       | NULL          | 
| http://例子.卷筒纸.中国             | standard URL with internationalized domain name                               | 例子.卷筒纸.中国     | 中国       | 卷筒纸.中国     | 
|         www.Example.Co.UK         | non-standard URL with spaces, upper case letters, and without scheme          | www.Example.Co.UK | Co.UK      | Example.Co.UK | 
| mailto:?to=&subject=&body=        | URI rather than URL--unsupported                                              | mailto            | NULL       | NULL          | 
+-----------------------------------+-------------------------------------------------------------------------------+-------------------+------------+---------------+

相关函数

NET_PUBLIC_SUFFIX函数属于网络函数,更多网络相关函数请参见网络函数