Aller au contenu principal

JSON (DataStreamReader)

Charge un flux de fichiers JSON et renvoie les résultats sous forme de DataFrame. Les lignes JSON (JSON délimité par des retours à la ligne) sont prises en charge by default. Pour les fichiers JSON avec un enregistrement par fichier, définissez l'option multiLine sur true. Si schema n'est pas spécifié, le schéma d'entrée est déduit des données.

Syntaxe​

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

parameter​

parameter

Type

Description

path

str

Chemin d'accès au dataset JSON.

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 d'accès au dataset JSON.

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 JSON temporaire :

Python
import tempfile
import time
with tempfile.TemporaryDirectory(prefix="json") as d:
spark.createDataFrame(
[(100, "Hyukjin Kwon"),], ["age", "name"]
).write.mode("overwrite").format("json").save(d)
q = spark.readStream.schema(
"age INT, name STRING"
).json(d).writeStream.format("console").start()
time.sleep(3)
q.stop()