All Products
Search
Document Center

Tablestore:Configure a Presto server and execute sample SQL statements

Last Updated:Jan 31, 2024

When you connect PrestoDB to Tablestore, you must complete the catalog and schema configurations of the Presto server based on your business requirements. After PrestoDB is connected to Tablestore, you can use common sample SQL statements to perform operations on Tablestore data. This topic describes how to configure a Presto server and provides sample SQL statements.

Configure a Presto server

Configure the catalog and schema of the Presto server based on your business requirements.

  • The catalog configurations include the configurations of a connector and the schema configuration mode. You can configure the schema based on a local static file. You can also dynamically configure the schema based on a metadata table.

  • The schema configurations mainly include the configurations of the local static file. If you use the dynamic configuration mode based on a metadata table, you do not need to configure a schema before you start the Presto server. You need to only create a schema and a mapping table after you run the SQL CLI.

Catalog configurations

The catalog configuration file contains the configurations of the connector and schema. The catalog configuration file is stored in the etc/catalog/tablestore.properties path of the directory in which PrestoDB is installed.

The following sample code provides an example of a catalog configuration file:

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

The following table describes the parameters.

Parameter

Example

Required

Description

connector.name

tablestore

Yes

The name of the connector. Set the value to tablestore.

tablestore.schema-mode

meta-table

Yes

The schema configuration mode. Valid values:

  • meta-table: dynamically configures the schema based on a metadata table. If you use this mode, you must configure a metastore that is used to store the schema information.

  • file: configures the schema based on a local static file. If you use this mode, you must configure the full path of the local static file and the local static file.

tablestore.schema-file

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

Conditional

This parameter is required only if the tablestore.schema-mode parameter is set to file.

The full path of the local static file. For more information about how to configure a local static file, see the "Schema configurations" section of this topic.

tablestore.meta-instance

metastoreinstance

Conditional

This parameter is required only if the tablestore.schema-mode parameter is set to meta-table.

The Tablestore instance that is used to store metadata. Modify the value of this parameter based on your business requirements. For more information, see Instance.

Important

Make sure that the Tablestore instance that you configure exists in your Alibaba Cloud account.

tablestore.endpoint

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

Conditional

This parameter is required only if the tablestore.schema-mode parameter is set to meta-table.

The endpoint of the Tablestore instance that is used to store metadata. Modify the value of this parameter based on your business requirements. For more information, see Endpoints.

tablestore.accessid

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

Conditional

This parameter is required only if the tablestore.schema-mode parameter is set to meta-table.

The AccessKey ID and AccessKey secret of the Resource Access Management (RAM) user who has permissions to access the Tablestore instance that is used to store metadata.

tablestore.accesskey

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

Conditional

tablestore.meta-table

meta_table

Conditional

This parameter is required only if the tablestore.schema-mode parameter is set to meta-table.

The name of the Tablestore table that is used to store metadata. Modify the value of this parameter based on your business requirements.

tablestore.auto-create-meta-table

true

Conditional

This parameter is required only if the tablestore.schema-mode parameter is set to meta-table.

Specifies whether to automatically create a metadata table. Default value: true, which indicates that a metadata table is automatically created when you create a schema.

Schema configurations

You can configure the schema based on a local static file. You can also dynamically configure the schema based on a metadata table. We recommend that you use the dynamic configuration mode based on a metadata table.

Configure the schema in dynamic configuration mode based on a metadata table

  • If you use this mode, you do not need to configure a schema before you run the Presto server.

  • After you connect PrestoDB to Tablestore and run the SQL CLI, you need to execute theCREATE SCHEMA and CREATE TABLE statements to dynamically create a schema and a mapping table. For more information, see the Create a schema and Create a mapping table sections of this topic.

Configure the schema based on a local static file

The path of the local static file must be the same as the full path that is specified by the tablestore.schema-file parameter in the catalog configuration file.

