Use Tablestore with PrestoDB

更新时间:
复制 MD 格式

PrestoDB is an open source, distributed SQL query engine for big data that uses a massively parallel processing (MPP) architecture. It supports various data sources, such as MySQL, Elasticsearch, and Tablestore. After you connect PrestoDB to Tablestore, you can use PrestoDB to query, analyze, write, and import data in Tablestore.

Background information

PrestoDB is an open source, distributed SQL query engine for big data that uses an MPP architecture. It supports various data sources, such as MySQL, Elasticsearch, and Tablestore. PrestoDB can be used as a query tool, a data extract, transform, and load (ETL) tool, a load testing tool, and a unified query engine to meet data processing needs in different scenarios.

  • As a query tool, PrestoDB supports SQL queries for daily development and troubleshooting.

  • As a data ETL tool, PrestoDB supports data replication across tables or instances and data copying from disparate data sources.

  • As a load testing tool, PrestoDB supports batch data scanning or writing.

  • As a unified query engine within a data mid-end, PrestoDB supports connections to multiple disparate data sources.

To use Tablestore with PrestoDB, you must first establish a connection. After the connection is established, you can use PrestoDB to query, analyze, write, and import data in Tablestore.

Prerequisites

  • You have a server that runs Linux or macOS with the following software installed. This topic uses a Linux server as an example.

    Note

    If you do not have a Linux server, you can use an Elastic Compute Service (ECS) instance. For more information, see Create an instance using the wizard.

    • Java 8 (64-bit) and a Python 3 development environment are installed.

    • PrestoDB is installed.

    • Download the prestodb-tablestore-connector file that corresponds to your PrestoDB version. Upload and decompress the file in the plugin directory of your PrestoDB installation folder.

      The following table describes the compatibility between prestodb-tablestore-connector plugin versions and PrestoDB versions.

      Plugin version

      PrestoDB version

      Description

      prestodb-tablestore-connector-202401

      0.280

      First release. This plugin is compatible with PrestoDB 0.280 and later.

  • You have the required Tablestore information, such as the access account, instance details, and table information.

  • You have created an AccessKey for a Resource Access Management (RAM) user that has permissions to operate Tablestore. For more information, see Obtain an AccessKey pair.

    Note

    If the RAM user does not have the required permissions to operate Tablestore, you must grant the permissions before you proceed. For more information, see Grant permissions to a RAM user using a RAM policy.

Notes

  • This feature applies only to wide table models.

  • The default write mode for PrestoDB is UpdateRow. The PutRow write mode is not supported. If you want to write data to Tablestore using PrestoDB, make sure that the Allow Update parameter for the Tablestore data table is set to Yes. Otherwise, the data write operation will fail.

    Important
    • You cannot write data to a table using PrestoDB if the table uses a search index time to live (TTL). This is because you must set the Allow Update parameter to No when a search index TTL is used (the value is not -1).

    • To use the PutRow write mode, join DingTalk group 36165029092 (Tablestore Technical Support-3) to contact us.

    You can view the Allow Update setting on the Basic Details tab of the data table in the console or by calling the DescribeTable operation.

    image.png

Field type mapping

Tablestore and PrestoDB use different data types. The following table describes the mapping between the field types.

Tablestore field type

PrestoDB field type

string

varchar

integer

bigint

double

double

boolean

boolean

binary

varbinary

Procedure

To use Tablestore with PrestoDB, you need to connect PrestoDB to Tablestore, run the PrestoDB SQL command-line interface (CLI), create a schema and a mapping table, and then use SQL to operate on data.

Step 1: Connect PrestoDB to Tablestore

After you install PrestoDB, you must configure the PrestoDB catalog and schema to connect to Tablestore. The catalog configuration file is located at etc/catalog/tablestore.properties.

Configuration details

The Presto catalog configuration file includes settings for the connector, schema configuration mode, and other parameters. The connector must be set to `tablestore`. The schema can be configured using one of the following methods: local static file configuration or dynamic configuration with a meta table.

The following table describes the schema configuration modes.

Schema configuration mode

Description

Configuration flow

Local static file configuration

You must predefine Tablestore resource information, such as the access account, instance details, and mapping table metadata, in a static file.

Important

When you configure table metadata, you must include all primary key columns. The order of the primary key columns must match the order of the primary keys in the Tablestore table.

When you use PrestoDB to access Tablestore, you can access only the tables in the instances that are configured in the static file.

Before you run PrestoDB, perform the following steps:

  1. Set the schema mode to file and specify the full path of the static file.

  2. Define the access account, instance information, and table information in the static file.

