Created
August 6, 2025 07:26
-
-
Save rok/8a68066c51801458a3746772a2c736c5 to your computer and use it in GitHub Desktop.
[C++] Write uniformly encrypted table to parquet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| arrow::Status WriteUniformlyEncryptedFile() { | |
| std::vector<std::shared_ptr<arrow::Array>> arrays; | |
| std::shared_ptr<arrow::Table> _table; | |
| std::vector<std::shared_ptr<arrow::Field>> fields; | |
| const std::string KEY_LABEL = "kf"; | |
| const ::arrow::util::SecureString KEY_VALUE("0123456789abcdef"); | |
| std::string outFileName = "tbl.parquet"; | |
| std::filesystem::path exePath = std::filesystem::path("/home/somepath"); | |
| std::filesystem::path outPath = exePath / outFileName; | |
| ARROW_LOG(INFO) << outPath; | |
| // Create an integer field | |
| auto int_field = arrow::field("int_field", arrow::int32()); | |
| // Create a boolean field | |
| auto bool_field = arrow::field("bool_field", arrow::boolean()); | |
| // Add fields to the fields vector | |
| fields.push_back(int_field); | |
| fields.push_back(bool_field); | |
| // Create sample data for each field | |
| arrow::Int32Builder int_builder; | |
| arrow::BooleanBuilder bool_builder; | |
| // Example data: 3 rows | |
| ARROW_RETURN_NOT_OK(int_builder.Append(10)); | |
| ARROW_RETURN_NOT_OK(int_builder.Append(20)); | |
| ARROW_RETURN_NOT_OK(int_builder.Append(30)); | |
| ARROW_RETURN_NOT_OK(bool_builder.Append(true)); | |
| ARROW_RETURN_NOT_OK(bool_builder.Append(false)); | |
| ARROW_RETURN_NOT_OK(bool_builder.Append(true)); | |
| std::shared_ptr<arrow::Array> int_array; | |
| std::shared_ptr<arrow::Array> bool_array; | |
| ARROW_RETURN_NOT_OK(int_builder.Finish(&int_array)); | |
| ARROW_RETURN_NOT_OK(bool_builder.Finish(&bool_array)); | |
| // Add arrays to the arrays vector | |
| arrays.push_back(int_array); | |
| arrays.push_back(bool_array); | |
| auto schema = std::make_shared<arrow::Schema>(fields); | |
| std::string schema_string = schema->ToString(); | |
| _table = arrow::Table::Make(schema, arrays); | |
| parquet::WriterProperties::Builder prop_builder; | |
| prop_builder.compression(parquet::Compression::UNCOMPRESSED); | |
| parquet::FileEncryptionProperties::Builder file_encryption_builder(KEY_VALUE); | |
| auto file_encryption_properties = file_encryption_builder | |
| .footer_key_metadata(KEY_LABEL) | |
| ->build(); | |
| prop_builder.encryption(file_encryption_properties); | |
| prop_builder.enable_write_page_index(); | |
| auto writer_properties = prop_builder.build(); | |
| std::shared_ptr<arrow::io::FileOutputStream> outfile; | |
| PARQUET_ASSIGN_OR_THROW(outfile, arrow::io::FileOutputStream::Open(outPath)); | |
| PARQUET_THROW_NOT_OK(parquet::arrow::WriteTable(*_table, arrow::default_memory_pool(), outfile, parquet::DEFAULT_MAX_ROW_GROUP_LENGTH, writer_properties)); | |
| PARQUET_THROW_NOT_OK(outfile->Flush()); | |
| PARQUET_THROW_NOT_OK(outfile->Close()); | |
| return ::arrow::Status::OK(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment