Condividi tramite


DataSourceRegistration

Wrapper per la registrazione dell'origine dati.

È possibile accedere a questa istanza tramite spark.dataSource. Usarla per registrare una sottoclasse personalizzata DataSource in modo che possa essere fatto riferimento in base al nome in spark.read.format() e df.write.format().

Sintassi

spark.dataSource.register(MyDataSource)

Methods

metodo Descrizione
register(dataSource) Registra un'origine dati definita dall'utente python. dataSource deve essere una sottoclasse di DataSource.

Examples

Registrare un'origine dati personalizzata e leggerla:

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()