全部產品
Search
文件中心

Tablestore:Presto Server配置和SQL樣本

更新時間:Jun 30, 2024

配置PrestoDB對接Tablestore時,您需要根據實際進行Presto Server的Catalog配置和Schema配置。對接完成後,您可以參考常見SQL樣本進行資料操作。

Server配置

根據需要對Presto Server進行Catalog配置和Schema配置。

  • Catalog配置主要包括連接器、Schema配置模式等配置。其中Schema配置模式包括本地靜態檔案配置和Meta表動態配置兩種方式。

  • Schema配置主要包括本地靜態檔案配置。當使用Meta表動態配置的Schema配置模式時,在啟動Presto Server前無需進行Schema配置,只需要在運行SQL CLI後進行Schema和映射表的建立。

Catalog配置

Catalog設定檔中包括連接器、Schema配置等配置。設定檔儲存路徑為PrestoDB安裝目錄下的etc/catalog/tablestore.properties

Catalog設定檔的配置樣本如下:

connector.name=tablestore
tablestore.schema-mode=meta-table
#tablestore.schema-mode=file
tablestore.schema-file=/users/test/tablestore/presto/tablestore.schema
tablestore.meta-instance=metastoreinstance
tablestore.endpoint=http://metastoreinstance.cn-hangzhou,ots.aliyuncs.com/
tablestore.accessid=****************
tablestore.accesskey=**************************
tablestore.meta-table=meta_table
tablestore.auto-create-meta-table=true

具體配置項說明請參見下表。

配置項

樣本

是否必選

說明

connector.name

tablestore

連接器名稱。此項必須配置tablestore。

tablestore.schema-mode

meta-table

Schema模式配置。取值範圍如下:

  • meta-table:Meta表動態配置方式,需要配置用於儲存Schema的Metastore資訊。

  • file:本地靜態檔案配置方式,需要配置本地靜態檔案完整路徑和本地靜態檔案。

tablestore.schema-file

/users/test/tablestore/presto/tablestore.schema

是,有條件

只有配置當tablestore.schema-modefile時才需要配置此項。

本地靜態檔案完整路徑。具體靜態檔案配置請參見Schema配置

tablestore.meta-instance

metastoreinstance

是,有條件

只有當配置tablestore.schema-modemeta-table時才需要配置此項。

Tablestore中用於儲存中繼資料的執行個體,請根據實際修改。更多資訊,請參見執行個體

重要

請確保配置的執行個體已在阿里雲帳號中存在。

tablestore.endpoint

http://metastoreinstance.cn-hangzhou,ots.aliyuncs.com/

是,有條件

只有當配置tablestore.schema-modemeta-table時才需要配置此項。

Tablestore中用於儲存中繼資料的執行個體的訪問地址,請修改實際修改。更多資訊,請參見服務地址

tablestore.accessid

****************

是,有條件

只有當配置tablestore.schema-modemeta-table時才需要配置此項。

具有儲存中繼資料的執行個體存取權限的使用者AccessKey ID和AccessKey Secret。

tablestore.accesskey

**************************

是,有條件

tablestore.meta-table

meta_table

是,有條件

只有當配置tablestore.schema-modemeta-table時才需要配置此項。

Tablestore中用於儲存中繼資料的表名,請根據實際修改。

tablestore.auto-create-meta-table

true

只有當配置tablestore.schema-modemeta-table時才需要配置此項。

是否需要自動建立中繼資料表。預設值為true,表示建立Schema時會自動建立中繼資料表。

Schema配置

Schema配置模式支援本地靜態檔案配置和Meta表動態配置兩種配置方式。推薦使用Meta表動態配置方式進行Schema配置。

Meta表動態配置

  • 運行Presto Server前,無需配置任何Schema資訊。

  • 完成對接並運行SQL CLI後,需要通過create schemacreate table來動態建立Schema和映射表。更多資訊,請參見建立Schema建立映射表

本地靜態檔案配置

本地靜態檔案所在的路徑必須與Catalog配置中的tablestore.schema-file指定的檔案完整路徑一致。

您可以在伺服器中建立一個檔案,在檔案中進行如下配置:

