Clickhouse笔记——数据字典

数据字典

1. 内置字典

2. 外部扩展字典

2.1. 准备

以CSV数据为准,在ch的配置文件config.xml下,<dictionaries_config>*_dictionary.xml</dictionaries_config>, 默认将加载所有该匹配到的配置文件。并且支持不停机更新。完整的配置结构如下:

<?xml version="1.0"?>
<dictionaties>
    <dictionary>
        <name>geo</name>

        <structure>
            <!--典的数据结构-->
            <!-- <id> 或者 <key> 用于定义字典的键值,相当于数据库的主键,
包括数值型UInt64, 支持flat、hashed、range_hashed、cache-->
            <id>
                <!--Key属性-->
            </id>

            <!-- 符合型 -->
            <key>
                <attribute>
                    <name>field1</name>
                    <type>String</type>
                </attribute>
                <attribute>
                    <name>field2</name>
                    <type>UInt64</type>
                </attribute>
            </key>

            <attribute>
                <!--字段属性-->
            </attribute>
        </structure>

        <layout>
            <!--内存中的数据格式类型 7种-->
        </layout>

        <source>
            <!--数据源配置 文件、数据库和其他 三类数据来源-->
        </source>

        <lifetime>
            <!--字典的自动更新频率-->
        </lifetime>
    </dictionaty>
</dictionaties>

其中,标签attribute中的配置说明如下:

配置名称是否必填默认值说明
name字段名称
type字段类型
null_value在查询时,条件key没有对应元素时的默认值
expression无表达式表达式,可以调用或者表达式
hierarchicalfalse是否支持层次结构
injectivefalse是否支持集合单映射优化,开启后在后续的GROUP BY查询中,如果调用了dictGet函数通过key获取value,则该value直接从GROUP BY数据返回
is_object_idfalse是否开启MongoDB优化,通过ObjectID对MongoDB文档执行查询

2.2. 扩展字典的类型

扩展字典的类型使用layout元素定义,目前支持7种类型。一个字段类型,决定了其数据在内存中的存储结构和字典支持的key类型。一种是单数值的key类型,包括flat、hashed、range_hashed和cache;另一类是复合key类型,包括complex_key_hashed、complex_key_cache和ip_trie。

2.2.1. flat

flat字典是性能最高的字典类型,只能以UInt64数值型key,使用数组结构存储,初始大小为1024上限为500000,在创建字典时数据量超出其上限,那么字典会创建失败。以下为flat字典的示例:

测试数据:

1,"a0001","研发部"
2,"a0002","产品部"
3,"a0003","数据部"
4,"a0004","测试部"
5,"a0005","运维部"
6,"a0006","规划部"

配置文件:

<?xml version="1.0"?>
<dictionaries>
    <dictionary>
        <name>test_flat_dict</name>
        <!--数据源-->
        <source>
            <file>
                <path>/home/data/ch/organization.csv</path>
                <format>CSV</format>
            </file>
        </source>

        <!--字典类型-->
        <layout>
            <flat/>
        </layout>

        <!--与数据结构对应-->
        <structure>
            <id>
                <name>id</name>
            </id>
            
            <attribute>
                <name>code</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>

            <attribute>
                <name>name</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>
        </structure>

        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
    </dictionary>
</dictionaries>

由于字典数据是懒加载,起初的状态为 NOT_LOADED:
在这里插入图片描述

在查询了数据后:
在这里插入图片描述
在这里插入图片描述

2.2.2. hashed

hashed字典与flat不同的是,flat是以数组的方式存储,hashed则是散列结构,没有上限制约。
以下是hashed字典的配置示例:

<?xml version="1.0"?>
<dictionaries>
    <dictionary>
        <name>test_flat_dict</name>
        <!--数据源-->
        <source>
            <file>
                <path>/home/data/ch/organization.csv</path>
                <format>CSV</format>
            </file>
        </source>

        <!--字典类型  只有这个地方不一样-->
        <layout>
            <hashed/>
        </layout>

        <!--与数据结构对应-->
        <structure>
            <id>
                <name>id</name>
            </id>

            <attribute>
                <name>code</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>

            <attribute>
                <name>name</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>
        </structure>

        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
    </dictionary>
