Aller au contenu principal

csv (DataStreamReader)

Charge un stream de fichier CSV et renvoie le résultat sous forme de DataFrame. Si inferSchema est activé, la fonction parcourt l’entrée une fois pour déterminer le schéma. Pour éviter cette passe, désactivez inferSchema ou spécifiez le schéma explicitement à l’aide de schema.

Syntaxe

csv(path, schema=None, **options)

parameter

parameter

Type

Description

path

str

Chemin pour l'entrée CSV.

schema

StructType ou str, facultatif.

Schéma en tant que StructType ou chaîne formatée DDL (par exemple, col0 INT, col1 DOUBLE).

parameter

Type

Description

path

str

Chemin pour l'entrée CSV.

schema

StructType ou str, facultatif.

Schéma en tant que StructType ou chaîne formatée DDL (par exemple, col0 INT, col1 DOUBLE).

Renvoie

DataFrame

Exemples

Chargez un Stream à partir d'un fichier CSV temporaire :

Python
import tempfile
import time
with tempfile.TemporaryDirectory(prefix="csv") as d:
spark.createDataFrame([(1, "2"),]).write.mode("overwrite").format("csv").save(d)
q = spark.readStream.schema(
"col0 INT, col1 STRING"
).format("csv").load(d).writeStream.format("console").start()
time.sleep(3)
q.stop()