(Recommended) Dynamic configuration using a meta table

You only need to configure the metastore information that is used to store the schema. You do not need to predefine Tablestore resource information.

You can dynamically create schemas and tables to define Tablestore resource information when you use PrestoDB to access Tablestore. When you create a schema, you must configure the account and instance information. When you create a table, you must configure the mapping table information.

  1. Before you run PrestoDB, set the schema mode to meta-table and configure the metastore information for storing the schema.

  2. After you run PrestoDB, use the create schema and create table statements to dynamically create the required schema and mapping table.

As shown in the table, the local static file configuration method requires you to predefine the table information. If table information is not configured in advance, you must manually add it before you can access the table. The dynamic configuration method that uses a meta table lets you dynamically create the required table information based on your business scenario when you use PrestoDB to access Tablestore. This method provides more flexibility. We recommend that you use the dynamic configuration using a meta table method.

Procedure

Specify the schema configuration mode in the catalog configuration file and configure the corresponding parameters. The following procedure describes how to configure the connection using the dynamic configuration method with a meta table.

  1. Go to the PrestoDB installation directory and create a tablestore.properties file in the etc/catalog/ directory.

    Important

    Before you edit the file, make sure that you have executable permissions on it.

    The following figure shows the directory where the catalog configuration file is located.

    image.png

  2. Edit the tablestore.properties file. Modify the configuration example based on your requirements and copy it to the file.

    When you use the dynamic configuration method with a meta table, set tablestore.schema-mode to meta-table.

    Note

    If you want to use the local static file configuration method, set tablestore.schema-mode to file and set tablestore.schema-file to the full path of the local static file. Then, configure the schema information in the static file. For more information, see Server configuration.

    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 configuration items.

    Configuration item

    Example

    Required

    Description

    connector.name

    tablestore

    Yes

    The name of the connector. You must set this parameter to `tablestore`.

    tablestore.schema-mode

    meta-table

    Yes

    The schema mode configuration. Set this parameter to meta-table.

    tablestore.meta-instance

    metastoreinstance

    Yes

    The Tablestore instance used to store metadata. Modify the value as needed. For more information, see Instances.

    Important

    Make sure the configured instance exists in your Alibaba Cloud account.

    tablestore.endpoint

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

    Yes

    The endpoint of the Tablestore instance used to store metadata. Modify the value as needed. For more information, see Endpoints.

    tablestore.accessid

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

    Yes

    The AccessKey ID and AccessKey secret of the user who has access permissions for the instance that stores metadata.

    tablestore.accesskey

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

    tablestore.meta-table

    meta_table

    Yes

    The name of the table in Tablestore used to store metadata. Modify the value as needed.

    tablestore.auto-create-meta-table

    true

    No

    Specifies whether to automatically create a metadata table. The default value is true, which means a metadata table is automatically created when a schema is created.

  3. Save the configuration file and exit.

    Note

    You can run the `cat` command to confirm that the configuration is saved.

Step 2: Run the Presto SQL CLI

After the connection is established, you can start the Presto Server and run SQL statements.

  1. Obtain the client executable file.

    1. Download the PrestoDB client.

      This topic uses presto-cli-0.280-executable.jar as an example.

    2. Save the PrestoDB client to the bin directory of the PrestoDB installation folder.

    3. In the bin directory of the PrestoDB installation folder, run the following command to obtain the client executable file.

      # Rename the PrestoDB client file to presto. Replace presto-cli-0.280-executable.jar with the name of your client version.
      mv presto-cli-0.280-executable.jar presto
      # Grant the user permissions to operate the presto file.
      chmod +x presto

      The following figure shows the directory where the client executable file is located.

      image.png

  2. In the bin directory of the PrestoDB installation folder, run the command to start the Presto Server.

    Important

    When you start the Presto Server, you must start the coordinator and at least one worker.

    # You can start the server in the foreground or background. The foreground mode is more convenient for viewing operational logs.
    # Method 1: Run in the background
    ./launcher start
    # Method 2: Run in the foreground
    ./launcher run
  3. In the bin directory of the PrestoDB installation folder, run the following command to start the SQL CLI.

    Note
    • By default, PrestoDB runs on port 8080. To change the port, modify the http-server.http.port setting in the etc/config.properties file.

    • After you modify configuration settings, you must restart the server for the changes to take effect.

    • In the command, ./presto is the relative path of the client executable file in the bin directory of the PrestoDB installation folder.

    ./presto --server localhost:8080 --catalog tablestore --schema default

    The following table describes the configuration items.

    Configuration item

    Example

    Required

    Description

    --server

    localhost:8080

    Yes

    The URI of the Presto Server. Replace the value as needed. This setting must be the same as the discovery.uri parameter in the etc/config.properties file in the PrestoDB installation directory.

    --catalog

    tablestore

    Yes

    The name of the catalog configuration file. Set this to `tablestore`, which is the same as the name of the created catalog configuration file.

    --schema

    default

    Yes

    The schema configuration. Keep the default value.

