将 PrestoDB 对接 Tablestore 前,需完成 Presto Server 的 Catalog 配置和 Schema 配置。对接完成后,可参考本文的 SQL 示例进行数据操作。
Server配置
根据需要对 Presto Server 进行 Catalog 配置和 Schema 配置。
Catalog 配置定义连接器设置和 Schema 配置模式。Schema 配置模式分为 Meta 表动态配置和本地静态文件配置两种。
使用 Meta 表动态配置时,启动 Presto Server 前无需配置任何 Schema 信息,只需在启动 SQL CLI 后通过 SQL 命令创建 Schema 和映射表。
Catalog配置
Catalog 配置文件位于 PrestoDB 安装目录下的etc/catalog/tablestore.properties。
配置示例如下:
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.schema-mode |
meta-table |
是 |
Schema 配置模式。取值如下:
|
|
tablestore.schema-file |
/users/test/tablestore/presto/tablestore.schema |
是,有条件 |
仅当 本地静态文件的完整路径。详见Schema配置。 |
|
tablestore.meta-instance |
metastoreinstance |
是,有条件 |
仅当 用于存储元数据的 Tablestore 实例,请根据实际修改。更多信息,请参见实例。 重要
请确保配置的实例已在阿里云账号中存在。 |
|
tablestore.endpoint |
http://metastoreinstance.cn-hangzhou,ots.aliyuncs.com/ |
是,有条件 |
仅当 用于存储元数据的 Tablestore 实例的访问地址,请根据实际修改。更多信息,请参见服务地址。 |
|
tablestore.accessid |
|
是,有条件 |
仅当 对元数据实例有访问权限的阿里云账号或RAM用户的AccessKey ID和AccessKey Secret。 |
|
tablestore.accesskey |
|
是,有条件 |
|
|
tablestore.meta-table |
meta_table |
是,有条件 |
仅当 用于存储元数据的 Tablestore 表名,请根据实际修改。 |
|
tablestore.auto-create-meta-table |
true |
否 |
仅当 是否自动创建元数据表。默认值为 |
Schema配置
Schema 配置支持 Meta 表动态配置和本地静态文件配置两种方式,推荐使用 Meta 表动态配置。
Meta表动态配置
本地静态文件配置
本地静态文件的路径必须与 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_info> |
originName |
否 |
对应 Tablestore 中实际的表名称。
若 connector 在 Tablestore 中找不到对应的表,则无法对该表执行任何读写操作。 |
|
columns(list([<column_info>])) |
是 |
表的元数据,需包含所有定义的列。 重要
|
|
|
<column_info> |
name |
是 |
列名。 重要
列名在 PrestoDB 中大小写不敏感。 |
|
type |
是 |
列类型。PrestoDB 中映射到 Tablestore 列类型的支持类型为 Bigint、Varchar、Varbinary、Double 和 Boolean。 重要
PrestoDB 中定义的列类型必须与 Tablestore 中的列类型匹配。 |
|
|
originName |
否 |
对应 Tablestore 实例中的列名。 |
|
常用SQL示例
以下示例展示如何通过 PrestoDB 操作 Tablestore 数据。所有示例基于名为 testdb 的 Schema(连接到 Tablestore 实例 myinstance)和名为 exampletable 的映射表(表结构为 (pk varchar, c1 double, c2 varchar, c3 varchar))。
创建Schema
Schema 用于配置 Tablestore 实例的访问信息并进行鉴权。以下示例创建名为 testdb 的 Schema,用于访问 Tablestore 实例 myinstance。
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 中实际的表名称相同。
CREATE TABLE 语句中的
table_name用于映射到 Tablestore 中的实际表。可以为同一个 Tablestore 数据表创建多个不同名称的映射表。映射表必须包含数据表的所有主键列,但可以只包含部分属性列。
映射表中主键列的名称和顺序必须与 Tablestore 数据表中保持一致。每个属性列可通过
origin_name参数映射到 Tablestore 中实际的列名。
以下示例为 Tablestore 数据表 exampletable 创建同名映射表。
CREATE TABLE if not exists exampletable
(
pk varchar,
c1 double with (origin_name = 'col1'),
c2 varchar 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 后,才能对其中的表执行操作。
以下示例切换到 testdb Schema。
use testdb;
返回示例如下:
USE
映射表相关操作
创建映射表后,可查看指定 Schema 中的表列表、查看表的描述信息或删除映射表。
获取Schema中表列表
切换到 Schema 后,查看已创建的表列表。
show tables;
返回示例如下:
Table
-----------------
exampletable
sampletable
sampletabletest
table1
testtable
(5 rows)
查看表的描述信息
以下示例查看 exampletable 的描述信息。
describe exampletable;
返回示例如下:
Column | Type | Extra | Comment
--------+---------+-------+---------
pk | varchar | |
c1 | double | |
c2 | varchar | |
c3 | varchar | |
(4 rows)
删除映射表
以下示例删除映射表 table1。
drop table table1;
返回示例如下:
DROP TABLE
数据操作
创建映射表后,可向 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中c1列的平均值。select avg(c1) as Average from exampletable;返回结果如下:
Average --------- 15.0 (1 row) -
统计表的总行数
以下示例统计
exampletable的总行数。select count(*) as total from exampletable;返回示例如下:
total ------- 3 (1 row)