Apache Presto教程 - MySQL连接器

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

现在您已经在服务器中创建了“tutorials”数据库。要启用数据库类型,请在查询窗口中使用命令“use tutorials”。

创建表

让我们在“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> 

其中 “tutorials” 指的是mysql服务器中的schema。

列出schema

要列出mysql中的所有schema,请在Presto服务器中输入以下查询。

查询


presto:tutorials> show schemas from mysql;

结果


      Schema 
 
 information_schema 
 performance_schema 
 sys 
 tutorials

从这个结果中,我们可以得出前三个schema是预定义的,最后一个schema是由您自己创建的。

从Schema中列出表

以下查询将列出 “tutorials” schema中的所有表。

查询


presto:tutorials> show tables from mysql.tutorials; 

结果


  Table 
 
 author

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

描述表

要描述表字段,请输入以下查询。

查询


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服务器记录。

使用 as 命令创建表

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

查询


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

结果


CREATE TABLE: 3 rows

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

  • 创建
  • 插入
  • 更新
  • 删除
  • 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

原文链接 大专栏  https://www.dazhuanlan.com/2019/07/19/apache-presto%e6%95%99%e7%a8%8b-mysql%e8%bf%9e%e6%8e%a5%e5%99%a8/

猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11416194.html