可以使用 SparkSession 的 read 方法来读取本地文件,例如:
from pyspark.sql import SparkSession
# 创建 SparkSession 对象
spark = SparkSession.builder.appName("MyApp").getOrCreate()
# 读取 CSV 文件
df = spark.read.csv("/path/to/local/file.csv", header=True)
# 读取 JSON 文件
df = spark.read.json("/path/to/local/file.json")
# 读取 Parquet 文件
df = spark.read.parquet("/path/to/local/file.parquet")
在上面的代码中,我们创建了一个 SparkSession 对象,然后使用它的 read 方法来读取本地 CSV、JSON 和 Parquet 文件。注意,对于 CSV 文件,我们需要指定 header 参数为 True,以指示文件的第一行包含列名。
希望这些信息能帮助您。