|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use datafusion::common::DataFusionError; |
| 19 | +use datafusion::config::TableParquetOptions; |
| 20 | +use datafusion::dataframe::{DataFrame, DataFrameWriteOptions}; |
| 21 | +use datafusion::logical_expr::{col, lit}; |
| 22 | +use datafusion::parquet::encryption::decrypt::FileDecryptionProperties; |
| 23 | +use datafusion::parquet::encryption::encrypt::FileEncryptionProperties; |
| 24 | +use datafusion::prelude::{ParquetReadOptions, SessionContext}; |
| 25 | +use tempfile::TempDir; |
| 26 | + |
| 27 | +#[tokio::main] |
| 28 | +async fn main() -> datafusion::common::Result<()> { |
| 29 | + // The SessionContext is the main high level API for interacting with DataFusion |
| 30 | + let ctx = SessionContext::new(); |
| 31 | + |
| 32 | + // Find the local path of "alltypes_plain.parquet" |
| 33 | + let testdata = datafusion::test_util::parquet_test_data(); |
| 34 | + let filename = &format!("{testdata}/alltypes_plain.parquet"); |
| 35 | + |
| 36 | + // Read the sample parquet file |
| 37 | + let parquet_df = ctx |
| 38 | + .read_parquet(filename, ParquetReadOptions::default()) |
| 39 | + .await?; |
| 40 | + |
| 41 | + // Show information from the dataframe |
| 42 | + println!( |
| 43 | + "===============================================================================" |
| 44 | + ); |
| 45 | + println!("Original Parquet DataFrame:"); |
| 46 | + query_dataframe(&parquet_df).await?; |
| 47 | + |
| 48 | + // Setup encryption and decryption properties |
| 49 | + let (encrypt, decrypt) = setup_encryption(&parquet_df)?; |
| 50 | + |
| 51 | + // Create a temporary file location for the encrypted parquet file |
| 52 | + let tmp_dir = TempDir::new()?; |
| 53 | + let tempfile = tmp_dir.path().join("alltypes_plain-encrypted.parquet"); |
| 54 | + let tempfile_str = tempfile.into_os_string().into_string().unwrap(); |
| 55 | + |
| 56 | + // Write encrypted parquet |
| 57 | + let mut options = TableParquetOptions::default(); |
| 58 | + options.crypto.file_encryption = Some((&encrypt).into()); |
| 59 | + parquet_df |
| 60 | + .write_parquet( |
| 61 | + tempfile_str.as_str(), |
| 62 | + DataFrameWriteOptions::new().with_single_file_output(true), |
| 63 | + Some(options), |
| 64 | + ) |
| 65 | + .await?; |
| 66 | + |
| 67 | + // Read encrypted parquet |
| 68 | + let ctx: SessionContext = SessionContext::new(); |
| 69 | + let read_options = |
| 70 | + ParquetReadOptions::default().file_decryption_properties((&decrypt).into()); |
| 71 | + |
| 72 | + let encrypted_parquet_df = ctx.read_parquet(tempfile_str, read_options).await?; |
| 73 | + |
| 74 | + // Show information from the dataframe |
| 75 | + println!("\n\n==============================================================================="); |
| 76 | + println!("Encrypted Parquet DataFrame:"); |
| 77 | + query_dataframe(&encrypted_parquet_df).await?; |
| 78 | + |
| 79 | + Ok(()) |
| 80 | +} |
| 81 | + |
| 82 | +// Show information from the dataframe |
| 83 | +async fn query_dataframe(df: &DataFrame) -> Result<(), DataFusionError> { |
| 84 | + // show its schema using 'describe' |
| 85 | + println!("Schema:"); |
| 86 | + df.clone().describe().await?.show().await?; |
| 87 | + |
| 88 | + // Select three columns and filter the results |
| 89 | + // so that only rows where id > 1 are returned |
| 90 | + println!("\nSelected rows and columns:"); |
| 91 | + df.clone() |
| 92 | + .select_columns(&["id", "bool_col", "timestamp_col"])? |
| 93 | + .filter(col("id").gt(lit(5)))? |
| 94 | + .show() |
| 95 | + .await?; |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
| 99 | + |
| 100 | +// Setup encryption and decryption properties |
| 101 | +fn setup_encryption( |
| 102 | + parquet_df: &DataFrame, |
| 103 | +) -> Result<(FileEncryptionProperties, FileDecryptionProperties), DataFusionError> { |
| 104 | + let schema = parquet_df.schema(); |
| 105 | + let footer_key = b"0123456789012345".to_vec(); // 128bit/16 |
| 106 | + let column_key = b"1234567890123450".to_vec(); // 128bit/16 |
| 107 | + |
| 108 | + let mut encrypt = FileEncryptionProperties::builder(footer_key.clone()); |
| 109 | + let mut decrypt = FileDecryptionProperties::builder(footer_key.clone()); |
| 110 | + |
| 111 | + for field in schema.fields().iter() { |
| 112 | + encrypt = encrypt.with_column_key(field.name().as_str(), column_key.clone()); |
| 113 | + decrypt = decrypt.with_column_key(field.name().as_str(), column_key.clone()); |
| 114 | + } |
| 115 | + |
| 116 | + let encrypt = encrypt.build()?; |
| 117 | + let decrypt = decrypt.build()?; |
| 118 | + Ok((encrypt, decrypt)) |
| 119 | +} |
0 commit comments