1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
//! DataFrameReader & DataFrameWriter representations
use std::collections::HashMap;
use crate::plan::LogicalPlanBuilder;
use crate::session::SparkSession;
use crate::spark;
use crate::DataFrame;
use spark::write_operation::SaveMode;
use arrow::error::ArrowError;
/// DataFrameReader represents the entrypoint to create a DataFrame
/// from a specific file format.
#[derive(Clone, Debug)]
pub struct DataFrameReader {
spark_session: SparkSession,
format: Option<String>,
read_options: HashMap<String, String>,
}
impl DataFrameReader {
/// Create a new DataFrameReader with a [SparkSession]
pub fn new(spark_session: SparkSession) -> Self {
Self {
spark_session,
format: None,
read_options: HashMap::new(),
}
}
/// Specifies the input data source format
pub fn format(mut self, format: &str) -> Self {
self.format = Some(format.to_string());
self
}
/// Add an input option for the underlying data source
pub fn option(mut self, key: &str, value: &str) -> Self {
self.read_options.insert(key.to_string(), value.to_string());
self
}
/// Set many input options based on a [HashMap] for the underlying data source
pub fn options(mut self, options: HashMap<String, String>) -> Self {
self.read_options = options;
self
}
/// Loads data from a data source and returns it as a [DataFrame]
///
/// Example:
/// ```rust
/// let paths = vec!["some/dir/path/on/the/remote/cluster/".to_string()];
///
/// // returns a DataFrame from a csv file with a header from a the specific path
/// let mut df = spark.read().format("csv").option("header", "true").load(paths);
/// ```
pub fn load(&mut self, paths: Vec<String>) -> DataFrame {
let read_type = Some(spark::relation::RelType::Read(spark::Read {
is_streaming: false,
read_type: Some(spark::read::ReadType::DataSource(spark::read::DataSource {
format: self.format.clone(),
schema: None,
options: self.read_options.clone(),
paths,
predicates: vec![],
})),
}));
let relation = spark::Relation {
common: Some(spark::RelationCommon {
source_info: "NA".to_string(),
plan_id: Some(1),
}),
rel_type: read_type,
};
let logical_plan = LogicalPlanBuilder::new(relation);
DataFrame::new(self.spark_session.clone(), logical_plan)
}
/// Returns the specific table as a [DataFrame]
///
/// # Arguments:
/// * `table_name`: &str of the table name
/// * `options`: (optional Hashmap) contains additional read options for a table
///
pub fn table(
&mut self,
table_name: &str,
options: Option<HashMap<String, String>>,
) -> DataFrame {
let read_type = Some(spark::relation::RelType::Read(spark::Read {
is_streaming: false,
read_type: Some(spark::read::ReadType::NamedTable(spark::read::NamedTable {
unparsed_identifier: table_name.to_string(),
options: options.unwrap_or(self.read_options.clone()),
})),
}));
let relation = spark::Relation {
common: Some(spark::RelationCommon {
source_info: "NA".to_string(),
plan_id: Some(1),
}),
rel_type: read_type,
};
let logical_plan = LogicalPlanBuilder::new(relation);
DataFrame::new(self.spark_session.clone(), logical_plan)
}
}
/// DataFrameWriter provides the ability to output a [DataFrame]
/// to a specific file format supported by Spark
pub struct DataFrameWriter {
dataframe: DataFrame,
format: Option<String>,
mode: SaveMode,
bucket_by: Option<spark::write_operation::BucketBy>,
partition_by: Vec<String>,
sort_by: Vec<String>,
write_options: HashMap<String, String>,
}
impl DataFrameWriter {
/// Create a new DataFrameWriter from a provided [DataFrame]
///
/// # Defaults
/// - `format`: None,
/// - `mode`: [SaveMode::Overwrite],
/// - `bucket_by`: None,
/// - `partition_by`: vec![],
/// - `sort_by`: vec![],
/// - `write_options`: HashMap::new()
///
pub fn new(dataframe: DataFrame) -> Self {
Self {
dataframe,
format: None,
mode: SaveMode::Overwrite,
bucket_by: None,
partition_by: vec![],
sort_by: vec![],
write_options: HashMap::new(),
}
}
/// Target format to output the [DataFrame]
pub fn format(mut self, format: &str) -> Self {
self.format = Some(format.to_string());
self
}
/// Specifies the behavior when data or table already exists
///
/// # Arguments:
/// - `mode`: (&str) translates to a specific [SaveMode] from the protobuf
///
pub fn mode(mut self, mode: &str) -> Self {
self.mode = match mode {
"append" => SaveMode::Append,
"overwrite" => SaveMode::Overwrite,
"error" | "errorifexists" => SaveMode::ErrorIfExists,
"ignore" => SaveMode::Ignore,
_ => SaveMode::Unspecified,
};
self
}
/// Buckets the output by the given columns.
/// If specified, the output is laid out on the file system
/// similar to Hive’s bucketing scheme.
#[allow(non_snake_case)]
pub fn bucketBy(mut self, num_buckets: i32, buckets: Vec<String>) -> Self {
self.bucket_by = Some(spark::write_operation::BucketBy {
bucket_column_names: buckets,
num_buckets,
});
self
}
/// Sorts the output in each bucket by the given columns on the file system
#[allow(non_snake_case)]
pub fn sortBy(mut self, cols: Vec<String>) -> Self {
self.sort_by = cols;
self
}
/// Partitions the output by the given columns on the file system
#[allow(non_snake_case)]
pub fn partitionBy(mut self, cols: Vec<String>) -> Self {
self.sort_by = cols;
self
}
/// Add an input option for the underlying data source
pub fn option(mut self, key: &str, value: &str) -> Self {
self.write_options
.insert(key.to_string(), value.to_string());
self
}
/// Set many input options based on a [HashMap] for the underlying data source
pub fn options(mut self, options: HashMap<String, String>) -> Self {
self.write_options = options;
self
}
/// Save the contents of the [DataFrame] to a data source.
///
/// The data source is specified by the `format` and a set of `options`.
pub async fn save(&mut self, path: &str) -> Result<(), ArrowError> {
let write_command = spark::command::CommandType::WriteOperation(spark::WriteOperation {
input: Some(self.dataframe.logical_plan.relation.clone()),
source: self.format.clone(),
mode: self.mode.into(),
sort_column_names: self.sort_by.clone(),
partitioning_columns: self.partition_by.clone(),
bucket_by: self.bucket_by.clone(),
options: self.write_options.clone(),
save_type: Some(spark::write_operation::SaveType::Path(path.to_string())),
});
let plan = LogicalPlanBuilder::build_plan_cmd(write_command);
self.dataframe
.spark_session
.consume_plan(Some(plan))
.await
.unwrap();
Ok(())
}
async fn save_table(&mut self, table_name: &str, save_method: i32) -> Result<(), ArrowError> {
let write_command = spark::command::CommandType::WriteOperation(spark::WriteOperation {
input: Some(self.dataframe.logical_plan.relation.clone()),
source: self.format.clone(),
mode: self.mode.into(),
sort_column_names: self.sort_by.clone(),
partitioning_columns: self.partition_by.clone(),
bucket_by: self.bucket_by.clone(),
options: self.write_options.clone(),
save_type: Some(spark::write_operation::SaveType::Table(
spark::write_operation::SaveTable {
table_name: table_name.to_string(),
save_method,
},
)),
});
let plan = LogicalPlanBuilder::build_plan_cmd(write_command);
self.dataframe
.spark_session
.consume_plan(Some(plan))
.await
.unwrap();
Ok(())
}
/// Saves the context of the [DataFrame] as the specified table.
#[allow(non_snake_case)]
pub async fn saveAsTable(&mut self, table_name: &str) -> Result<(), ArrowError> {
self.save_table(table_name, 1).await
}
/// Inserts the content of the [DataFrame] to the specified table.
///
/// It requires that the schema of the [DataFrame] is the same as the
/// schema of the target table.
///
/// Unlike `saveAsTable()`, this method ignores the column names and just uses
/// position-based resolution
#[allow(non_snake_case)]
pub async fn insertInto(&mut self, table_name: &str) -> Result<(), ArrowError> {
self.save_table(table_name, 2).await
}
}