一.字典创建和查询

1.创建表和数据:

drop table t_region;
create table t_region(region_id UInt64, parent_region UInt64, region_name String) ENGINE=TinyLog;
insert into t_region values
(1, 0, 'jiangsu'),(2, 1, 'suzhou'),(3, 2, 'huqiu'),(4, 0, 'anhui'),(5, 4, 'hefei');创建字典, 指定HIERARCHICAL字段:
DROP DICTIONARY t_dict_region;
CREATE DICTIONARY t_dict_region (region_id UInt64,parent_region UInt64  HIERARCHICAL,region_name String
)
PRIMARY KEY region_id
SOURCE(CLICKHOUSE(host 'localhost'port 9001user 'default'db 'default'password ''table 't_region'
))
LAYOUT(HASHED())
LIFETIME(30);

2.字典的查询

SELECT dictGetString('default.t_dict_region', 'region_name', toUInt64(2)) AS regionName;┌─regionName─┐
│ suzhou     │
└────────────┘
SELECT dictGetHierarchy('default.t_dict_region', toUInt64(3));┌─dictGetHierarchy('default.t_dict_region', toUInt64(3))─┐
│ [3,2,1]                                                │
└────────────────────────────────────────────────────────┘

3.字典数据源之mysql表

  1. 在mysql数据库创建表并插入数据:
drop table test.test_dc;
create table test.test_dc(id bigint,name varchar(100),age int,PRIMARY KEY (id)
);insert into test.test_dc values(1, 'flink', 4);
insert into test.test_dc values(2, 'spark', 6);
insert into test.test_dc values(3, 'clickhouse', 5);查看MySQL数据:
mysql> select * from test.test_dc;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
|  1 | flink      |    4 |
|  2 | spark      |    6 |
|  3 | clickhouse |    5 |
+----+------------+------+
  1. 在ClickHouse创建字典:
DROP DICTIONARY mydicmysql;
CREATE DICTIONARY mydicmysql
(id UInt64,name String,age UInt8
)
PRIMARY KEY id
SOURCE(MYSQL(port 3306user 'root'password '123456'replica(host '127.0.0.1' priority 1)replica(host '127.0.0.1' priority 1)db 'test'table 'test_dc'invalidate_query 'select age from test.test_dc where id=3'
))
LAYOUT(FLAT())
LIFETIME(MIN 3 MAX 5);
select dictGet('default.mydicmysql', 'name', toUInt64(2)) as name;更改mysql的age字段生效。

4.字典的数据源之文件数据源

TabSeparated格式
文件示例:准备测试数据
文件命名为person.tsv,存放在目录:/var/lib/clickhouse/user_files,字段之间使用制表符分隔,即格式为TabSeparated。数据如下:
1   'id001'   'xiaohe'  23
2   'id002'   'xiaoxue' 25
3   'id003'   'xiaoyu'  26
4   'id004'   'xiaoxi'  27创建字典:
DROP DICTIONARY t_dict_person_ddl;
CREATE DICTIONARY t_dict_person_ddl
(id UInt64,code String,name String,age UInt8
)
PRIMARY KEY id
SOURCE(FILE(path '/var/lib/clickhouse/user_files/person.tsv' format 'TabSeparated'))
LAYOUT(FLAT())
LIFETIME(30);SELECT dictGetString('default.t_dict_person_ddl', 'name', toUInt64(2)) AS regionName;

当然,字典类型的数据也可以通过配置实现

<yandex> <dictionary> <name>t_dict_executable</name>  <structure> <id> <name>id</name> </id>  <attribute> <name>code</name>  <type>String</type>  <null_value/> </attribute>  <attribute> <name>name</name>  <type>String</type>  <null_value/> </attribute>  <attribute> <name>age</name>  <type>UInt8</type>  <null_value/> </attribute> </structure>  <source> <executable><command>cat /var/lib/clickhouse/user_files/person.tsv</command><format>TabSeparated</format></executable></source>  <layout> <hashed/> </layout>  <lifetime>10</lifetime> </dictionary>
</yandex>

二.字典的存储方式

以下测试均在default数据库。

1. flat/hash/sparse_hash/cache

DROP DICTIONARY t_dict_person_ddl;
CREATE DICTIONARY t_dict_person_ddl
(id UInt64,code String,name String,age UInt8
)
PRIMARY KEY id
SOURCE(CLICKHOUSE(host 'localhost'port 9001user 'default'db 'default'password ''table 't_dic_ch'where 'id>0'
))
LAYOUT(CACHE(SIZE_IN_CELLS 10000))
LIFETIME(30);SELECT dictGetString('default.t_dict_person_ddl', 'name', toUInt64(2)) AS regionName;

