|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | +import Atomics |
| 20 | +import Foundation |
| 21 | +import GRPCCore |
| 22 | +import GRPCNIOTransportHTTP2 |
| 23 | +import GRPCProtobuf |
| 24 | +import NIOCore |
| 25 | +import SwiftyTextTable |
| 26 | +import Synchronization |
| 27 | + |
| 28 | +/// An interface used to load a `DataFrame` from external storage systems |
| 29 | +/// (e.g. file systems, key-value stores, etc). Use `SparkSession.read` to access this. |
| 30 | +public actor DataFrameReader: Sendable { |
| 31 | + var source: String = "" |
| 32 | + |
| 33 | + var paths: [String] = [] |
| 34 | + |
| 35 | + // TODO: Case-insensitive Map |
| 36 | + var extraOptions: [String: String] = [:] |
| 37 | + |
| 38 | + let sparkSession: SparkSession |
| 39 | + |
| 40 | + init(sparkSession: SparkSession) { |
| 41 | + self.sparkSession = sparkSession |
| 42 | + } |
| 43 | + |
| 44 | + /// Specifies the input data source format. |
| 45 | + /// - Parameter source: A string. |
| 46 | + /// - Returns: A `DataFrameReader`. |
| 47 | + public func format(_ source: String) -> DataFrameReader { |
| 48 | + self.source = source |
| 49 | + return self |
| 50 | + } |
| 51 | + |
| 52 | + /// Adds an input option for the underlying data source. |
| 53 | + /// - Parameters: |
| 54 | + /// - key: A key string. |
| 55 | + /// - value: A value string. |
| 56 | + /// - Returns: A `DataFrameReader`. |
| 57 | + public func option(_ key: String, _ value: String) -> DataFrameReader { |
| 58 | + self.extraOptions[key] = value |
| 59 | + return self |
| 60 | + } |
| 61 | + |
| 62 | + /// Loads input in as a `DataFrame`, for data sources that don't require a path (e.g. external |
| 63 | + /// key-value stores). |
| 64 | + /// - Returns: A `DataFrame`. |
| 65 | + public func load() -> DataFrame { |
| 66 | + return load([]) |
| 67 | + } |
| 68 | + |
| 69 | + /// Loads input in as a `DataFrame`, for data sources that require a path (e.g. data backed by a |
| 70 | + /// local or distributed file system). |
| 71 | + /// - Parameter path: A path string. |
| 72 | + /// - Returns: A `DataFrame`. |
| 73 | + public func load(_ path: String) -> DataFrame { |
| 74 | + return load([path]) |
| 75 | + } |
| 76 | + |
| 77 | + /// Loads input in as a `DataFrame`, for data sources that support multiple paths. Only works if |
| 78 | + /// the source is a HadoopFsRelationProvider. |
| 79 | + /// - Parameter paths: An array of path strings. |
| 80 | + /// - Returns: A `DataFrame`. |
| 81 | + public func load(_ paths: [String]) -> DataFrame { |
| 82 | + self.paths = paths |
| 83 | + |
| 84 | + var dataSource = DataSource() |
| 85 | + dataSource.format = self.source |
| 86 | + dataSource.paths = self.paths |
| 87 | + dataSource.options = self.extraOptions |
| 88 | + |
| 89 | + var read = Read() |
| 90 | + read.dataSource = dataSource |
| 91 | + |
| 92 | + var relation = Relation() |
| 93 | + relation.read = read |
| 94 | + |
| 95 | + var plan = Plan() |
| 96 | + plan.opType = .root(relation) |
| 97 | + |
| 98 | + return DataFrame(spark: sparkSession, plan: plan) |
| 99 | + } |
| 100 | + |
| 101 | + /// Loads a CSV file and returns the result as a `DataFrame`. See the documentation on the other |
| 102 | + /// overloaded `csv()` method for more details. |
| 103 | + /// - Parameter path: A path string |
| 104 | + /// - Returns: A `DataFrame`. |
| 105 | + public func csv(_ path: String) -> DataFrame { |
| 106 | + self.source = "csv" |
| 107 | + return load(path) |
| 108 | + } |
| 109 | + |
| 110 | + /// Loads CSV files and returns the result as a `DataFrame`. |
| 111 | + /// This function will go through the input once to determine the input schema if `inferSchema` |
| 112 | + /// is enabled. To avoid going through the entire data once, disable `inferSchema` option or |
| 113 | + /// specify the schema explicitly using `schema`. |
| 114 | + /// - Parameter paths: Path strings. |
| 115 | + /// - Returns: A `DataFrame`. |
| 116 | + public func csv(_ paths: String...) -> DataFrame { |
| 117 | + self.source = "csv" |
| 118 | + return load(paths) |
| 119 | + } |
| 120 | + |
| 121 | + /// Loads a JSON file and returns the result as a `DataFrame`. |
| 122 | + /// - Parameter path: A path string |
| 123 | + /// - Returns: A `DataFrame`. |
| 124 | + public func json(_ path: String) -> DataFrame { |
| 125 | + self.source = "json" |
| 126 | + return load(path) |
| 127 | + } |
| 128 | + |
| 129 | + /// Loads JSON files and returns the result as a `DataFrame`. |
| 130 | + /// - Parameter paths: Path strings |
| 131 | + /// - Returns: A `DataFrame`. |
| 132 | + public func json(_ paths: String...) -> DataFrame { |
| 133 | + self.source = "json" |
| 134 | + return load(paths) |
| 135 | + } |
| 136 | + |
| 137 | + /// Loads an ORC file and returns the result as a `DataFrame`. |
| 138 | + /// - Parameter path: A path string |
| 139 | + /// - Returns: A `DataFrame`. |
| 140 | + public func orc(_ path: String) -> DataFrame { |
| 141 | + self.source = "orc" |
| 142 | + return load(path) |
| 143 | + } |
| 144 | + |
| 145 | + /// Loads ORC files and returns the result as a `DataFrame`. |
| 146 | + /// - Parameter paths: Path strings |
| 147 | + /// - Returns: A `DataFrame`. |
| 148 | + public func orc(_ paths: String...) -> DataFrame { |
| 149 | + self.source = "orc" |
| 150 | + return load(paths) |
| 151 | + } |
| 152 | + |
| 153 | + /// Loads a Parquet file and returns the result as a `DataFrame`. |
| 154 | + /// - Parameter path: A path string |
| 155 | + /// - Returns: A `DataFrame`. |
| 156 | + public func parquet(_ path: String) -> DataFrame { |
| 157 | + self.source = "parquet" |
| 158 | + return load(path) |
| 159 | + } |
| 160 | + |
| 161 | + /// Loads Parquet files, returning the result as a `DataFrame`. |
| 162 | + /// - Parameter paths: Path strings |
| 163 | + /// - Returns: A `DataFrame`. |
| 164 | + public func parquet(_ paths: String...) -> DataFrame { |
| 165 | + self.source = "parquet" |
| 166 | + return load(paths) |
| 167 | + } |
| 168 | +} |
0 commit comments