我正在通过以下代码读取一个 csv 文件:-
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.master("local[2]") \
.getOrCreate()
现在有四种不同的阅读选项:
df = spark.read.load("/..../xyz.csv")df = spark.read.csv("/..../xyz.csv")df = spark.read.format('csv').load("/..../xyz.csv")df = spark.read.option().csv("/..../xyz.csv")
我应该使用哪个选项?
编辑:-
此外,inferSchema="true" 和 inferSchema=True 都在工作。我们可以盲目使用任何一个吗?
请您参考如下方法:
2 和 3 是等价的。
3 允许额外的 option(key, value) 函数(参见 4,或 spark.read.format( 'csv').option(...).load()) 可以让您跳过标题行,或设置逗号以外的分隔符,例如。
1 不解析 CSV,it uses Parquet as the default format .
def load(self, path=None, format=None, schema=None, **options):
"""Loads data from a data source and returns it as a :class`DataFrame`.
:param path: optional string or a list of string for file-system backed data sources.
:param format: optional string for format of the data source. Default to 'parquet'.
:param schema: optional :class:`pyspark.sql.types.StructType` for the input schema
or a DDL-formatted string (For example ``col0 INT, col1 DOUBLE``).
:param options: all other string options
我建议使用 inferSchema=True 来防止字符串值中的拼写错误