FLAT()、HASHED()、SPARSE_HASHED()、CACHE(SIZE_IN_CELLS 10000)

2. complex_key_hashed/complex_key_cache

DROP DICTIONARY t_dict_person_ddl;
CREATE DICTIONARY t_dict_person_ddl
(id UInt64,code String,name String,age UInt8
)
PRIMARY KEY id,code
SOURCE(CLICKHOUSE(host 'localhost'port 9001user 'default'db 'default'password ''table 't_dic_ch'where 'id>0'
))
LAYOUT(COMPLEX_KEY_HASHED())
LIFETIME(30);SELECT dictGet('default.t_dict_person_ddl', 'name', tuple(toUInt64(2), 'id002')) AS name;

COMPLEX_KEY_HASHED()、COMPLEX_KEY_CACHE(SIZE_IN_CELLS 10000)

3. range_hashed

drop table t_hash_range;
create table t_hash_range(id UInt64, start Date, end Date, amount Float32) ENGINE=TinyLog;
insert into t_hash_range values
(123, '2020-03-20', '2020-03-22', 0.15)
(123, '2020-03-23', '2020-03-27', 0.25)
(456, '2020-04-20', '2020-04-30', 0.35)
;查看数据:
SELECT * FROM t_hash_range;┌──id─┬──────start─┬────────end─┬─amount─┐
│ 123 │ 2020-03-20 │ 2020-03-22 │   0.15 │
│ 123 │ 2020-03-23 │ 2020-03-27 │   0.25 │
│ 456 │ 2020-04-20 │ 2020-04-30 │   0.35 │
└─────┴────────────┴────────────┴────────┘创建字典:
DROP DICTIONARY t_dict_hash_range;
CREATE DICTIONARY t_dict_hash_range (id UInt64,start Date,end Date,amount Float32
)
PRIMARY KEY id
SOURCE(CLICKHOUSE(host 'localhost'port 9001user 'default'db 'default'password ''table 't_hash_range'
))
LAYOUT(RANGE_HASHED())
RANGE(MIN start MAX end)
LIFETIME(30);查看id为123的记录,在日期2020-03-21日的amount:
select dictGetFloat32('default.t_dict_hash_range', 'amount', toUInt64(123), toDate('2020-03-21')) as amount;查看id为123的记录,在日期2020-03-25日的amount:
select dictGetFloat32('default.t_dict_hash_range', 'amount', toUInt64(123), toDate('2020-03-25')) as amount;日期之外的记录:
SELECT dictGetFloat32('default.t_dict_hash_range', 'amount', toUInt64(123), toDate('2020-03-29')) AS amount;

4. ip_tire

创建表和测试数据:
drop table t_ip_tire;
create table t_ip_tire(prefix String, asn UInt32, ccode String) ENGINE=TinyLog;
insert into t_ip_tire values
('202.79.32.0/20', 17501, 'NP')
('2620:0:870::/48', 3856, 'US')
('2a02:6b8:1::/48', 13238, 'RU')
('2001:db8::/32', 65536, 'ZZ')
;查看数据:
SELECT * FROM t_ip_tire;┌─prefix──────────┬───asn─┬─ccode─┐
│ 202.79.32.0/20  │ 17501 │ NP    │
│ 2620:0:870::/48 │  3856 │ US    │
│ 2a02:6b8:1::/48 │ 13238 │ RU    │
│ 2001:db8::/32   │ 65536 │ ZZ    │
└─────────────────┴───────┴───────┘创建字典:
DROP DICTIONARY t_dict_ip_tire;
CREATE DICTIONARY t_dict_ip_tire (prefix String,asn UInt32,ccode String
)
PRIMARY KEY prefix
SOURCE(CLICKHOUSE(host 'localhost'port 9001user 'default'db 'default'password ''table 't_ip_tire'
))
LAYOUT(IP_TRIE())
LIFETIME(30);检索数据:
select
dictGetUInt32('default.t_dict_ip_tire', 'asn', tuple(IPv4StringToNum('202.79.32.22'))) as asn,
dictGetString('default.t_dict_ip_tire', 'ccode', tuple(IPv4StringToNum('202.79.32.22'))) as ccode

