parquet/file/
mod.rs

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//! APIs for reading parquet data.
19//!
20//! Provides access to file and row group readers and writers, record API, metadata, etc.
21//!
22//! # See Also:
23//! * [`SerializedFileReader`] and [`SerializedFileWriter`] for reading / writing parquet
24//! * [`metadata`]: for working with metadata such as schema
25//! * [`statistics`]: for working with statistics in metadata
26//!
27//! [`SerializedFileReader`]: serialized_reader::SerializedFileReader
28//! [`SerializedFileWriter`]: writer::SerializedFileWriter
29//!
30//! # Example of writing a new file
31//!
32//! ```rust,no_run
33//! use std::{fs, path::Path, sync::Arc};
34//!
35//! use parquet::{
36//!     file::{
37//!         properties::WriterProperties,
38//!         writer::SerializedFileWriter,
39//!     },
40//!     schema::parser::parse_message_type,
41//! };
42//!
43//! let path = Path::new("/path/to/sample.parquet");
44//!
45//! let message_type = "
46//!   message schema {
47//!     REQUIRED INT32 b;
48//!   }
49//! ";
50//! let schema = Arc::new(parse_message_type(message_type).unwrap());
51//! let file = fs::File::create(&path).unwrap();
52//! let mut writer = SerializedFileWriter::new(file, schema, Default::default()).unwrap();
53//! let mut row_group_writer = writer.next_row_group().unwrap();
54//! while let Some(mut col_writer) = row_group_writer.next_column().unwrap() {
55//!     // ... write values to a column writer
56//!     col_writer.close().unwrap()
57//! }
58//! row_group_writer.close().unwrap();
59//! writer.close().unwrap();
60//!
61//! let bytes = fs::read(&path).unwrap();
62//! assert_eq!(&bytes[0..4], &[b'P', b'A', b'R', b'1']);
63//! ```
64//! # Example of reading an existing file
65//!
66//! ```rust,no_run
67//! use parquet::file::reader::{FileReader, SerializedFileReader};
68//! use std::{fs::File, path::Path};
69//!
70//! let path = Path::new("/path/to/sample.parquet");
71//! if let Ok(file) = File::open(&path) {
72//!     let reader = SerializedFileReader::new(file).unwrap();
73//!
74//!     let parquet_metadata = reader.metadata();
75//!     assert_eq!(parquet_metadata.num_row_groups(), 1);
76//!
77//!     let row_group_reader = reader.get_row_group(0).unwrap();
78//!     assert_eq!(row_group_reader.num_columns(), 1);
79//! }
80//! ```
81//! # Example of reading multiple files
82//!
83//! ```rust,no_run
84//! use parquet::file::reader::SerializedFileReader;
85//! use std::convert::TryFrom;
86//!
87//! let paths = vec![
88//!     "/path/to/sample.parquet/part-1.snappy.parquet",
89//!     "/path/to/sample.parquet/part-2.snappy.parquet"
90//! ];
91//! // Create a reader for each file and flat map rows
92//! let rows = paths.iter()
93//!     .map(|p| SerializedFileReader::try_from(*p).unwrap())
94//!     .flat_map(|r| r.into_iter());
95//!
96//! for row in rows {
97//!     println!("{}", row.unwrap());
98//! }
99//! ```
100pub mod footer;
101pub mod metadata;
102pub mod page_encoding_stats;
103pub mod page_index;
104pub mod properties;
105pub mod reader;
106pub mod serialized_reader;
107pub mod statistics;
108pub mod writer;
109
110/// The length of the parquet footer in bytes
111pub const FOOTER_SIZE: usize = 8;
112const PARQUET_MAGIC: [u8; 4] = [b'P', b'A', b'R', b'1'];
113const PARQUET_MAGIC_ENCR_FOOTER: [u8; 4] = [b'P', b'A', b'R', b'E'];