{
	"account" : {
		"accessId" : "xxxxxxxx", ----- <必選>阿里雲帳號或RAM使用者的AccessKey ID。
		"accessKey" : "xxxxxxxxxxxxxxx", ----- <必選>阿里雲帳號或RAM使用者的AccessKey Secret。
	},
	"instances" : {
		"mydb" : { ----- <必選>PrestoDB中Schema名稱。
			"instanceName" : "myinstance", ----- <必選>Tablestore執行個體的名稱。
			"endpoint" : "http://myinstance.cn-hangzhou.ots.aliyuncs.com", ----- <必選>Tablestore執行個體的Endpoint。
			"tables": {
				"mytable" : { ----- <必選>PrestoDB中表的名稱。
					"originName" : "SampleTable", ----- <可選>對應tablestore中表的名稱(注意大小寫),如果不配置,則與Presto中表名一致。
					"columns" : [  ----- <必選> 表的中繼資料,必須包含所有的主鍵列,且主鍵列順序必須與表的主鍵順序一致。
						{"name" : "gid", "type" : "bigint"},
						{"name" : "uid", "type" : "bigint"},
						{"name" : "c1", "type" : "boolean", "originName" : "col1"},
						{"name" : "c2", "type" : "bigint", "originName" : "col2"},
						{"name" : "C3", "type" : "varchar", "originName" : "col3"}
					]
				},
				"anotherTable" : {
					"originName" : "sampleTable2",
					"columns" : [
						{"name" : "gid", "type" : "bigint"},
						{"name" : "uid", "type" : "bigint"},
						{"name" : "a", "type" : "varchar"},
						{"name" : "b", "type" : "varchar"},
						{"name" : "c", "type" : "boolean"},
						{"name" : "d", "type" : "bigint"},
						{"name" : "e", "type" : "varchar"}
					]
				}
			}
		}
	}
}

配置項

是否必選

說明

account

accessId

阿里雲帳號或者RAM使用者的AccessKey ID和AccessKey Secret

accessKey

instances(map(<schema_name> -> <schema_info>))

包含Schema名稱和Schema資訊的JSON Map配置。

重要

Schema名稱在PrestoDB中大小寫不敏感。

<schema_info>

instanceName

Schema對應到Tablestore中實際的執行個體名稱。更多資訊,請參見執行個體

endpoint

Tablestore執行個體的服務地址。更多資訊,請參見服務地址

tables(map(<table_name> -> <table_info>))

掛載到PrestoDB中的表列表。

重要

<table_name>在PrestoDB中大小寫不敏感。

<table_info>

originName

對應到Tablestore中實際的表名稱。

  • 如果未配置此參數,則connector會自動在Tablestore內找到與映射表名對應的Tablestore實際表名稱。

  • 如果配置了此參數,則connector會以配置的實際表名稱為準。

如果connector在Tablestore中未找到對應的表,則無法對該表進行任何讀寫操作。

columns(list([<column_info>]))

表的Meta資訊,需要包含所有定義的列。

重要
  • 列的定義必須要包含Tablestore表的所有主鍵列,並且主鍵列的名稱和順序與Tablestore表的實際定義一致。

  • 如果Meta資訊有誤,則之後對該表的讀寫操作均會報錯。

<column_info>

name

列的名稱。

重要

列名在PrestoDB中大小寫不敏感。

type

列的類型。目前PrestoDB中對應到Tablestore中的列類型僅支援Bigint、Varchar、Varbinary、Double和Boolean類型。

重要

PrestoDB中定義的列類型必須與Tablestore中的列類型相匹配。

originName

對應到Tablestore中執行個體的列名稱。

常用SQL樣本

使用PrestoDB動作表格儲存的SQL常見樣本包括建立Schema、建立映射表、Schema相關操作、映射表相關操作和資料操作。

建立Schema

建立Schema用於配置要訪問的Tablestore執行個體資訊以及進行使用者鑒權。

建立一個訪問Tablestore myinstance執行個體的Schema,Schema名稱為testdb。

CREATE SCHEMA tablestore.testdb
WITH (
  endpoint = 'https://myinstance.cn-hangzhou.ots.aliyuncs.com',
  instance_name = 'myinstance',
  access_id = '************************',
  access_key = '********************************'
);

具體參數配置說明請參見下表。

參數

樣本

是否必選

說明

endpoint

https://myinstance.cn-hangzhou.ots.aliyuncs.com

Tablestore執行個體的訪問地址。更多資訊,請參見服務地址

instance_name

myinstance

Tablestore執行個體名稱。更多資訊,請參見執行個體

access_id

************************

阿里雲帳號或者RAM使用者的AccessKey ID。

access_key

********************************

阿里雲帳號或者RAM使用者的AccessKey Secret。

建立映射表

建立映射表用於對應到Tablestore執行個體中的表,

