|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Firelink Data |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +// File created: 2024-05-07 |
| 25 | +// Last updated: 2024-05-15 |
| 26 | +// |
| 27 | + |
| 28 | +use crate::chunked::trimmer::{ColumnTrimmer, trimmer_factory}; |
| 29 | +use arrow::array::{ArrayRef, BooleanBuilder, Int32Builder, Int64Builder, StringBuilder}; |
| 30 | +use arrow::record_batch::RecordBatch; |
| 31 | +use log::{debug, info}; |
| 32 | +use ordered_channel::bounded; |
| 33 | +use parquet::arrow::ArrowWriter; |
| 34 | +use parquet::basic::Compression; |
| 35 | +use parquet::file::properties::WriterProperties; |
| 36 | +use parquet::format; |
| 37 | +use rayon::iter::IndexedParallelIterator; |
| 38 | +use rayon::prelude::*; |
| 39 | + |
| 40 | +use std::fs; |
| 41 | +use std::fs::File; |
| 42 | +use std::path::PathBuf; |
| 43 | +use std::str::from_utf8_unchecked; |
| 44 | +use std::sync::Arc; |
| 45 | + |
| 46 | +use super::{arrow_file_output, ColumnBuilder, Converter, FnFindLastLineBreak, FnLineBreakLen, Stats, trimmer}; |
| 47 | +use crate::datatype::DataType; |
| 48 | +use crate::schema; |
| 49 | +use crate::{chunked}; |
| 50 | +use arrow::datatypes::{Field, Schema, SchemaRef}; |
| 51 | +use atomic_counter::{AtomicCounter, ConsistentCounter}; |
| 52 | +use crossbeam::atomic::AtomicConsume; |
| 53 | +use libc::bsearch; |
| 54 | +use ordered_channel::Sender; |
| 55 | +use parquet::errors::{ParquetError, Result}; |
| 56 | +use parquet::file::metadata::FileMetaData; |
| 57 | +use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; |
| 58 | +use std::sync::mpsc::{sync_channel, Receiver, RecvError, SyncSender}; |
| 59 | +use std::thread; |
| 60 | +use std::thread::JoinHandle; |
| 61 | +use std::time::{Duration, Instant}; |
| 62 | +use thread::spawn; |
| 63 | +use arrow_ipc::CompressionType; |
| 64 | +use arrow_ipc::writer::IpcWriteOptions; |
| 65 | +use Compression::SNAPPY; |
| 66 | + |
| 67 | +pub(crate) struct parquet_file_out { |
| 68 | + pub(crate) sender: Option<Sender<RecordBatch>>, |
| 69 | +} |
| 70 | + |
| 71 | +impl arrow_file_output for parquet_file_out { |
| 72 | + fn setup(&mut self, schema: SchemaRef, outfile: PathBuf) -> (Sender<RecordBatch>, JoinHandle<Result<Stats>>) { |
| 73 | + |
| 74 | + let _out_file = fs::OpenOptions::new() |
| 75 | + .create(true) |
| 76 | + .append(true) |
| 77 | + .open(outfile) |
| 78 | + .expect("aaa"); |
| 79 | + |
| 80 | + let props = WriterProperties::builder() |
| 81 | + .set_compression(SNAPPY) |
| 82 | + .build(); |
| 83 | + |
| 84 | + let mut writer: ArrowWriter<File> = |
| 85 | + ArrowWriter::try_new(_out_file, schema, Some(props.clone())).unwrap(); |
| 86 | + |
| 87 | + |
| 88 | + let (sender, mut receiver) = bounded::<RecordBatch>(100); |
| 89 | + |
| 90 | + let t: JoinHandle<Result<Stats>> = thread::spawn(move || { |
| 91 | + 'outer: loop { |
| 92 | + let mut message = receiver.recv(); |
| 93 | + |
| 94 | + match message { |
| 95 | + Ok(rb) => { |
| 96 | + writer.write(&rb).expect("Error Writing batch"); |
| 97 | + if (rb.num_rows() == 0) { |
| 98 | + break 'outer; |
| 99 | + } |
| 100 | + } |
| 101 | + Err(e) => { |
| 102 | + info!("got RecvError in channel , break to outer"); |
| 103 | + break 'outer; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + info!("closing the writer for parquet"); |
| 108 | + writer.finish(); |
| 109 | + Ok(Stats { |
| 110 | + bytes_in: 0, |
| 111 | + bytes_out: 0, |
| 112 | + num_rows: 0, |
| 113 | + read_duration: Default::default(), |
| 114 | + parse_duration: Default::default(), |
| 115 | + builder_write_duration: Default::default(), |
| 116 | + }) |
| 117 | + }); |
| 118 | + (sender,t) |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +pub struct ipc_file_out { |
| 124 | + pub(crate) sender: Option<Sender<RecordBatch>>, |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +impl arrow_file_output for ipc_file_out { |
| 129 | + fn setup(&mut self, schema: SchemaRef, outfile: PathBuf) -> (Sender<RecordBatch>, JoinHandle<Result<Stats>>) { |
| 130 | + |
| 131 | + let _out_file = fs::OpenOptions::new() |
| 132 | + .create(true) |
| 133 | + .append(true) |
| 134 | + .open(outfile) |
| 135 | + .expect("aaa"); |
| 136 | + |
| 137 | + |
| 138 | + let p= IpcWriteOptions::try_with_compression(Default::default(),Some(CompressionType::LZ4_FRAME) ); |
| 139 | + |
| 140 | + let mut writer=arrow_ipc::writer::FileWriter::try_new_with_options(_out_file, &schema, Default::default()).expect("TODO: panic message"); |
| 141 | + |
| 142 | + let props = WriterProperties::builder() |
| 143 | + .set_compression(SNAPPY) |
| 144 | + .build(); |
| 145 | + |
| 146 | + let (sender, mut receiver) = bounded::<RecordBatch>(1000); |
| 147 | + |
| 148 | + let t: JoinHandle<Result<Stats>> = thread::spawn(move || { |
| 149 | + 'outer: loop { |
| 150 | + let mut message = receiver.recv(); |
| 151 | + |
| 152 | + match message { |
| 153 | + Ok(rb) => { |
| 154 | + writer.write(&rb).expect("Error Writing batch"); |
| 155 | + if (rb.num_rows() == 0) { |
| 156 | + break 'outer; |
| 157 | + } |
| 158 | + } |
| 159 | + Err(e) => { |
| 160 | + info!("got RecvError in channel , break to outer"); |
| 161 | + break 'outer; |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + info!("closing the writer for parquet"); |
| 166 | + writer.finish(); |
| 167 | + Ok(Stats { |
| 168 | + bytes_in: 0, |
| 169 | + bytes_out: 0, |
| 170 | + num_rows: 0, |
| 171 | + read_duration: Default::default(), |
| 172 | + parse_duration: Default::default(), |
| 173 | + builder_write_duration: Default::default(), |
| 174 | + }) |
| 175 | + }); |
| 176 | + (sender,t) |
| 177 | + } |
| 178 | +} |
0 commit comments