You can create a file on the Presto server and configure the file based on the following sample code:

{
	"account" : {
		"accessId" : "xxxxxxxx", ----- <Required> The AccessKey ID of your Alibaba Cloud account or a RAM user. 
		"accessKey" : "xxxxxxxxxxxxxxx", ----- <Required> The AccessKey secret of your Alibaba Cloud account or a RAM user. 
	},
	"instances" : {
		"mydb" : { ----- <Required> The name of the schema in PrestoDB. 
			"instanceName" : "myinstance", ----- <Required> The name of the connected Tablestore instance. 
			"endpoint" : "http://myinstance.cn-hangzhou.ots.aliyuncs.com", ----- <Required> The endpoint of the connected Tablestore instance. 
			"tables": {
				"mytable" : { ----- <Required> The name of the table in PrestoDB. 
					"originName" : "SampleTable", ----- <Optional> The name of the Tablestore table that is mapped to the table in PrestoDB. The name is case-sensitive. If you do not specify this parameter, the name of the Tablestore table is the same as the name of the table in Presto. 
					"columns" : [  ----- <Required> The metadata of the table in PrestoDB. The metadata must contain all the primary key columns. The order of the primary key columns in the metadata must be the same as that in the table. 
						{"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"}
					]
				}
			}
		}
	}
}

Parameter

Required

Description

account

accessId

Yes

The AccessKey ID and AccessKey secret of your Alibaba Cloud account or a RAM user

accessKey

Yes

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

Yes

The JSON map configurations that contain the schema name and schema information.

Important

The schema name is not case-sensitive in PrestoDB.

<schema_info>

instanceName

Yes

The name of the Tablestore instance that is mapped to the schema. For more information, see Instance.

endpoint

Yes

The endpoint of the Tablestore instance. For more information, see Endpoints.

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

Yes

The list of tables that are mounted to PrestoDB.

Important

The value of the <table_name>parameter is not case-sensitive in PrestoDB.

<table_info>

originName

No

The name of the Tablestore table that is mapped to the table in PrestoDB.

  • If you do not specify this parameter, the connector automatically uses the actual name of the Tablestore table that is mapped to the table in PrestoDB.

  • If you specify this parameter, the connector uses the table name that you specify.

If the connector does not find the name of the Tablestore table that is mapped to the table in PrstoDB, you cannot read data from or write data to the table.

columns(list([<column_info>]))

Yes

The metadata of the table. The metadata must contain all the defined columns.

Important
  • The metadata of columns must contain all primary key columns of the Tablestore table. The names and order of the primary key columns in metadata must be the same as those in the Tablestore table.

  • If the metadata is invalid, errors are reported when you read data from or write data to the table.

<column_info>

name

Yes

The name of the column.

Important

The name of the column is not case-sensitive in PrestoDB.

type

Yes

The data types of the column in the table. PrestoDB supports only the following data types: BIGINT, VARCHAR, VARBINARY, DOUBLE, and BOOLEAN.

Important

The data types that are defined in PrestoDB must map to the data types in Tablestore.

originName

No

The name of the column in the Tablestore instance that is mapped to the schema in PrestoDB.

Common sample SQL statements

If you use PrestoDB to access Tablestore, you can execute common sample SQL statements to create schemas and mapping tables. You can also execute common sample SQL statements to perform operations on the schemas, mapping tables, and data.

Create a schema

Create a schema that is used to configure the information about the Tablestore instance to be accessed and authenticate user permissions.

The following sample SQL statement provides an example on how to create a schema named testdb that is used to access the Tablestore instance named myinstance.

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

The following table describes the parameters.

Parameter

Example

Required

Description

endpoint

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

Yes

The endpoint of the Tablestore instance. For more information, see Endpoints.

instance_name

myinstance

Yes

The name of the connected Tablestore instance. For more information, see Instance.

access_id

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

Yes

The AccessKey ID of your Alibaba Cloud account or a RAM user.

