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://: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 伺服器中的模式。

列出模式

要在 Presto 伺服器中列出所有 MySQL 模式,請鍵入以下查詢。

查詢

presto:tutorials> show schemas from mysql;

結果

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

從結果中,我們可以得出結論,前三個模式是預定義的,最後一個是您自己建立的。

列出模式中的表

以下查詢列出 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 伺服器記錄。

使用 AS 命令建立表

Mysql 聯結器不支援 create table 查詢,但您可以使用 as 命令建立表。

查詢

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

結果

CREATE TABLE: 3 rows

您不能直接插入行,因為此聯結器有一些限制。它不支援以下查詢:

  • 建立
  • 插入
  • 更新
  • 刪除
  • 刪除

要檢視新建立表中的記錄,請鍵入以下查詢。

查詢

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

結果

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

© . All rights reserved.