Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read and write continously #49

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/commands/merge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::errors::PQRSError;
use crate::errors::PQRSError::{FileExists, FileNotFound};
use crate::utils::{check_path_present, get_row_batches, open_file, write_parquet};
use crate::utils::{check_path_present, get_row_batches, open_file};
use parquet::arrow::ArrowWriter;
use clap::Parser;
use arrow::datatypes::Schema;
use log::debug;
use std::ops::Add;
use std::fs::File;
use std::sync::Arc;
use std::path::PathBuf;

/// Merge file(s) into another parquet file
Expand Down Expand Up @@ -34,16 +37,40 @@ pub(crate) fn execute(opts: MergeCommandArgs) -> Result<(), PQRSError> {
}
}

let seed = open_file(&opts.input[0])?;
let mut combined = get_row_batches(seed)?;
let mut writer = {
let seed = open_file(&opts.input[0])?;
let data = get_row_batches(seed)?;

let file = File::create(&opts.output)?;
let fields = data.schema.fields().to_vec();

let schema_without_metadata = Schema::new(fields);

let mut writer = ArrowWriter::try_new(file, Arc::new(schema_without_metadata), None)?;

for record_batch in data.batches.iter() {
writer.write(record_batch)?;
}

writer
};


for input in &opts.input[1..] {
let current = open_file(input)?;
let local = get_row_batches(current)?;
combined = combined.add(local);

// write record batches one at a time
// record batches are not combined
for record_batch in local.batches.iter() {
writer.write(record_batch)?;
}
}
// debug!("The combined data looks like this: {:#?}", combined);
// debug!("This is the input schema: {:#?}", combined.schema);
write_parquet(combined, &opts.output)?;

// closing the writer writes out the FileMetaData
// if the writer is not closed properly, the metadata footer needed by the parquet
// format would be corrupt
writer.close()?;

Ok(())
}
26 changes: 0 additions & 26 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::errors::PQRSError::CouldNotOpenFile;
use arrow::{datatypes::Schema, record_batch::RecordBatch};
use log::debug;
use parquet::arrow::{arrow_reader::ArrowReaderBuilder, ArrowWriter};

Check warning on line 5 in src/utils.rs

View workflow job for this annotation

GitHub Actions / job_on_push

unused import: `ArrowWriter`

Check warning on line 5 in src/utils.rs

View workflow job for this annotation

GitHub Actions / job_on_push

unused import: `ArrowWriter`
use parquet::file::reader::{FileReader, SerializedFileReader};
use parquet::record::Row;
use rand::seq::SliceRandom;
Expand All @@ -11,7 +11,7 @@
use std::fs::File;
use std::ops::Add;
use std::path::Path;
use std::sync::Arc;

Check warning on line 14 in src/utils.rs

View workflow job for this annotation

GitHub Actions / job_on_push

unused import: `std::sync::Arc`

Check warning on line 14 in src/utils.rs

View workflow job for this annotation

GitHub Actions / job_on_push

unused import: `std::sync::Arc`
use walkdir::DirEntry;

// calculate the sizes in bytes for one KiB, MiB, GiB, TiB, PiB
Expand Down Expand Up @@ -255,32 +255,6 @@
})
}

/// Write a parquet file to the output location based on the given parquet input
pub fn write_parquet<P: AsRef<Path>>(
data: ParquetData,
output: P,
) -> Result<(), PQRSError> {
let file = File::create(output)?;
let fields = data.schema.fields().to_vec();
// the schema from the record batch might not contain the file specific metadata
// drop the schema to make sure that we don't fail in that case
let schema_without_metadata = Schema::new(fields);

let mut writer = ArrowWriter::try_new(file, Arc::new(schema_without_metadata), None)?;

// write record batches one at a time
// record batches are not combined
for record_batch in data.batches.iter() {
writer.write(record_batch)?;
}

// closing the writer writes out the FileMetaData
// if the writer is not closed properly, the metadata footer needed by the parquet
// format would be corrupt
writer.close()?;
Ok(())
}

/// Print the given parquet rows in json or json-like format
fn print_row(row: &Row, format: Formats) {
match format {
Expand Down
Loading