pyspark.sql.streaming.DataStreamReader.csv¶
-
DataStreamReader.
csv
(path, schema=None, sep=None, encoding=None, quote=None, escape=None, comment=None, header=None, inferSchema=None, ignoreLeadingWhiteSpace=None, ignoreTrailingWhiteSpace=None, nullValue=None, nanValue=None, positiveInf=None, negativeInf=None, dateFormat=None, timestampFormat=None, maxColumns=None, maxCharsPerColumn=None, maxMalformedLogPerPartition=None, mode=None, columnNameOfCorruptRecord=None, multiLine=None, charToEscapeQuoteEscaping=None, enforceSchema=None, emptyValue=None, locale=None, lineSep=None, pathGlobFilter=None, recursiveFileLookup=None, unescapedQuoteHandling=None)[source]¶ Loads a CSV file stream and returns the result as a
DataFrame
.This function will go through the input once to determine the input schema if
inferSchema
is enabled. To avoid going through the entire data once, disableinferSchema
option or specify the schema explicitly usingschema
.- Parameters:
- pathstr or list
string, or list of strings, for input path(s).
- schema
pyspark.sql.types.StructType
or str, optional an optional
pyspark.sql.types.StructType
for the input schema or a DDL-formatted string (For examplecol0 INT, col1 DOUBLE
).- sepstr, optional
sets a separator (one or more characters) for each field and value. If None is set, it uses the default value,
,
.- encodingstr, optional
decodes the CSV files by the given encoding type. If None is set, it uses the default value,
UTF-8
.- quotestr, optional sets a single character used for escaping quoted values where the
separator can be part of the value. If None is set, it uses the default value,
"
. If you would like to turn off quotations, you need to set an empty string.- escapestr, optional
sets a single character used for escaping quotes inside an already quoted value. If None is set, it uses the default value,
\
.- commentstr, optional
sets a single character used for skipping lines beginning with this character. By default (None), it is disabled.
- headerstr or bool, optional
uses the first line as names of columns. If None is set, it uses the default value,
false
.- inferSchemastr or bool, optional
infers the input schema automatically from data. It requires one extra pass over the data. If None is set, it uses the default value,
false
.- enforceSchemastr or bool, optional
If it is set to
true
, the specified or inferred schema will be forcibly applied to datasource files, and headers in CSV files will be ignored. If the option is set tofalse
, the schema will be validated against all headers in CSV files or the first header in RDD if theheader
option is set totrue
. Field names in the schema and column names in CSV headers are checked by their positions taking into accountspark.sql.caseSensitive
. If None is set,true
is used by default. Though the default value istrue
, it is recommended to disable theenforceSchema
option to avoid incorrect results.- ignoreLeadingWhiteSpacestr or bool, optional
a flag indicating whether or not leading whitespaces from values being read should be skipped. If None is set, it uses the default value,
false
.- ignoreTrailingWhiteSpacestr or bool, optional
a flag indicating whether or not trailing whitespaces from values being read should be skipped. If None is set, it uses the default value,
false
.- nullValuestr, optional
sets the string representation of a null value. If None is set, it uses the default value, empty string. Since 2.0.1, this
nullValue
param applies to all supported types including the string type.- nanValuestr, optional
sets the string representation of a non-number value. If None is set, it uses the default value,
NaN
.- positiveInfstr, optional
sets the string representation of a positive infinity value. If None is set, it uses the default value,
Inf
.- negativeInfstr, optional
sets the string representation of a negative infinity value. If None is set, it uses the default value,
Inf
.- dateFormatstr, optional
sets the string that indicates a date format. Custom date formats follow the formats at datetime pattern. # noqa This applies to date type. If None is set, it uses the default value,
yyyy-MM-dd
.- timestampFormatstr, optional
sets the string that indicates a timestamp format. Custom date formats follow the formats at datetime pattern. # noqa This applies to timestamp type. If None is set, it uses the default value,
yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX]
.- maxColumnsstr or int, optional
defines a hard limit of how many columns a record can have. If None is set, it uses the default value,
20480
.- maxCharsPerColumnstr or int, optional
defines the maximum number of characters allowed for any given value being read. If None is set, it uses the default value,
-1
meaning unlimited length.- maxMalformedLogPerPartitionstr or int, optional
this parameter is no longer used since Spark 2.2.0. If specified, it is ignored.
- modestr, optional
allows a mode for dealing with corrupt records during parsing. If None is set, it uses the default value,
PERMISSIVE
.PERMISSIVE
: when it meets a corrupted record, puts the malformed string into a field configured bycolumnNameOfCorruptRecord
, and sets malformed fields tonull
. To keep corrupt records, an user can set a string type field namedcolumnNameOfCorruptRecord
in an user-defined schema. If a schema does not have the field, it drops corrupt records during parsing. A record with less/more tokens than schema is not a corrupted record to CSV. When it meets a record having fewer tokens than the length of the schema, setsnull
to extra fields. When the record has more tokens than the length of the schema, it drops extra tokens.DROPMALFORMED
: ignores the whole corrupted records.FAILFAST
: throws an exception when it meets corrupted records.
- columnNameOfCorruptRecordstr, optional
allows renaming the new field having malformed string created by
PERMISSIVE
mode. This overridesspark.sql.columnNameOfCorruptRecord
. If None is set, it uses the value specified inspark.sql.columnNameOfCorruptRecord
.- multiLinestr or bool, optional
parse one record, which may span multiple lines. If None is set, it uses the default value,
false
.- charToEscapeQuoteEscapingstr, optional
sets a single character used for escaping the escape for the quote character. If None is set, the default value is escape character when escape and quote characters are different,
\0
otherwise.- emptyValuestr, optional
sets the string representation of an empty value. If None is set, it uses the default value, empty string.
- localestr, optional
sets a locale as language tag in IETF BCP 47 format. If None is set, it uses the default value,
en-US
. For instance,locale
is used while parsing dates and timestamps.- lineSepstr, optional
defines the line separator that should be used for parsing. If None is set, it covers all
\\r
,\\r\\n
and\\n
. Maximum length is 1 character.- pathGlobFilterstr or bool, optional
an optional glob pattern to only include files with paths matching the pattern. The syntax follows org.apache.hadoop.fs.GlobFilter. It does not change the behavior of partition discovery. # noqa
- recursiveFileLookupstr or bool, optional
recursively scan a directory for files. Using this option disables partition discovery. # noqa
- unescapedQuoteHandlingstr, optional
defines how the CsvParser will handle values with unescaped quotes. If None is set, it uses the default value,
STOP_AT_DELIMITER
.STOP_AT_CLOSING_QUOTE
: If unescaped quotes are found in the input, accumulate the quote character and proceed parsing the value as a quoted value, until a closing quote is found.BACK_TO_DELIMITER
: If unescaped quotes are found in the input, consider the value as an unquoted value. This will make the parser accumulate all characters of the current parsed value until the delimiter is found. If no delimiter is found in the value, the parser will continue accumulating characters from the input until a delimiter or line ending is found.STOP_AT_DELIMITER
: If unescaped quotes are found in the input, consider the value as an unquoted value. This will make the parser accumulate all characters until the delimiter or a line ending is found in the input.SKIP_VALUE
: If unescaped quotes are found in the input, the content parsed for the given value will be skipped and the value set in nullValue will be produced instead.RAISE_ERROR
: If unescaped quotes are found in the input, a TextParsingException will be thrown.
- .. versionadded:: 2.0.0
Notes
This API is evolving.
Examples
>>> csv_sdf = spark.readStream.csv(tempfile.mkdtemp(), schema = sdf_schema) >>> csv_sdf.isStreaming True >>> csv_sdf.schema == sdf_schema True