All Products
Search
Document Center

AnalyticDB:Example: Use the basic Confidential Engine for Apache Spark

Last Updated:Oct 20, 2026

The Basic Edition of the AnalyticDB for MySQL Confidential Engine for Apache Spark encrypts sensitive data, ensuring that the data is transmitted and stored as ciphertext. Only users who possess the key can decrypt the data, which prevents data breaches. This topic uses an RDS for MySQL database as an example to demonstrate the complete process of using the Basic Edition of the Confidential Engine for Apache Spark: encrypting data, running computations on ciphertext, and decrypting the results.

Prerequisites

  • An AnalyticDB for MySQL Enterprise Edition, Basic Edition, or Data Lakehouse Edition cluster is created.

  • A database account is created for the AnalyticDB for MySQL cluster.

  • The RAM user has been granted the AliyunADBFullAccess and AliyunADBSparkProcessingDataRole permissions, and read and write permissions on AnalyticDB for MySQL databases and tables. For more information, see Account authorization.

  • You have installed a Java 8 development environment to run the client tool. This topic uses an ECS instance that runs Linux as an example.

Limitations

  • The adb-crypto-tool client tool can only encrypt or decrypt data in MySQL and PostgreSQL databases.

    Note

    The MySQL or PostgreSQL database can be a cloud database, a self-managed database, or a third-party cloud database.

  • The confidential engine does not support the Decimal data type or nested data types. You can use the Float or Double data type as an alternative.

  • For PostgreSQL databases, the confidential engine does not automatically remove trailing spaces from the Char(n) data type. Use the Varchar(n) type instead.

Data preparation

In this example, an RDS for MySQL database is named adb, and a table named customer is created in the database. The following is a sample statement:

CREATE DATABASE IF NOT EXIST adb;
CREATE TABLE IF NOT EXISTS adb.customer(
id int,
name varchar(1023),
address varchar(1023),
age int
);

Insert data into the customer table. The following are sample statements:

INSERT INTO customer (id,name,address,age) VALUES('1', 'james', 'beijing', '10');
INSERT INTO customer (id,name,address,age) VALUES('2', 'bond', 'beijing', '15');
INSERT INTO customer (id,name,address,age) VALUES('3', 'jack', 'shanghai', '20');
INSERT INTO customer (id,name,address,age) VALUES('4', 'lucy', 'hangzhou', '25');
INSERT INTO customer (id,name,address,age) VALUES('5', 'tom', 'qingdao','30');

Procedure

  1. Step 1: Install the encryption and decryption client. Download and install the adb-crypto-tool client tool.

  2. Step 2: Encrypt and upload data. Use the client tool to encrypt the data from a plaintext table into a ciphertext dataset and upload it to Object Storage Service (OSS).

  3. Step 3: Execute SQL computations on ciphertext. Use the SQL Console of the AnalyticDB for MySQL Confidential Engine for Apache Spark to run computations on the ciphertext. The results are stored as ciphertext in OSS.

  4. Step 4: Download and decrypt the ciphertext results. Download the ciphertext results to your local machine, and then use the client tool to decrypt them. The decrypted data is automatically uploaded to a plaintext table in your database.

Install the encryption and decryption client

  1. Download the client tool: adb-crypto-tool-1.0.4.zip.

  2. Decompress the adb-crypto-tool-1.0.4.zip package.

    unzip adb-crypto-tool-1.0.4.zip

    After you decompress the package, you will find the following three files and folders:

    • cryptoTool: The entry point of the encryption and decryption tool.

      Note

      The cryptoTool requires a bash environment. On a Windows operating system, you can use a tool such as Windows Subsystem for Linux (WSL) to run the tool.

    • demo.json: A sample configuration file.

    • dist: The dependency packages for the encryption and decryption tool.

Encrypt and upload data

  1. Prepare an encryption configuration file. You can create a new configuration file by referencing demo.json, or directly modify the table, jdbcUrl, username, and password parameters in the demo.json file. The example in this topic uses a new configuration file named encryption_config.json. An example is as follows:

    {
      "dataDir": "file:///tmp",
      "table": [
        "customer"
      ],
      "encAlgo": "AES_128_GCM",
      "mek": "00112233445566778899aabbccdd****",
      "jdbcUrl": "jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb",
      "username": "demo_user",
      "password": "demo_password",
      "saveMode": "Overwrite",
      "format":"EncBlocksSource"
    }

    The following table describes the parameters.

    Parameter

    Description

    dataDir

    The local path where the ciphertext dataset is stored.

    table

    The name of the table to encrypt. In this example, the table is customer.

    encAlgo

    The encryption and decryption algorithm for the ciphertext. Only AES_128_GCM is supported.

    mek

    The master encryption key (MEK) is used to encrypt a data encryption key (DEK). For more information, see Key introduction.

    The master encryption key must be a 16-byte hexadecimal string. You can generate the key by using the OpenSSL tool. An example is 00112233445566778899aabbccdd****.

    Warning
    • The master encryption key is the root credential for accessing encrypted data. If you lose the key, you can no longer access the data. You must keep the master encryption key secure.

    • The confidential engine does not provide a service for generating or backing up master encryption keys. You must enter a master encryption key in the required format.

    jdbcUrl

    The JDBC URL and name of the database. Only MySQL and PostgreSQL databases are supported. The JDBC URL must be in one of the following formats:

    • MySQL database: jdbc:mysql://ip:port/database_name

    • PostgreSQL database: jdbc:postgresql://ip:port/database_name

    In this example, the JDBC URL is jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb.

    userName

    The database account.

    password

    The password of the database account.

    saveMode

    Specifies the action to take if the destination path for the ciphertext dataset already exists. Valid values:

    • Append: Appends data.

    • ErrorIfExists: Reports an error and exits if the destination exists. You can view the error information in the logs.

    • Ignore: Aborts the write operation if the destination exists. You cannot view error information in the logs.

    • Overwrite: Overwrites existing data.

    format

    The format of the ciphertext. Only EncBlocksSource is supported.

  1. Run the client tool to encrypt data in the customer table of the adb database. The encrypted dataset is stored in the dataDir path specified in the configuration file.

    ./cryptoTool encrypt encryption_config.json
  2. Upload the customer folder containing the encrypted dataset to an OSS bucket. In this topic, the example path is oss://testBucketName/adb/Spark/customer. For more information, see Simple upload.

