allisonwang-db commented on code in PR #43687:
URL: https://github.com/apache/spark/pull/43687#discussion_r1384245409
##########
python/pyspark/sql/readwriter.py:
##########
@@ -380,22 +380,72 @@ def json(
Examples
--------
- Write a DataFrame into a JSON file and read it back.
+ Example 1: Write a DataFrame into a JSON file and read it back.
>>> import tempfile
>>> with tempfile.TemporaryDirectory() as d:
... # Write a DataFrame into a JSON file
... spark.createDataFrame(
- ... [{"age": 100, "name": "Hyukjin Kwon"}]
+ ... [{"age": 100, "name": "Hyukjin"}]
... ).write.mode("overwrite").format("json").save(d)
...
... # Read the JSON file as a DataFrame.
... spark.read.json(d).show()
- +---+------------+
- |age| name|
- +---+------------+
- |100|Hyukjin Kwon|
- +---+------------+
+ +---+-------+
+ |age| name|
+ +---+-------+
+ |100|Hyukjin|
+ +---+-------+
+
+ Example 2: Read JSON from multiple files in a directory
+
+ >>> import tempfile
+ >>> with tempfile.TemporaryDirectory() as d1,
tempfile.TemporaryDirectory() as d2:
+ ... # Write a DataFrame into a JSON file
+ ... spark.createDataFrame(
+ ... [{"age": 30, "name": "Bob"}]
+ ... ).write.mode("overwrite").format("json").save(d1)
+ ...
+ ... # Read the JSON files as a DataFrame.
+ ... spark.createDataFrame(
+ ... [{"age": 25, "name": "Alice"}]
+ ... ).write.mode("overwrite").format("json").save(d2)
+ ... spark.read.json([d1, d2]).show()
+ +---+-----+
+ |age| name|
+ +---+-----+
+ | 25|Alice|
+ | 30| Bob|
+ +---+-----+
+
+ Example 3: Read JSON from an RDD of JSON strings
+
+ >>> json_strings = ["{'name': 'Alice', 'age': 25}", "{'name': 'Bob',
'age': 30}"]
+ >>> rdd = spark.sparkContext.parallelize(json_strings) # doctest:
+SKIP
+ >>> df = spark.read.json(rdd) # doctest: +SKIP
+ >>> df.show() # doctest: +SKIP
Review Comment:
Yea my concern is that if users are using Spark Connect, this example won't
work for them...
--
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]