Clickhouse 字典表使用场景相关推荐

  1. ClickHouse vs StarRocks 全场景MPP数据库选型对比

    ClickHouse vs StarRocks 选型对比 面向列存的 DBMS 新的选择 Hadoop 从诞生已经十三年了,Hadoop 的供应商争先恐后的为 Hadoop 贡献各种开源插件,发明各种 ...

  2. 业务中的字典表的MySQL实现方案

    为什么需要字典表? 某些变量在多个地方使用,而且一般固定,但随系统升级和后期变化,可能需要改变,如果这些变量写死在代码里面将会变得难以维护,所以要将其从代码中抽离. 一般的业务系统客户端与用户交互的时 ...

  3. redis初始化存储数据库字典表数据设计方案

    随着项目访问量的并发,对数据库操作越来越多,为了优化系统,将原数据库字典表提到了JVM内存里,但是随着开始着手集群后,弊端就出来了!最近想着将字典数据放到redis里,并且能够尽量实现多场景便捷读取. ...

  4. mysql表分区占用存储_MySQL 分区分表应用场景分析和分区中可能遇到的坑点

    MySQL的分区和分表应用场景分析 在日常工作中当我们的某张表的数据量过大的时候,首当其冲的可能就是进行分区和分表,但是是如何分区或者分表都要结合一点的业务场景下进行分析,才会显著的提升性能,来聊一聊 ...

  5. mysql dbcollat_Mysql Server 层混杂信息字典表 | 全方位认识 information_schem(四)

    在上一篇<Server层表级别对象字典表 | 全方位认识 information_schema>中,我们详细介绍了information_schema系统库的表级别对象字典表,本期我们将为 ...

  6. plsql tables 没有表_InnoDB 层锁、事务、统计信息字典表 | 全方位认识 information_schema...

    在上一篇<InnoDB 层系统字典表|全方位认识 information_schema>中,我们详细介绍了InnoDB层的系统字典表,本期我们将为大家带来系列第六篇<InnoDB 层 ...

  7. QPW 行政区划字典表(td_area)

    行政区划字典表 CREATE TABLE `td_area` (`area_code` varchar(10) NOT NULL COMMENT '区域编码',`area_name` varchar( ...

  8. td 字典表_数据库怎么设计字典表

    数据库怎么设计字典表,用来存储类型数据,需要可以编缉,大家都是怎么做的 CREATE TABLE `t_ci` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, ` ...

  9. 工作278:控制数据从字典表获取

    <el-form-item prop="role" label="角色" :label-width="formLabelWidth"& ...

最新文章

  1. 2015/4/24~GET方式和POST方式传值大小的限制
  2. 新的JavaScript库邀请程序员使用Canvas进行创意编程
  3. 第二十一节(数组概要, 一维、二维数组的声明和使用,数组的排序,数组的查找,)...
  4. 汤家凤高等数学基础手写笔记-不定积分
  5. c语言课程思政教案设计,设计类专业课程思政教学案例及教学设计
  6. HDU 6706 huntian oy (欧拉函数 + 杜教筛)
  7. project 2013 显示标题
  8. java中的文件操作:读取写入byte[]字节流、string字符串、list列表
  9. Centos上禁用 rpcbind 111端口
  10. 最短路径例题(Floyd、Dijkstra)
  11. 室内定位之蓝牙定位精度(蓝牙RSSI定位)
  12. 扬声器有小红叉,前置耳机孔没有声音,找不到realtek高清晰音频管理器
  13. 天蝎项目整机柜服务器解决方案,天蝎2.0整机柜服务器技术规范rev0.5(final).doc
  14. sht20 python_SHT20 IIC 寄存器概述
  15. 函数前置条件和后置条件
  16. 2020年小米校招JAVA岗笔试第二题
  17. Python模拟屏幕点击自动完成词达人任务(附源码)
  18. native、方法区
  19. BUUCTF WEB [BSidesCF 2020]Had a bad day
  20. PHP函数源码之SESSION实现机制

热门文章

  1. 锂电池生产工艺与环境要求|锂电池实验室设计SICOLAB
  2. 看《中国历代政治得失》
  3. python自动化弹框_Selenium2+python自动化16-alert\confirm\prompt
  4. Python爬虫练习:爬取糗事百科
  5. ViewBag 和 ViewData 的用法和区别
  6. 华润MMX链上云详解
  7. hadoop集群启动与关闭需要输入密码
  8. 三角形的内切圆与外接圆面积之比【几何计算】
  9. MHDD找不到硬盘的解决方案
  10. 高德 通过 起 经 止 经纬度 获取路线经纬度,(可搜索位置,新增经过点)