Step 3: Create a schema and a mapping table

When you use the dynamic schema configuration mode with a meta table, you must manually create a schema to configure the connected Tablestore instance and perform user authentication. Then, you must create a mapping table for the Tablestore table to query and analyze data.

Note

If you use the local static file schema configuration mode, you do not need to perform this step.

  1. Run the following command to create a schema.

    The following example shows how to create a Schema named testdb to access the Tablestore instance myinstance.

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

    For a description of the parameters, see the following table.

    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 Tablestore instance. For more information, see Instances.

    access_id

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

    Yes

    The AccessKey ID of the Alibaba Cloud account or RAM user.

    access_key

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

    Yes

    The AccessKey secret of the Alibaba Cloud account or RAM user.

  2. Run the use <SCHEMA_NAME>; command to use the schema that you created.

    Replace <SCHEMA_NAME> with the name of the schema you created. You can run the show schemas; command to obtain a list of schemas.

  3. Run the following command to create a mapping table for the Tablestore data table.

    Important

    Note the following points when you create a mapping table:

    • Make sure the field types in the mapping table match the field types in the Tablestore data table. For more information, see Field type mapping.

    • The mapping table name must match the table name in Tablestore.

    • In the CREATE TABLE SQL statement, the table_name property maps the Presto table to the actual table in Tablestore. You can create multiple mapping tables with different names for the same Tablestore data table.

    • The mapping table must include all primary key columns of the data table, but it can include only some of the attribute columns.

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

    Assume that the data table is named main_table and includes two primary key columns, gid and uid, and three attribute columns, col1, col2, and col3.

    The following SQL example creates a mapping table with the same name for the Tablestore data table main_table.

    CREATE TABLE if not exists main_table
    (
       gid bigint,
       uid bigint,
       c1 boolean with (origin_name = 'col1'),
       c2 bigint with (origin_name = 'col2'),
       c3 varchar with (origin_name = 'col3')
    ) WITH (
       table_name = 'main_table'
    );

Step 4: Use SQL to operate on data

Important

Before you use SQL to operate on Tablestore data, make sure that you have run the use <SCHEMA_NAME>; command to use the required schema.

You can use SQL statements to query schema configurations and table metadata, and to read and write data in Tablestore tables. For more SQL examples, see Common SQL examples.

  1. Query schema configurations.

    • Obtain a list of schemas

      show schemas;
    • Obtain a list of tables in a specified schema

      show tables;
  2. View the metadata of a specified table

    Replace <TABLE_NAME> with the actual table name.

    describe <TABLE_NAME>;
  3. Data operations.

    Read and write Tablestore data.

    • Write data

      • Insert a row of data

        The following example inserts a row of data into main_table.

        insert into main_table values(10001,10001,true,100,'hangzhou');
      • Import data in batches

        Important

        Before you import data in batches, make sure that the destination table is created and its table schema is the same as that of the source data table.

        The following example imports data from the gid, uid, c1, c2, and c3 columns of main_table into sampletable, where gid is greater than 0 and uid is less than 10000.

        insert into sampletable select gid, uid, c1, c2, c3 from main_table where gid > 0 and uid < 100000;
    • Read data

      The following example queries for rows in sampletable where the gid column value is greater than 0, the uid column value is less than 10, and c1 is true.

      select * from sampletable where gid > 0 and uid < 10 and c1 = true;

Billing

Tablestore supports two billing modes: VCU mode (formerly provisioned mode) and CU mode (formerly pay-as-you-go mode). For billing information, refer to the billing mode of your instance. For more information, see Billing overview.

  • VCU mode (formerly provisioned mode): Billable items include compute capacity, data storage, and outbound internet traffic. Data storage includes high-performance storage, storage-optimized storage, and search index storage.

    Accessing Tablestore with a compute engine consumes computing resources. The data storage that is used by the table that stores metadata depends on the storage type that was specified when the instance was created.

  • CU mode (formerly pay-as-you-go mode): Billable items include read throughput, write throughput, data storage, and outbound internet traffic.

    When you use a compute engine to access Tablestore, Tablestore meters and bills your usage based on the read and write throughput for specific read and write requests. The data storage that is used by the table that stores metadata depends on the storage type that was specified when the instance was created.

References