开发手册 欢迎您!
软件开发者资料库

Apache Presto - MySQL连接器

Apache Presto MySQL连接器 - 从简单和简单的步骤学习Apache Presto,从基本到高级概念,包括概述,体系结构,安装,配置设置,管理工具,基本SQL操作,SQL函数,MySQL,JMX,HIVE,KAFKA连接器, JDBC接口,自定义函数应用程序。

MySQL连接器用于查询外部MySQL数据库.

先决条件

MySQL服务器安装.

配置设置

希望您已在计算机上安装了mysql服务器.要在Presto服务器上启用mysql属性,必须在"etc/catalog"目录中创建文件"mysql.properties".发出以下命令来创建一个mysql.properties文件.

$ cd etc $ cd catalog $ vi mysql.properties   connector.name = mysql connection-url = jdbc:mysql://localhost:3306 connection-user = root connection-password = pwd

保存文件并退出终端.在上面的文件中,您必须在connection-password字段中输入您的mysql密码.

在MySQL服务器中创建数据库

打开MySQL服务器并创建一个数据库使用以下命令.

create database tutorials

现在你在服务器中创建了"教程"数据库.要启用数据库类型,请在查询窗口中使用命令"use tutorials".

创建表

让我们在"教程"上创建一个简单的表格数据库.

create table author(auth_id int not null, auth_name varchar(50),topic varchar(100))

插入表格

创建表格后,使用以下查询插入三条记录.

insert into author values(1,'Doug Cutting','Hadoop') insert into author values(2,’James Gosling','java') insert into author values(3,'Dennis Ritchie’,'C')

选择记录

检索所有记录,键入以下查询.

查询

select * from author

结果

auth_id    auth_name      topic  1        Doug Cutting     Hadoop 2        James Gosling    java 3        Dennis Ritchie     C

截至目前,您已使用MySQL服务器查询数据.让我们将Mysql存储插件连接到Presto服务器.

连接Presto CLI

在Presto CLI上输入以下命令连接MySql插件.

./presto --server localhost:8080 --catalog mysql --schema tutorials

您将收到以下回复.

presto:tutorials>

这里"教程"指的是mysql服务器中的模式.

列表模式

要列出mysql中的所有模式,请在Presto服务器中键入以下查询.

查询

presto:tutorials> show schemas from mysql;

结果

      Schema --------------------  information_schema  performance_schema  sys  tutorials

从这个结果中,我们可以将前三个模式结束为预定义,最后一个模式由您自己创建.

模式中的列表

以下查询列出了教程模式中的所有表.

查询

presto:tutorials> show tables from mysql.tutorials;

结果

  Table --------  author

我们在这个模式中只创建了一个表.如果您创建了多个表,它将列出所有表.

描述表

要描述表字段,请键入以下查询.

查询

presto:tutorials> describe mysql.tutorials.author;

结果

  Column   |     Type     | Comment -----------+--------------+---------  auth_id   | integer      |  auth_name | varchar(50)  |  topic     | varchar(100) |

显示表中的列

查询

presto:tutorials> show columns from mysql.tutorials.author;

结果

 Column    |     Type     | Comment -----------+--------------+---------  auth_id   | integer      |  auth_name | varchar(50)  |  topic     | varchar(100) |

访问表记录

要从mysql表中获取所有记录,请发出以下查询.

查询

presto:tutorials> select * from mysql.tutorials.author;

结果

auth_id  |   auth_name    | topic ---------+----------------+--------        1 | Doug Cutting   | Hadoop        2 | James Gosling  | java        3 | Dennis Ritchie | C

从这个结果中,您可以在Presto中检索mysql服务器记录.

使用命令创建表

Mysql连接器不支持创建表查询,但您可以使用as命令创建表.

查询

presto:tutorials> create table mysql.tutorials.sample as select * from mysql.tutorials.author;

结果

CREATE TABLE: 3 rows

您无法直接插入行,因为此连接器有一些限制.它不能支持以下查询 :

  • 创建

  • insert

  • 更新

  • 删除

  • drop

至查看新创建的表中的记录,键入以下查询.

查询

presto:tutorials> select * from mysql.tutorials.sample;

结果

auth_id  |   auth_name    | topic ---------+----------------+--------        1 | Doug Cutting   | Hadoop        2 | James Gosling  | java        3 | Dennis Ritchie | C