All Products
Search
Document Center

AnalyticDB:High-performance Apache Spark fully encrypted computing engine

Last Updated:May 19, 2026

The high-performance edition of the Apache Spark fully encrypted computing engine in AnalyticDB for MySQL extends the basic edition with support for Parquet modular encryption. It is compatible with community editions of Spark, Hadoop, and Hive, boosting data processing efficiency while ensuring data is secure in transit and at rest. This topic shows you how to encrypt data and run SQL queries on encrypted tables using this engine.

Prerequisites

Prepare data

Data files for encryption must be in the Parquet format. You can download the Spark fully encrypted computing sample data to follow the steps in this topic.

Procedure

AnalyticDB for MySQL lets you encrypt plaintext data using the console or an encryption tool. If your data is stored locally, use the encryption tool. If your data is stored in a cloud database, use the console. The main differences are as follows:

  • Encrypt data using the console: Upload plaintext data to an OSS bucket and then encrypt it.

  • Encrypt data using an encryption tool: Encrypt data locally and then upload the ciphertext data to an OSS bucket.

Encrypt with console

  1. Upload the plaintext data from the Prepare data section to an OSS bucket. In this example, the path is oss://testBucketName/adb/Spark/customer. For more information, see Simple upload.

  2. 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.

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

  4. In the SQL Console window, select the Spark engine and your job resource group.

  5. Run the following statements to create an encrypted table.

    1. Enable encrypted computing, set the master keys, and create a database.

      -- Enable native computing.
      SET spark.adb.native.enabled=true;
      -- Configure resources.
      SET spark.driver.resourceSpec=medium;
      SET spark.executor.instances=2;
      SET spark.executor.resourceSpec=medium;
      SET spark.app.name=Spark SQL Encryption Test;
      -- Enable encrypted reads and writes, and set the master key list, KMS client, and CryptoFactory. After these settings are enabled, the engine supports both plaintext and ciphertext.
      SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
      SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
      SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
      -- Create a database.
      CREATE database IF NOT EXISTS adb_external_db;

      Parameters:

      Parameter

      Description

      spark.hadoop.parquet.encryption.key.list

      A list of master keys. Each master key is mapped to a key ID. Use commas (,) to separate master keys and a colon (:) to separate a key ID from its key. The format is <Master Key ID 1>:<Base64-encoded Master Key 1>,<Master Key ID 2>:<Base64-encoded Master Key 2>. For more information, see Introduction to keys.

      Example: kf:MDEyMzQ1Njc4OTAxMjdy****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****

      Warning

      You can use common tools like OpenSSL to generate random master keys. The master key is the root credential for accessing encrypted data. If you lose the key, you will not be able to access your data. Store your master keys securely.

      spark.hadoop.parquet.encryption.kms.client.class

      The KMS client class name. Set this parameter to io.glutenproject.encryption.InMemoryKMS.

      spark.hadoop.parquet.crypto.factory.class

      The CryptoFactory class name. Set this parameter to org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory.

    2. Create an external table named customer to store the plaintext data. The LOCATION specifies the OSS path of the plaintext data. In this example, the path is oss://testBucketName/adb/Spark/customer.

      SET spark.adb.native.enabled=true;
      SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
      SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
      SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
      CREATE TABLE IF NOT EXISTS adb_external_db.customer 
       (
          c_custkey long,
          c_name       string,
          c_address    string,
          c_nationkey long,
          c_phone      string,
          c_acctbal    decimal(12, 2),
          c_mktsegment string,
          c_comment    string
      )
      USING parquet 
      LOCATION 'oss://testBucketName/adb/Spark/customer';
      Note
      • If the adb_external_db database already contains plaintext tables, you can skip this step.

      • If your data is stored in a different cloud database, create a corresponding external table. For the syntax, see CREATE EXTERNAL TABLE.

    3. Create an external table named enc_customer to store the ciphertext data. In this example, the data for the enc_customer external table is stored at oss://testBucketName/adb/Spark/enc_customer.

      SET spark.adb.native.enabled=true;
      SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
      SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
      SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
      CREATE TABLE IF NOT EXISTS adb_external_db.enc_customer
      USING Parquet
      OPTIONS (
       'parquet.encryption.column.keys'='kc1:c_name;kc2:c_phone',
       'parquet.encryption.footer.key'='kf'
      )
      LOCATION 'oss://testBucketName/adb/Spark/enc_customer'
      AS
      SELECT *
      FROM adb_external_db.customer;

      Parameters:

      Parameter

      Required

      Description

      parquet.encryption.column.keys

      Yes

      Specifies which columns to encrypt and which master keys to use. Use a colon (:) to separate a key ID from its column names, commas (,) for multiple column names, and semicolons (;) for different key-column pairs.

      parquet.encryption.footer.key

      Yes

      The key for encrypting the Parquet file footer, which contains metadata.

      Note

      The footer is a data structure at the end of a Parquet file. It typically stores metadata, such as the version number, group metadata, column metadata, and key metadata.

      Important

      The parquet.encryption.column.keys and parquet.encryption.footer.key parameters must be set together. Otherwise, the file is not encrypted.

    4. (Optional) Drop the external table customer.

      DROP TABLE IF EXISTS adb_external_db.customer;
      Important

      The DROP TABLE statement deletes the customer external table. Manually delete the corresponding metadata in OSS to prevent plaintext data leakage.

  6. Create the external table enc_customer_output, and write the SQL calculation results from the enc_customer table to the enc_customer_output external table. The data for the enc_customer_output external table is stored in oss://testBucketName/adb/Spark/enc_customer_output.

    SET spark.adb.native.enabled=true;
    SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
    SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
    SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
    CREATE TABLE IF NOT EXISTS adb_external_db.enc_customer_output
    USING Parquet
    OPTIONS (
     'parquet.encryption.column.keys'='kc1:c_name;kc2:c_phone',
     'parquet.encryption.footer.key'='kf'
    )
    LOCATION 'oss://testBucketName/adb/Spark/enc_customer_output'
    AS
    SELECT *
    FROM adb_external_db.enc_customer
    WHERE 
    c_custkey < 15;
  7. Decrypt the computation results.

    1. Create the external table customer_output. Decrypt the data from the enc_customer_output table and write it to the customer_output external table. The data of the customer_output external table is stored in oss://testBucketName/adb/Spark/customer_output.

      SET spark.adb.native.enabled=true;
      SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
      SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
      SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
      CREATE TABLE IF NOT EXISTS adb_external_db.customer_output
      USING Parquet
      LOCATION 'oss://testBucketName/adb/Spark/customer_output'
      AS
      SELECT *
      FROM adb_external_db.enc_customer_output;
    2. Query the data in the customer_output table.

      SELECT * FROM adb_external_db.customer_output;

