-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spark DataFrames handled as a type if using spark (#267)
- Loading branch information
Showing
12 changed files
with
527 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .schema import SparkDataFrameSchemaReader, SparkDataFrameSchemaWriter, SparkDataFrameTransformer | ||
from .task import Spark | ||
|
||
__all__ = [Spark] | ||
__all__ = [Spark, SparkDataFrameTransformer, SparkDataFrameSchemaReader, SparkDataFrameSchemaWriter] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import typing | ||
from typing import Type | ||
|
||
from flytekit import FlyteContext | ||
from flytekit.annotated.type_engine import T, TypeEngine, TypeTransformer | ||
from flytekit.models.literals import Literal, Scalar, Schema | ||
from flytekit.models.types import LiteralType, SchemaType | ||
from flytekit.plugins import pyspark | ||
from flytekit.types.schema import SchemaEngine, SchemaFormat, SchemaHandler, SchemaReader, SchemaWriter | ||
|
||
|
||
class SparkDataFrameSchemaReader(SchemaReader[pyspark.sql.DataFrame]): | ||
def __init__(self, from_path: str, cols: typing.Optional[typing.Dict[str, type]], fmt: SchemaFormat): | ||
super().__init__(from_path, cols, fmt) | ||
|
||
def iter(self, **kwargs) -> typing.Generator[T, None, None]: | ||
raise NotImplementedError("Spark DataFrame reader cannot iterate over individual chunks in spark dataframe") | ||
|
||
def all(self, **kwargs) -> pyspark.sql.DataFrame: | ||
if self._fmt == SchemaFormat.PARQUET: | ||
ctx = FlyteContext.current_context().user_space_params | ||
return ctx.spark_session.read.parquet(self.from_path) | ||
raise AssertionError("Only Parquet type files are supported for spark dataframe currently") | ||
|
||
|
||
class SparkDataFrameSchemaWriter(SchemaWriter[pyspark.sql.DataFrame]): | ||
def __init__(self, to_path: str, cols: typing.Optional[typing.Dict[str, type]], fmt: SchemaFormat): | ||
super().__init__(to_path, cols, fmt) | ||
|
||
def write(self, *dfs: pyspark.sql.DataFrame, **kwargs): | ||
if dfs is None or len(dfs) == 0: | ||
return | ||
if len(dfs) > 1: | ||
raise AssertionError("Only one Spark.DataFrame can be returned per return variable currently") | ||
if self._fmt == SchemaFormat.PARQUET: | ||
dfs[0].write.mode("overwrite").parquet(self.to_path) | ||
return | ||
raise AssertionError("Only Parquet type files are supported for spark dataframe currently") | ||
|
||
|
||
class SparkDataFrameTransformer(TypeTransformer[pyspark.sql.DataFrame]): | ||
""" | ||
Transforms Spark DataFrame's to and from a Schema (typed/untyped) | ||
""" | ||
|
||
def __init__(self): | ||
super(SparkDataFrameTransformer, self).__init__("spark-df-transformer", t=pyspark.sql.DataFrame) | ||
|
||
@staticmethod | ||
def _get_schema_type() -> SchemaType: | ||
return SchemaType(columns=[]) | ||
|
||
def get_literal_type(self, t: Type[pyspark.sql.DataFrame]) -> LiteralType: | ||
return LiteralType(schema=self._get_schema_type()) | ||
|
||
def to_literal( | ||
self, | ||
ctx: FlyteContext, | ||
python_val: pyspark.sql.DataFrame, | ||
python_type: Type[pyspark.sql.DataFrame], | ||
expected: LiteralType, | ||
) -> Literal: | ||
remote_path = ctx.file_access.get_random_remote_directory() | ||
w = SparkDataFrameSchemaWriter(to_path=remote_path, cols=None, fmt=SchemaFormat.PARQUET) | ||
w.write(python_val) | ||
return Literal(scalar=Scalar(schema=Schema(remote_path, self._get_schema_type()))) | ||
|
||
def to_python_value(self, ctx: FlyteContext, lv: Literal, expected_python_type: Type[pyspark.sql.DataFrame]) -> T: | ||
if not (lv and lv.scalar and lv.scalar.schema): | ||
return pyspark.sql.DataFrame() | ||
r = SparkDataFrameSchemaReader(from_path=lv.scalar.schema.uri, cols=None, fmt=SchemaFormat.PARQUET) | ||
return r.all() | ||
|
||
|
||
SchemaEngine.register_handler( | ||
SchemaHandler( | ||
"pyspark.sql.DataFrame-Schema", | ||
pyspark.sql.DataFrame, | ||
SparkDataFrameSchemaReader, | ||
SparkDataFrameSchemaWriter, | ||
handles_remote_io=True, | ||
) | ||
) | ||
TypeEngine.register(SparkDataFrameTransformer()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
from flytekit.types.flyte_file import FlyteFile | ||
from flytekit.types.schema import ( | ||
FlyteSchema, | ||
PandasSchemaReader, | ||
PandasSchemaWriter, | ||
Schema, | ||
SchemaFormat, | ||
SchemaOpenMode, | ||
SchemaType, | ||
) | ||
from flytekit.types.pandas_schema import PandasSchemaReader, PandasSchemaWriter | ||
from flytekit.types.schema import FlyteSchema |
Oops, something went wrong.