数据源注册的包装器。
可以通过 . 访问 spark.dataSource此实例。 使用它注册自定义DataSource子类,以便可以按名称和名称spark.read.format()df.write.format()引用它。
Syntax
spark.dataSource.register(MyDataSource)
方法
| 方法 | 说明 |
|---|---|
register(dataSource) |
注册 Python 用户定义的数据源。
dataSource 必须是 . 的 DataSource子类。 |
示例
注册自定义数据源并从中读取:
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()