Encrypt with tool

  1. Use an encryption tool to encrypt the locally stored plaintext data into a ciphertext dataset. For more information about the encryption tool, see Spark encryption tool.

    import org.apache.spark.sql.SparkSession
    import org.apache.spark.sql.functions._
    import org.apache.spark.SparkConf
    
    // Initialize a SparkSession and enter the encryption and decryption parameters.
    val conf = new SparkConf()
    .set("spark.hadoop.parquet.encryption.kms.client.class", "org.apache.parquet.crypto.keytools.mocks.InMemoryKMS")
    .set("spark.hadoop.parquet.encryption.key.list", "kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****")
    .set("spark.hadoop.parquet.crypto.factory.class", "org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory")
    
    val spark = SparkSession.builder().appName("SquareDataFrame").config(conf).getOrCreate()
    
    // Read the plaintext data from 'customer'.
    val df = spark.read.parquet("customer")
    // Encrypt the 'customer' plaintext data. The c_name column is encrypted with kc1, and the footer is encrypted with kf. The encrypted file is named 'enc_customer'.
    df.write
    .option("parquet.encryption.column.keys" , "kc1:c_name")
    .option("parquet.encryption.footer.key" , "kf")
    // Specify the local path for the ciphertext dataset.
    .parquet("enc_customer")

    Parameters:

    Parameter

    Required

    Description

    spark.hadoop.parquet.encryption.kms.client.class

    Yes

    The KMS client class name.

    • When you encrypt data locally, set this parameter to org.apache.parquet.crypto.keytools.mocks.InMemoryKMS.

    • When you create an encrypted table in the console, set this parameter to io.glutenproject.encryption.InMemoryKMS.

    spark.hadoop.parquet.encryption.key.list

    Yes

    A list of master keys. Each master key is mapped to a key ID. Use commas (,) to separate master keys and a colon (:) to separate a key ID from its key. The format is <Master Key ID 1>:<Base64-encoded Master Key 1>,<Master Key ID 2>:<Base64-encoded Master Key 2>. For more information, see Introduction to keys.

    Example: kf:MDEyMzQ1Njc4OTAxMjdy****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****

    Warning

    You can use common tools like OpenSSL to generate random master keys. The master key is the root credential for accessing encrypted data. If you lose the key, you will not be able to access your data. Store your master keys securely.

    spark.hadoop.parquet.crypto.factory.class

    Yes

    The CryptoFactory class name. Set this parameter to org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory.

    parquet.encryption.column.keys

    Yes

    Specifies which columns to encrypt and which master keys to use. Use a colon (:) to separate a key ID from its column names, commas (,) for multiple column names, and semicolons (;) for different key-column pairs.

    parquet.encryption.footer.key

    Yes

    The key for encrypting the Parquet file footer, which contains metadata.

    Note

    The footer is a data structure at the end of a Parquet file. It typically stores metadata, such as the version number, group metadata, column metadata, and key metadata.

  2. Upload the ciphertext dataset enc_customer.parquet to OSS. This topic uses oss://testBucketName/adb/Spark/enc_customer.parquet as an example. For more information, see Simple upload.

  3. Create the encrypted table.

    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 your job resource group.

    4. Run the following statements to create an encrypted table.

      1. Enable native computing and create a database.

        -- Enable native computing.
        SET spark.adb.native.enabled=true;
        -- Configure resources.
        SET spark.driver.resourceSpec=medium;
        SET spark.executor.instances=2;
        SET spark.executor.resourceSpec=medium;
        -- Enable encrypted reads and writes, and set the master key list, KMS client, and CryptoFactory. After these settings are enabled, the engine supports both plaintext and ciphertext.
        SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
        SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
        SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
        -- Create a database.
        CREATE DATABASE IF NOT EXISTS adb_external_db;
      2. Create the external table enc_customer. The LOCATION is the OSS path of the enc_customer ciphertext dataset. An example path is oss://testBucketName/adb/Spark/enc_customer.parquet.

        SET spark.adb.native.enabled=true;
        SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
        SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
        SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
        CREATE TABLE IF NOT EXISTS adb_external_db.enc_customer 
        USING parquet
        LOCATION  'oss://testBucketName/adb/Spark/enc_customer';
  4. Create the external table enc_customer_output, and write the SQL calculation results from the enc_customer table to the enc_customer_output external table. The data for the enc_customer_output external table is stored at oss://testBucketName/adb/Spark/enc_customer_output.

    SET spark.adb.native.enabled=true;
    SET spark.hadoop.parquet.encryption.key.list=kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****;
    SET spark.hadoop.parquet.encryption.kms.client.class=io.glutenproject.encryption.InMemoryKMS;
    SET spark.hadoop.parquet.crypto.factory.class=org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory;
    CREATE TABLE IF NOT EXISTS adb_external_db.enc_customer_output
    USING Parquet
    OPTIONS (
     'parquet.encryption.column.keys'='kc1:c_name;kc2:c_phone',
     'parquet.encryption.footer.key'='kf'
    )
    LOCATION 'oss://testBucketName/adb/Spark/enc_customer_output'
    AS
    SELECT *
    FROM adb_external_db.enc_customer
    WHERE 
    c_custkey < 15;
  5. Download and decrypt the ciphertext results.

    1. Download the ciphertext result from the OSS path oss://testBucketName/adb/Spark/enc_customer_output to your local machine. For more information, see Download a file.

    2. Decrypt the ciphertext dataset of the computation result, and save the decrypted file in customer_output.

      // Decrypt the ciphertext dataset.
      val conf = new SparkConf()
      .set("spark.hadoop.parquet.encryption.kms.client.class", "org.apache.parquet.crypto.keytools.mocks.InMemoryKMS")
      .set("spark.hadoop.parquet.encryption.key.list", "kf:MDEyMzQ1Njc4OTAxMjM0****,kc1:bvCDwqcOJGSdZSEMLjfk****,kc2:kflI/sq+uf50Qhl1MmtG****")
      .set("spark.hadoop.parquet.crypto.factory.class", "org.apache.parquet.crypto.keytools.PropertiesDrivenCryptoFactory")
      val spark = SparkSession.builder().appName("SquareDataFrame").config(conf).getOrCreate()
      val df2 = spark.read.parquet("enc_customer_output")
      // Save the decrypted file locally.
      df2.write
      .parquet("customer_output")