The MySQL connector is used to query an external MySQL database.
Prerequisites
MySQL server installation.
Configuration Settings
Hopefully you have installed mysql server on your machine. To enable mysql properties on Presto server, you must create a file “mysql.properties” in “etc/catalog” directory. Issue the following command to create a mysql.properties file.
$ cd etc
$ cd catalog
$ vi mysql.properties
connector.name = mysql
connection-url = jdbc:mysql://localhost:3306
connection-user = root
connection-password = pwd
Save the file and quit the terminal. In the above file, you must enter your mysql password in connection-password field.
Create Database in MySQL Server
Open MySQL server and create a database using the following command.
create database tutorials
Now you have created “tutorials” database in the server. To enable database type, use the command “use tutorials” in the query window.
Create Table
Let’s create a simple table on “tutorials” database.
create table author(auth_idintnotnull,auth_name varchar(50),topic varchar(100))
插入表
创建表后,使用以下查询插入三个记录。
insertintoauthor values(1,"Doug Cutting","Hadoop")insertintoauthor values(2,’JamesGosling","java")
insert into author values(3,"DennisRitchie’,"C")
选择记录
要检索所有记录,请键入以下查询。
查询
select*fromauthor
结果
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 schemasfrommysql;
结果
Schema
--------------------
information_schema
performance_schema
sys
tutorials
从这个结果,我们可以将前三个模式作为预定义结束,最后一个由你自己创建。
从Schema列表
以下查询列出了教程模式中的所有表。
查询
presto:tutorials>show tablesfrommysql.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 columnsfrommysql.tutorials.author;
结果
Column | Type | Comment
-----------+--------------+---------
auth_id | integer |
auth_name | varchar(50) |
topic | varchar(100) |
访问表记录
要从mysql表中获取所有记录,请发出以下查询。
查询
presto:tutorials>select*frommysql.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.sampleasselect*frommysql.tutorials.author;
结果
CREATE TABLE: 3 rows
您不能直接插入行,因为此连接器有一些限制。它不能支持以下查询 -
创建
插
更新
删除
下降
要查看新创建的表中的记录,请键入以下查询。
查询
presto:tutorials>select*frommysql.tutorials.sample;
结果
auth_id | auth_name | topic
---------+----------------+--------
1 | Doug Cutting | Hadoop
2 | James Gosling | java
3 | Dennis Ritchie | C