|NO.Z.00006|——————————|Deployment|——|Hadoop&OLAP数据库管理系统.v06|——|ClickHouse.v06|ClickHouse链接MySQL|


一、ClickHouse链接mysql
### --- mysql说明

~~~     MySQL 引擎可以对存储在远程 MySQL 服务器上的数据执行 SELECT 查询。
### --- 调用格式:

MySQL('host:port', 'database', 'table', 'user', 'password'[, replace_query,'on_duplicate_clause']);
### --- 调用参数

~~~     host:port — MySQL 服务器地址。
~~~     database — 数据库的名称。
~~~     table — 表名称。
~~~     user — 数据库用户。
~~~     password — 用户密码。
~~~     replace_query — 将 INSERT INTO 查询是否替换为 REPLACE INTO 的标志。
~~~     如果 replace_query=1,则替换查询
~~~     'on_duplicate_clause' — 将 ON DUPLICATE KEY UPDATE 'on_duplicate_clause' 
~~~     表达式添加到 INSERT 查询语句中。
~~~     例如:impression = VALUES(impression) + impression。
~~~     如果需要指定 'on_duplicate_clause',则需要设置 replace_query=0。
~~~     如果同时设置 replace_query = 1 和 'on_duplicate_clause',则会抛出异常。
~~~     此时,简单的 WHERE 子句(例如 =, !=, >, >=, <, <=)是在 MySQL 服务器上执行。
~~~     其余条件以及 LIMIT 采样约束语句仅在对MySQL的查询完成后才在ClickHouse中执行。
~~~     MySQL 引擎不支持 可为空 数据类型,
~~~     因此,当从MySQL表中读取数据时,NULL 将转换为指定列类型的默认值(通常为0或空字符串)。
二、ClickHouse链接mysql案例
### --- 在mysql下查看表并查看数据

~~~     # 进入mysql数据库
[root@hadoop03 ~]# mysql -uroot -p
~~~     # 查看库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| bigdata            |
+--------------------+
~~~     # 查看表并查看里面的数据

mysql> use bigdata;
mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  6 | jack  |   28 |
|  5 | lucas |   18 |
+----+-------+------+
### --- 在ClickHouse下创建MySQL链接表

~~~     # MySQL('192.168.1.123:3306数据库地址+端口', 注意mysql大小写不可以有错误
~~~     'bigdata',                  // 库
~~~     'student',                  // 表 
~~~     'root',                     // 用户名
~~~     '12345678')                 // 密码
~~~     # 创建ClickHouse链接MySQL表

hadoop01 :) CREATE TABLE mysql_table2 ( `id` UInt32, `name` String, `age` UInt32 ) ENGINE = MySQL('192.168.1.123:3306', 'bigdata', 'student', 'root', '12345678');

CREATE TABLE mysql_table2
(
    `id` UInt32,
    `name` String,
    `age` UInt32
)
ENGINE = MySQL('192.168.1.123:3306', 'bigdata', 'student', 'root', '12345678')

Ok.
### --- 查看ClickHouse链接mysql表是否成功

~~~     # 在ClickHouse下查看链接的mysql表数据
hadoop01 :) SELECT *FROM mysql_table2;

SELECT *
FROM mysql_table2

┌─id─┬─name──┬─age─┐
│  6 │ jack  │  28 │
│  5 │ lucas │  18 │
└────┴───────┴─────┘

                 
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor
 

相关