重要

建立映射表時,請注意如下事項:

  • 請確保映射表中的欄位類型和表格Tablestore資料表中的欄位類型相匹配更多資訊,請參見欄位類型映射

  • 映射表名稱必須與Tablestore中實際的表名稱相同。

  • SQL語句CREATE TABLE中的table_name用於映射到Tablestore中實際的表。您可以為Tablestore中的同一個資料表建立多個不同的映射表。

  • 映射表中必須包括資料表的所有主鍵列,但是支援只包括部分屬性列。

  • 映射表中主鍵列的名稱和順序必須與Tablestore資料表中主鍵列的名稱和順序保持一致。映射表中每一個屬性列可通過指定 origin_name參數來映射到Tablestore中實際表內的列名。

以下SQL樣本用於為Tablestore資料表exampletable建立一個同名映射表。

CREATE TABLE if not exists exampletable
(
   pk varchar,
   c1 double with (origin_name = 'col1'),
   c2 varcha with (origin_name = 'col2'),
   c3 varchar with (origin_name = 'col3')
) WITH (
   table_name = 'exampletable'
);

Schema相關操作

建立Schema後,您可以根據需要執行擷取Schema列表和使用Schema操作。

擷取Schema列表

擷取已建立的Schema列表。

show schemas;

返回樣本如下:

       Schema       
--------------------
 information_schema 
 testdb             
 testdb1            
(3 rows)

使用Schema

使用Schema用於後續操作Schema中的表。

重要

只有使用Schema後才能操作Schema中的表。

以下樣本用於使用testdb Schema。

use testb;

返回樣本如下:

USE

映射表相關操作

建立映射表後,您可以根據需要執行擷取指定Schema中表列表、查看指定表的中繼資料和刪除映射表操作。

擷取Schema中表列表

使用Schema後,擷取已建立的表列表。

show tables;

返回樣本如下:

      Table      
-----------------
 exampletable    
 sampletable     
 sampletabletest 
 table1          
 testtable       
(5 rows)

查看錶的描述資訊

以下樣本用於查看exampletable的描述資訊。

describe exmapletable;

返回樣本如下:

Column |  Type   | Extra | Comment 
--------+---------+-------+---------
 pk     | varchar |       |         
 c1     | double  |       |         
 c2     | varchar |       |         
 c3     | varchar |       |         
(4 rows)

刪除映射表

以下樣本用於刪除映射表table1。

drop table table1;

返回樣本如下:

DROP TABLE

資料操作

建立映射表後,寫入資料到Tablestore表中以及查詢和分析Tablestore表中資料。

重要

當前不支援通過PrestoDB更新或者刪除Tablestore表中資料。

寫入資料

  • 插入一行資料

    以下樣本用於寫入一行資料到exampletable。

    insert into exampletable values('101', 22.0, 'Mary', '10002');

    返回樣本如下:

    INSERT: 1 row
  • 大量匯入資料

    重要

    大量匯入資料前,請確保已建立目標表,且目標表的表結構與來源資料表的表結構保持一致。

    以下樣本用於將exampletable中滿足c1小於50的行資料拷貝到sampletable表中。

    insert into sampletable select pk, c1, c2, c3 from exampletable where c1 < 50; 

    返回樣本如下:

    INSERT: 3 row

讀取資料

  • 查詢表中所有資料

    以下樣本用於讀取exampletable表中資料。

    select * from exampletable;

    返回樣本如下:

     pk  |  c1  |  c2  |  c3   
    -----+------+------+-------
     100 | 11.0 | Lily | 10001 
     101 | 22.0 | Mary | 10002  
     102 | 12.0 | Jim  | 10003   
    (3 rows)
  • 查詢表中滿足指定條件的資料

    以下樣本用於查詢exampletable表中滿足c1小於15且c3等於10001的行資料。

    select * from exampletable where c1 < 15 and c3 = '10001'; 

    返回樣本如下:

     pk  |  c1  |  c2  |  c3   
    -----+------+------+-------
     100 | 11.0 | Lily | 10001 
    (1 row)

分析資料

  • 計算表中指定列的平均值

    計算exampletable表中c2列的平均值。

    select avg(c1) as Average from exampletable;

    返回結果如下:

    Average 
    ---------
        15.0 
    (1 row)
  • 統計表的總行數

    以下樣本用於統計exampletable表的總行數。

    select count(*) as total from exampletable;

    返回樣本如下:

    total 
    -------
         3 
    (1 row)