HyukjinKwon commented on code in PR #43301:
URL: https://github.com/apache/spark/pull/43301#discussion_r1354119362
##########
python/pyspark/sql/readwriter.py:
##########
@@ -505,24 +506,71 @@ def parquet(self, *paths: str, **options:
"OptionalPrimitiveType") -> "DataFrame
.. # noqa
+ Returns
+ -------
+ :class:`DataFrame`
+ A DataFrame containing the data from the Parquet files.
+
Examples
--------
+ Create sample dataframes.
+
+ >>> df = spark.createDataFrame(
+ ... [(10, "Alice"), (15, "Bob"), (20, "Tom")], schema=["age",
"name"])
+ >>> df2 = spark.createDataFrame([(70, "Alice"), (80, "Bob")],
schema=["height", "name"])
+
Write a DataFrame into a Parquet file and read it back.
>>> import tempfile
>>> with tempfile.TemporaryDirectory() as d:
- ... # Write a DataFrame into a Parquet file
- ... spark.createDataFrame(
- ... [{"age": 100, "name": "Hyukjin Kwon"}]
- ... ).write.mode("overwrite").format("parquet").save(d)
+ ... # Write a DataFrame into a Parquet file.
+ ... df.write.mode("overwrite").format("parquet").save(d)
...
... # Read the Parquet file as a DataFrame.
- ... spark.read.parquet(d).show()
- +---+------------+
- |age| name|
- +---+------------+
- |100|Hyukjin Kwon|
- +---+------------+
+ ... spark.read.parquet(d).orderBy("name").show()
+ +---+-----+
+ |age| name|
+ +---+-----+
+ | 10|Alice|
+ | 15| Bob|
+ | 20| Tom|
+ +---+-----+
+
+ Read a Parquet file with a specific column.
+
+ >>> with tempfile.TemporaryDirectory as d:
+ ... df.write.mode("overwrite").format("parquet").save(d)
+ ...
+ ... # Read the Parquet file with only the 'name' column.
+ ... spark.read.schema("name
string").parquet(d).orderBy("name").show()
+ +-----+
+ | name|
+ +-----+
+ |Alice|
+ | Bob|
+ | Tom|
+ +-----+
+
+ Read multiple Parquet files and merge schema.
+
+ >>> with tempfile.TemporaryDirectory() as d1,
tempfile.TemporaryDirectory() as d2:
+ ... df.write.mode("overwrite").format("parquet").save(d1)
+ ... df2.write.mode("overwrite").format("parquet").save(d2)
+ ...
+ ... spark.read.option(
+ ... "mergeSchema", "true"
+ ... ).parquet(d1, d2).select(
+ ... "name", "age", "height"
+ ... ).orderBy("name", "age").show()
+ +-----+----+------+
+ | name| age|height|
+ +-----+----+------+
+ |Alice|null| 70|
+ |Alice| 10| null|
+ | Bob|null| 80|
+ | Bob| 15| null|
+ | Tom| 20| null|
+ +-----+----+------+
Review Comment:
```suggestion
+-----+----+------+
| name| age|height|
+-----+----+------+
|Alice|NULL| 70|
|Alice| 10| NULL|
| Bob|NULL| 80|
| Bob| 15| NULL|
| Tom| 20| NULL|
+-----+----+------+
```
--
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]