Execute SQL computations on ciphertext

  1. Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.

  2. In the left-side navigation pane, choose Job Development>SQL Development.

  3. In the SQL Console window, select the Spark engine and a job resource group.

  4. Run the following statements to perform SQL computations on the ciphertext.

    1. Enable confidential computing and set the master encryption key.

      -- Enable confidential computing.
      set spark.adb.tee.enabled=true;
      
      -- Set the master encryption key. This must match the MEK specified in the encryption configuration file.
      set spark.adb.tee.encryption.key=00112233445566778899aabbccdd****;
      
      -- Create a database to store the ciphertext and the computation results.
      CREATE DATABASE IF NOT EXISTS test_db;
    2. Use the fully encrypted engine to create the customer external table. LOCATION is the OSS path to the ciphertext dataset folder. An example is oss://testBucketName/adb/Spark/customer.

      set spark.adb.tee.enabled=true;
      set spark.adb.tee.encryption.key=00112233445566778899aabbccdd****;
      CREATE TABLE IF NOT EXISTS test_db.customer 
      USING EncBlocksSource 
      LOCATION  'oss://testBucketName/adb/Spark/customer';
    3. Create the external table customer_output to write the results of the SQL query on the customer table, storing the data in oss://testBucketName/adb/Spark/customer_output. The customer_output external table stores the results of the SQL query on the customer table.

      set spark.adb.tee.enabled=true;
      set spark.adb.tee.encryption.key=00112233445566778899aabbccdd****;
      CREATE TABLE IF NOT EXISTS test_db.customer_output
      USING EncBlocksSource
      LOCATION 'oss://testBucketName/adb/Spark/customer_output'
      AS
      SELECT *
      FROM test_db.customer
      WHERE id = 1;

Download and decrypt the ciphertext results

  1. Download the encrypted result from the OSS path oss://testBucketName/adb/Spark/customer_output to your local machine. For detailed instructions, see Download files.

  2. Prepare a decryption configuration file. The following example is a new configuration file named decryption_config.json.

    {
      "dataDir": "file:///tmp",
      "table": [
        "customer_output"
      ],
      "encAlgo": "AES_128_GCM",
      "outTblPrefix": "abc_",
      "mek": "00112233445566778899aabbccdd****",
      "jdbcUrl": "jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb",
      "username": "demo_user",
      "password": "demo_password",
      "saveMode": "Overwrite",
      "format":"EncBlocksSource"
    }

    The following table describes the parameters:

    Parameter

    Description

    dataDir

    The local path where the ciphertext dataset is stored.

    table

    The name of the folder that contains the ciphertext dataset.

    encAlgo

    The encryption and decryption algorithm for the ciphertext. Only AES_128_GCM is supported.

    outTblPrefix

    The prefix for the destination table that stores the decrypted data. The final table name is created by combining this prefix with the folder name of the ciphertext dataset.

    mek

    The master encryption key (MEK) is used to encrypt a data encryption key (DEK). For more information, see Key introduction.

    The master encryption key must be a 16-byte hexadecimal string. You can generate the key by using the OpenSSL tool. An example is 00112233445566778899aabbccdd****.

    Warning
    • The master encryption key is the root credential for accessing encrypted data. If you lose the key, you can no longer access the data. You must keep the master encryption key secure.

    • The confidential engine does not provide a service for generating or backing up master encryption keys. You must enter a master encryption key in the required format.

    jdbcUrl

    The JDBC URL and name of the database. Only MySQL and PostgreSQL databases are supported. The JDBC URL must be in one of the following formats:

    • MySQL database: jdbc:mysql://ip:port/database_name

    • PostgreSQL database: jdbc:postgresql://ip:port/database_name

    In this example, the JDBC URL is jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb.

    userName

    The database account.

    password

    The password of the database account.

    saveMode

    Specifies the action to take if the destination table for the decrypted data already exists. Valid values:

    • Append: Appends data.

    • ErrorIfExists: Reports an error and exits if the destination exists. You can view the error information in the logs.

    • Ignore: Aborts the write operation if the destination exists. You cannot view error information in the logs.

    • Overwrite: Overwrites existing data.

    format

    The format of the ciphertext. Only EncBlocksSource is supported.

  3. When you run client-side decryption, the decrypted data is automatically inserted into the adb database in RDS MySQL. The name of the destination table is determined by the folder name of the encrypted dataset and the outTblePrefix parameter. In this example, the table is named adb_customer_output.

    ./cryptoTool decrypt decryption_config.json
  4. Query the decrypted data in the RDS for MySQL database to view the plaintext results.

    SELECT * FROM abc_customer_output;

    Expected output:

    +--------+----------+----------+----------+
    |  id    |   name   |  address |   age    |
    +--------+----------+----------+----------+
    |   1    |  james   |  beijing |   10     |
    +--------+----------+----------+----------+