access_key

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

Yes

The AccessKey secret of your Alibaba Cloud account or a RAM user.

Create a mapping table

Create a mapping table that is mapped to a Tablestore table.

Important

When you create a mapping table, take note of the following items:

  • Make sure that the data types of columns in the mapping table are mapped to those in the Tablestore table. For more information, see the Data type mappings section of the "Access Tablestore by using PrtestoDB" topic.

  • The name of the mapping table must be the same as the actual name of the Tablestore table.

  • The table that is specified by the table_name parameter in the CREATE TABLE statement is mapped to the actual table in Tablestore. You can create multiple mapping tables for the same Tablestore table.

  • The mapping table must contain all primary key columns of the Tablestore table, but only specific attribute columns.

  • The names and order of the primary key columns in the mapping table must be the same as those in the Tablestore table. You can map each attribute column in the mapping table to a column name in the actual Tablestore table by specifying the origin_name parameter.

The following SQL statement provides an example on how to create a mapping table with the same name as the Tablestore table 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'
);

Perform operations on schemas

After you create schemas, you can query a list of schemas and use a schema based on your business requirements.

Query a list of schemas

Execute the following SQL statement to query a list of schemas:

show schemas;

Sample response:

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

Use a schema

Use a schema to perform subsequent operations on tables in the schema.

Important

You can perform operations on tables in a schema only after you use the schema.

The following SQL statement provides an example on how to use the testdb schema.

use testb;

Sample response:

USE

Perform operations on mapping tables

After you create mapping tables, you can query the tables in a specific schema, view the metadata of a specific table, and delete a mapping table.

Query the tables in a specific schema

Execute the following SQL statement to query the tables in a used schema:

show tables;

Sample response:

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

View the metadata of a specific table

The following SQL statement provides an example on how to view the metadata of the exampletable table.

describe exampletable;

Sample response:

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

View the description of a table

The following SQL statement provides an example on how to view the description of the exampletable table.

describe exmapletable;

Sample response:

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

Delete a mapping table

The following SQL statement provides an example on how to delete the mapping table named table1.

drop table table1;

Sample response:

DROP TABLE

Perform operations on data

After you create a mapping table for a Tablestore table, you can write data to the Tablestore table, as well as query and analyze data in the Tablestore table.

Important

You cannot use PrestoDB to update or delete data in a Tablestore table.

Write data

  • Insert a row of data

    The following SQL statement provides an example on how to insert a row of data to the exampletable table.

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

    Sample response:

    INSERT: 1 row
  • Import multiple data records at a time

    Important

    Before you import multiple data records at a time, make sure that a destination table is created and the schema of the destination table is the same as that of the source table.

    The following SQL statement provides an example on how to copy the rows whose value of the c1 column is less than 50 from the exampletable table to the sampletable table:

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

    Sample response:

    INSERT: 3 row

Read data

  • Query all data in a table

    The following SQL statement provides an example on how to read data from the exampletable table.

    select * from exampletable;

    Sample response:

    pk | c1 | c2 | c3   
    ----- +------+------+------+-------
     100 | 11.0 | Lily | 10001 
     101 | 22.0 | Mary | 10002  
     102 | 12.0 | Jim | 10003   
    (3 rows)
  • Query data that meets specified conditions in a table

    The following sample SQL statement provides an example on how to query the rows whose value of the c1 column is less than 15 and value of the c3 column is equal to 10001 in the exampletable table.

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

    Sample response:

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

Analyze data.

  • Calculate the average of values of a specific column in a table

    The following SQL statement provides an example on how to calculate the average of values of the c2 column in the exampletable table.

    select avg(c1) as Average from exampletable;

    Sample response:

    Average 
    ---------
        15.0 
    (1 row)
  • Calculate the number of rows in a table

    The following SQL statement provides an example on how to calculate the number of rows in the exampletable table.

    select count(*) as total from exampletable;

    Sample response:

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