HyukjinKwon commented on code in PR #46089:
URL: https://github.com/apache/spark/pull/46089#discussion_r1568048732


##########
python/docs/source/user_guide/sql/python_data_source.rst:
##########
@@ -0,0 +1,139 @@
+..  Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+..    http://www.apache.org/licenses/LICENSE-2.0
+
+..  Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+======================
+Python Data Source API
+======================
+
+.. currentmodule:: pyspark.sql
+
+Overview
+--------
+The Python Data Source API is a new feature introduced in Spark 4.0, enabling 
developers to read from custom data sources and write to custom data sinks in 
Python.
+This guide provides a comprehensive overview of the API and instructions on 
how to create, use, and manage Python data sources.
+
+
+Creating a Python Data Source
+-----------------------------
+To create a custom Python data source, you'll need to subclass the 
:class:`DataSource` base classes and implement the necessary methods for 
reading and writing data.
+
+This example demonstrates creating a simple data source to generate synthetic 
data using the `faker` library. Ensure the `faker` library is installed and 
accessible in your Python environment.
+
+**Step 1: Define the Data Source**
+
+Start by creating a new subclass of :class:`DataSource`. Define the source 
name, schema, and reader logic as follows:
+
+.. code-block:: python
+
+    from pyspark.sql.datasource import DataSource, DataSourceReader
+    from pyspark.sql.types import StructType
+
+    class FakeDataSource(DataSource):
+        """
+        A fake data source for PySpark to generate synthetic data using the 
`faker` library.
+        Options:
+        - numRows: specify number of rows to generate. Default value is 3.
+        """
+
+        @classmethod
+        def name(cls):
+            return "fake"
+
+        def schema(self):
+            return "name string, date string, zipcode string, state string"
+
+        def reader(self, schema: StructType):
+            return FakeDataSourceReader(schema, self.options)
+
+
+**Step 2: Implement the Reader**
+
+Define the reader logic to generate synthetic data. Use the `faker` library to 
populate each field in the schema.
+
+.. code-block:: python
+
+    class FakeDataSourceReader(DataSourceReader):
+
+        def __init__(self, schema, options):
+            self.schema: StructType = schema
+            self.options = options
+
+        def read(self, partition):
+            from faker import Faker
+            fake = Faker()
+            # Note: every value in this `self.options` dictionary is a string.
+            num_rows = int(self.options.get("numRows", 3))
+            for _ in range(num_rows):
+                row = []
+                for field in self.schema.fields:
+                    value = getattr(fake, field.name)()
+                    row.append(value)
+                yield tuple(row)
+
+
+Using a Python Data Source

Review Comment:
   Could we add a small section for `pip install` case too? I have an example 
at https://github.com/hyukjinkwon/pyspark-jira. We should probably say that 
this is not supported with Spark Connect client side (thus it has to be 
installed in Spark Connect server side).



-- 
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]

Reply via email to