</dictionaries>
2.2.3. range_hashed

在hashed字典的基础上增加了指定时间区间的特性,数据会以散列结构存储并按照时间排序。时间的区间通过range_min和range_max元素指定,所指定的字段必须是Date或者DateTime类型。

<?xml version="1.0"?>
<dictionaries>
    <dictionary>
        <name>test_range_hashed_dict</name>
        <!--数据源-->
        <source>
            <file>
                <path>/home/data/ch/sales.csv</path>
                <format>CSV</format>
            </file>
        </source>

        <!--字典类型  只有这个地方不一样-->
        <layout>
            <range_hashed/>
        </layout>

        <!--与数据结构对应-->
        <structure>
            <id>
                <name>id</name>
            </id>

            <range_min>
                <name>start</name>
                <!--如果 type 如果没有指定,则默认类型将使用-Date-->
            </range_min>

            <range_max>
                <name>end</name>
            </range_max>

            <attribute>
                <name>price</name>
                <type>Float32</type>
                <null_value></null_value>
            </attribute>
        </structure>

        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
    </dictionary>
</dictionaries>
2.2.4. cache

在内存中会通过固定长度的向量数组保存,长度为2 的整数倍并会自动向上取整,并不会像其他字典查询一次后一次性全部直接加载到内存,而是命中一次加载一次,所以性能最不稳定,完全取决于命中率(缓存命中率=命中次数/查询次数)

2.2.5. complex_key_hashed

该类型的字典在功能上与hashed字典完全相同,只是将单个的数值的key替换成了复合型

<?xml version="1.0"?>
<dictionaries>
    <dictionary>
        <name>test_complex_hashed_dict</name>
        <!--数据源-->
        <source>
            <file>
                <path>/home/data/ch/organization.csv</path>
                <format>CSV</format>
            </file>
        </source>

        <!--字典类型  只有这个地方不一样-->
        <layout>
            <complex_key_hashed/>
        </layout>

        <!--与数据结构对应-->
        <structure>
            <key>
                <attribute>
                    <name>id</name>
                    <type>UInt64</type>
                </attribute>
                <attribute>
                    <name>code</name>
                    <type>String</type>
                </attribute>
            </key>

            <attribute>
                <name>name</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>
        </structure>

        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
    </dictionary>
</dictionaries>

查询方式如下:
在这里插入图片描述

2.2.6. complex_key_cache

在cache字典的基础上,将单数值的key替换为复合型。

2.2.7. ip_trie

专门用于IP前缀查询的场景,配置如下:

<?xml version="1.0"?>
<dictionaries>
    <dictionary>
        <name>test_ip_trie_dict</name>
        <!--数据源-->
        <source>
            <file>
                <path>/home/data/ch/asn.csv</path>
                <format>CSV</format>
            </file>
        </source>

        <!--字典类型-->
        <layout>
            <ip_trie/>
        </layout>

        <!--与数据结构对应-->
        <structure>
            <key>
                <attribute>
                    <name>prefix</name>
                    <type>String</type>
                </attribute>
            </key>

            <attribute>
                <name>asn</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>

            <attribute>
                <name>country</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>
        </structure>

        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
    </dictionary>
</dictionaries>

在这里插入图片描述

2.2.8. 总结
名称存储结构字典键类型支持的数据源
flat数组UInt64Local file、Executable file、HTTP、DBMS
hashed散列UInt64Local file、Executable file、HTTP、DBMS
range_hashed散列按时间排序UInt64和时间Local file、Executable file、HTTP、DBMS
complex_key_hashed散列复合型keyLocal file、Executable file、HTTP、DBMS
ip_trie层次结构复合型key(单个String)Local file、Executable file、HTTP、DBMS
cache固定大小数组UInt64Executable file、HTTP、ClickHouse、MySQL
complex_key_cache固定大小数组复合型keyExecutable file、HTTP、ClickHouse、MySQL

2.3 数据源

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值