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
An AnalyticDB for MySQL Enterprise Edition, Basic Edition, or Data Lakehouse Edition cluster is created.
An Object Storage Service (OSS) bucket is created in the same region as the AnalyticDB for MySQL cluster.
A database account is created for the AnalyticDB for MySQL cluster.
If you use an Alibaba Cloud account, you need to only create a privileged account.
If you use a Resource Access Management (RAM) user, you must create a privileged account and a standard account and associate the standard account with the RAM user.
-
The RAM user has the AliyunADBFullAccess, AliyunADBSparkProcessingDataRole, and read/write permissions for AnalyticDB for MySQL databases and tables. For more information, see Spark permission management.
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
-
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. 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.
-
In the left-side navigation pane, choose .
-
In the SQL Console window, select the Spark engine and your job resource group.
-
Run the following statements to create an encrypted table.
-
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****WarningYou 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. -
Create an external table named
customerto store the plaintext data. The LOCATION specifies the OSS path of the plaintext data. In this example, the path isoss://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_dbdatabase 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.
-
-
Create an external table named
enc_customerto store the ciphertext data. In this example, the data for theenc_customerexternal table is stored atoss://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.
NoteThe 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.
ImportantThe parquet.encryption.column.keys and parquet.encryption.footer.key parameters must be set together. Otherwise, the file is not encrypted.
-
(Optional) Drop the external table
customer.DROP TABLE IF EXISTS adb_external_db.customer;ImportantThe DROP TABLE statement deletes the
customerexternal table. Manually delete the corresponding metadata in OSS to prevent plaintext data leakage.
-
-
Create the external table
enc_customer_output, and write the SQL calculation results from theenc_customertable to theenc_customer_outputexternal table. The data for theenc_customer_outputexternal table is stored inoss://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; -
Decrypt the computation results.
-
Create the external table
customer_output. Decrypt the data from theenc_customer_outputtable and write it to thecustomer_outputexternal table. The data of thecustomer_outputexternal table is stored inoss://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; -
Query the data in the
customer_outputtable.SELECT * FROM adb_external_db.customer_output;
-
Encrypt with tool
-
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****WarningYou 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.
NoteThe 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.
-
-
Upload the ciphertext dataset
enc_customer.parquetto OSS. This topic usesoss://testBucketName/adb/Spark/enc_customer.parquetas an example. For more information, see Simple upload. -
Create the encrypted table.
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.
-
In the left-side navigation pane, choose .
-
In the SQL Console window, select the Spark engine and your job resource group.
-
Run the following statements to create an encrypted table.
-
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; -
Create the external table
enc_customer. The LOCATION is the OSS path of theenc_customerciphertext dataset. An example path isoss://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';
-
-
Create the external table
enc_customer_output, and write the SQL calculation results from theenc_customertable to theenc_customer_outputexternal table. The data for theenc_customer_outputexternal table is stored atoss://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; -
Download and decrypt the ciphertext results.
-
Download the ciphertext result from the OSS path
oss://testBucketName/adb/Spark/enc_customer_outputto your local machine. For more information, see Download a file. -
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")
-