ClickHouse/docs/en/functions/ym_dict_functions.rst

120 lines
7.2 KiB
ReStructuredText
Raw Normal View History

2017-04-26 19:16:38 +00:00
Functions for working with Yandex.Metrica dictionaries
2017-04-03 19:49:50 +00:00
----------------
2017-04-26 19:16:38 +00:00
In order for the functions below to work, the server config must specify the paths and addresses for getting all the Yandex.Metrica dictionaries. The dictionaries are loaded at the first call of any of these functions. If the reference lists can't be loaded, an exception is thrown.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
For information about creating reference lists, see the section "Dictionaries".
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
Multiple geobases
2017-04-03 19:49:50 +00:00
~~~~~~~~~
2017-04-26 19:16:38 +00:00
ClickHouse supports working with multiple alternative geobases (regional hierarchies) simultaneously, in order to support various perspectives on which countries certain regions belong to.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
The 'clickhouse-server' config specifies the file with the regional hierarchy:
2017-04-03 19:49:50 +00:00
``<path_to_regions_hierarchy_file>/opt/geo/regions_hierarchy.txt</path_to_regions_hierarchy_file>``
2017-04-26 19:16:38 +00:00
Besides this file, it also searches for files nearby that have the _ symbol and any suffix appended to the name (before the file extension).
For example, it will also find the file ``/opt/geo/regions_hierarchy_ua.txt``, if present.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
``ua`` is called the dictionary key. For a dictionary without a suffix, the key is an empty string.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
All the dictionaries are re-loaded in runtime (once every certain number of seconds, as defined in the builtin_dictionaries_reload_interval config parameter, or once an hour by default). However, the list of available dictionaries is defined one time, when the server starts.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
All functions for working with regions have an optional argument at the end - the dictionary key. It is indicated as the geobase.
Example:
2017-04-03 19:49:50 +00:00
::
2017-04-26 19:16:38 +00:00
regionToCountry(RegionID) - Uses the default dictionary: /opt/geo/regions_hierarchy.txt
regionToCountry(RegionID, '') - Uses the default dictionary: /opt/geo/regions_hierarchy.txt
regionToCountry(RegionID, 'ua') - Uses the dictionary for the 'ua' key: /opt/geo/regions_hierarchy_ua.txt
2017-04-03 19:49:50 +00:00
regionToCity(id[, geobase])
~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts a UInt32 number - the region ID from the Yandex geobase. If this region is a city or part of a city, it returns the region ID for the appropriate city. Otherwise, returns 0.
2017-04-03 19:49:50 +00:00
regionToArea(id[, geobase])
~~~~~~~~
2017-04-26 19:16:38 +00:00
Converts a region to an area (type 5 in the geobase). In every other way, this function is the same as 'regionToCity'.
2017-04-03 19:49:50 +00:00
.. code-block:: sql
2017-04-26 19:16:38 +00:00
SELECT DISTINCT regionToName(regionToArea(toUInt32(number), 'ua'), 'en')
2017-04-03 19:49:50 +00:00
FROM system.numbers
LIMIT 15
2017-04-26 19:16:38 +00:00
┌─regionToName(regionToArea(toUInt32(number), \'ua\'), \'en\')─┐
│ │
│ Moscow and Moscow region │
│ Saint-Petersburg and Leningradskaya oblast │
│ Belgorod District │
│ Ivanovo district │
│ Kaluga District │
│ Kostroma District │
│ Kursk District │
│ Lipetsk District │
│ Orel District │
│ Ryazhan District │
│ Smolensk District │
│ Tambov District │
│ Tver District │
│ Tula District │
└──────────────────────────────────────────────────────────────┘
2017-04-03 19:49:50 +00:00
regionToDistrict(id[, geobase])
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Converts a region to a federal district (type 4 in the geobase). In every other way, this function is the same as 'regionToCity'.
2017-04-03 19:49:50 +00:00
.. code-block:: sql
2017-04-26 19:16:38 +00:00
SELECT DISTINCT regionToName(regionToDistrict(toUInt32(number), 'ua'), 'en')
2017-04-03 19:49:50 +00:00
FROM system.numbers
LIMIT 15
2017-04-26 19:16:38 +00:00
┌─regionToName(regionToDistrict(toUInt32(number), \'ua\'), \'en\')─┐
│ │
│ Central │
│ Northwest │
│ South │
│ North Kavkaz │
│ Volga Region │
│ Ural │
│ Siberian │
│ Far East │
│ Scotland │
│ Faroe Islands │
│ Flemish Region │
│ Brussels-Capital Region │
│ Wallonia │
│ Federation of Bosnia and Herzegovina │
└──────────────────────────────────────────────────────────────────┘
2017-04-03 19:49:50 +00:00
regionToCountry(id[, geobase])
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Converts a region to a country. In every other way, this function is the same as 'regionToCity'.
Example: ``regionToCountry(toUInt32(213)) = 225`` converts ``Moscow (213)`` to ``Russia (225)``.
2017-04-03 19:49:50 +00:00
regionToContinent(id[, geobase])
~~~~~~~~~
2017-04-26 19:16:38 +00:00
Converts a region to a continent. In every other way, this function is the same as 'regionToCity'.
Example: ``regionToContinent(toUInt32(213)) = 10001`` converts Moscow (213) to Eurasia (10001).
2017-04-03 19:49:50 +00:00
regionToPopulation(id[, geobase])
~~~~~~~~
2017-04-26 19:16:38 +00:00
Gets the population for a region.
The population can be recorded in files with the geobase. See the section "External dictionaries".
If the population is not recorded for the region, it returns 0.
In the Yandex geobase, the population might be recorded for child regions, but not for parent regions..
2017-04-03 19:49:50 +00:00
regionIn(lhs, rhs[, geobase])
~~~~~~~~~~
2017-04-26 19:16:38 +00:00
Checks whether a 'lhs' region belongs to a 'rhs' region. Returns a UInt8 number equal to 1 if it belongs, or 0 if it doesn't belong.
The relationship is reflexive - any region also belongs to itself.
2017-04-03 19:49:50 +00:00
regionHierarchy(id[, geobase])
~~~~~~~~~
2017-04-26 19:16:38 +00:00
ПAccepts a UInt32 number - the region ID from the Yandex geobase. Returns an array of region IDs consisting of the passed region and all parents along the chain.
Example: ``regionHierarchy(toUInt32(213)) = [213,1,3,225,10001,10000]``.
2017-04-03 19:49:50 +00:00
regionToName(id[, lang])
~~~~~~~~
2017-04-26 19:16:38 +00:00
Accepts a UInt32 number - the region ID from the Yandex geobase. A string with the name of the language can be passed as a second argument. Supported languages are: ru, en, ua, uk, by, kz, tr. If the second argument is omitted, the language 'ru' is used. If the language is not supported, an exception is thrown. Returns a string - the name of the region in the corresponding language. If the region with the specified ID doesn't exist, an empty string is returned.
2017-04-03 19:49:50 +00:00
2017-04-26 19:16:38 +00:00
``ua`` and ``uk`` mean the same thing - Ukrainian.