amoghrajesh commented on code in PR #60000:
URL: https://github.com/apache/spark/pull/60000#discussion_r4595866065
##########
python/pyspark/sql/session.py:
##########
@@ -145,6 +144,10 @@ def toDF(self, schema=None, sampleRatio=None):
T = TypeVar("T")
+# Must match SparkSession.GET_OR_CREATE_MARKER on the Scala side, so that a
dataset stored by
+# one of them is recognised as complete by the other.
+_GET_OR_CREATE_MARKER = "_spark_getorcreate_success"
+
Review Comment:
Yeah seems reasonable. I moved the check and the write into two JVM helpers
on the classic session, `isDataFrameMaterialized` and `materializeDataFrame`.
Scala and Python both go through them now, so the marker name only lives in
Scala. Python still evals the lambda itself.
For Spark connect, I have left it throwing for now. Exposing the check there
needs a new RPC, so I prefer to do it as a follow up.
##########
python/pyspark/sql/session.py:
##########
@@ -1990,6 +1993,53 @@ def table(self, tableName: str) -> DataFrame:
return DataFrame(self._jsparkSession.table(tableName), self)
+ def getOrCreate(self, path: str, create: Callable[[], DataFrame]) ->
DataFrame:
+ """Returns the :class:`DataFrame` stored at ``path``, computing and
storing it first if
+ it is not already there. The dataset is stored as Parquet.
+
+ .. versionadded:: 4.4.0
+
+ Parameters
+ ----------
+ path : str
+ Directory holding the dataset. The caller supplies it, so a
scheduled pipeline can
+ keep its runs apart by including a run identifier in it.
+ create : function
+ Zero-argument function returning the :class:`DataFrame` to store.
It is called only
+ when ``path`` does not already hold a complete dataset.
+
+ Returns
+ -------
+ :class:`DataFrame`
+
+ Notes
+ -----
+ Supports Spark Classic only.
+
+ Completeness is tracked with a marker file written under ``path`` once
the data is
+ stored, so a path left behind by an interrupted write is recomputed
rather than read
+ back as if it were finished.
+
+ Examples
+ --------
+ >>> import tempfile
+ >>> with tempfile.TemporaryDirectory() as d:
+ ... path = os.path.join(d, "ids")
+ ... spark.getOrCreate(path, lambda: spark.range(3)).count()
+ ... spark.getOrCreate(path, lambda: spark.range(100)).count()
+ 3
+ 3
+ """
+ assert self._jvm is not None
+ marker = self._jvm.org.apache.hadoop.fs.Path(path,
_GET_OR_CREATE_MARKER)
+ fs = marker.getFileSystem(self._jsc.hadoopConfiguration())
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]