共用方式為


資料來源註冊

一個用於資料來源註冊的包裝器。

此實例可透過 spark.dataSource存取。 用它來註冊一個自訂DataSource子類別,這樣它就能在 和 df.write.format()中以名稱spark.read.format()來引用。

語法

spark.dataSource.register(MyDataSource)

方法

方法 說明
register(dataSource) 註冊一個 Python 使用者定義的資料來源。 dataSource 必須是 的 DataSource子類。

Examples

註冊一個自訂資料來源並從中讀取:

from pyspark.sql.datasource import DataSource, DataSourceReader

class MyDataSource(DataSource):
    @classmethod
    def name(cls):
        return "my_data_source"

    def schema(self):
        return "id INT, value STRING"

    def reader(self, schema):
        return MyDataSourceReader(schema)

class MyDataSourceReader(DataSourceReader):
    def read(self, partition):
        yield (1, "hello")
        yield (2, "world")

spark.dataSource.register(MyDataSource)
df = spark.read.format("my_data_source").load()
df.show()