ClickHouse/doc/reference/en/functions/ip_address_functions.rst

102 lines
3.8 KiB
ReStructuredText

Functions for working with IP addresses
-------------------------
IPv4NumToString(num)
~~~~~~~~~~~~~
Takes a UInt32 number. Interprets it as an IPv4 address in big endian. Returns a string containing the corresponding IPv4 address in the format A.B.C.d (dot-separated numbers in decimal form).
IPv4StringToNum(s)
~~~~~~~~
The reverse function of IPv4NumToString. If the IPv4 address has an invalid format, it returns 0.
IPv4NumToStringClassC(num)
~~~~~~~~~~~
Similar to IPv4NumToString, but using ``xxx`` instead of the last octet.
Example:
.. code-block:: sql
SELECT
IPv4NumToStringClassC(ClientIP) AS k,
count() AS c
FROM test.hits
GROUP BY k
ORDER BY c DESC
LIMIT 10
kc
83.149.9.xxx 26238
217.118.81.xxx 26074
213.87.129.xxx 25481
83.149.8.xxx 24984
217.118.83.xxx 22797
78.25.120.xxx 22354
213.87.131.xxx 21285
78.25.121.xxx 20887
188.162.65.xxx 19694
83.149.48.xxx 17406
Since using ``'xxx'`` is highly unusual, this may be changed in the future. We recommend that you don't rely on the exact format of this fragment.
IPv6NumToString(x)
~~~~~~~~~~~~
Accepts a FixedString(16) value containing the IPv6 address in binary format. Returns a string containing this address in text format.
IPv6-mapped IPv4 addresses are output in the format ``::ffff:111.222.33.44``. Examples:
.. code-block:: sql
SELECT IPv6NumToString(toFixedString(unhex('2A0206B8000000000000000000000011'), 16)) AS addr
addr
2a02:6b8::11
SELECT
IPv6NumToString(ClientIP6 AS k),
count() AS c
FROM hits_all
WHERE EventDate = today() AND substring(ClientIP6, 1, 12) != unhex('00000000000000000000FFFF')
GROUP BY k
ORDER BY c DESC
LIMIT 10
IPv6NumToString(ClientIP6)c
2a02:2168:aaa:bbbb::2 24695
2a02:2698:abcd:abcd:abcd:abcd:8888:5555 22408
2a02:6b8:0:fff::ff 16389
2a01:4f8:111:6666::2 16016
2a02:2168:888:222::1 15896
2a01:7e00::ffff:ffff:ffff:222 14774
2a02:8109:eee:ee:eeee:eeee:eeee:eeee 14443
2a02:810b:8888:888:8888:8888:8888:8888 14345
2a02:6b8:0:444:4444:4444:4444:4444 14279
2a01:7e00::ffff:ffff:ffff:ffff 13880
SELECT
IPv6NumToString(ClientIP6 AS k),
count() AS c
FROM hits_all
WHERE EventDate = today()
GROUP BY k
ORDER BY c DESC
LIMIT 10
IPv6NumToString(ClientIP6)c
::ffff:94.26.111.111 747440
::ffff:37.143.222.4 529483
::ffff:5.166.111.99 317707
::ffff:46.38.11.77 263086
::ffff:79.105.111.111 186611
::ffff:93.92.111.88 176773
::ffff:84.53.111.33 158709
::ffff:217.118.11.22 154004
::ffff:217.118.11.33 148449
::ffff:217.118.11.44 148243
IPv6StringToNum(s)
~~~~~~~~
The reverse function of IPv6NumToString. If the IPv6 address has an invalid format, it returns a string of null bytes.
HEX can be uppercase or lowercase.