इसके माध्यम से साझा किया गया


from_csv function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Returns a struct value with the csvStr and schema.

Syntax

from_csv(csvStr, schema [, options])

Arguments

  • csvStr: A STRING expression specifying a row of CSV data.
  • schema: A STRING literal or invocation of schema_of_csv function.
  • options: An optional MAP<STRING,STRING> literal specifying directives.

Returns

A STRUCT with field names and types matching the schema definition.

csvStr should be well formed with respect to the schema and options. schema must be defined as comma-separated column name and data type pairs as used in for example CREATE TABLE.

options, if provided, can be any of the following:

  • sep (default ,): sets a separator for each field and value. This separator can be one or more characters.
  • encoding (default UTF-8): decodes the CSV files by the specified encoding type.
  • quote (default "): sets a single character used for escaping quoted values where the separator can be part of the value. If you would like to turn off quotations, you need to set not null but an empty string. This behavior is different from com.databricks.spark.csv.
  • escape (default \): sets a single character used for escaping quotes inside an already quoted value.
  • charToEscapeQuoteEscaping (default escape or \0): sets a single character used for escaping the escape for the quote character. The default value is escape character when escape and quote characters are different, \0 otherwise.
  • comment (default empty string): sets a single character used for skipping lines beginning with this character. By default, it is disabled.
  • header (default false): uses the first line as names of columns.
  • enforceSchema (default true): If it is set to true, the specified or inferred schema is forcibly applied to datasource files, and headers in CSV files is ignored. If the option is set to false, the schema is validated against all headers in CSV files in the case when the header option is set to true. Field names in the schema and column names in CSV headers are checked by their positions taking into account spark.sql.caseSensitive. Though the default value is true, it is recommended to disable the enforceSchema option to avoid incorrect results.
  • inferSchema (default false): infers the input schema automatically from data. It requires one extra pass over the data.
  • samplingRatio (default 1.0): defines fraction of rows used for schema inferring.
  • ignoreLeadingWhiteSpace (default false): a flag indicating whether or not leading whitespaces from values being read should be skipped.
  • ignoreTrailingWhiteSpace (default false): a flag indicating whether or not trailing whitespaces from values being read should be skipped.
  • nullValue (default empty string): sets the string representation of a null value.
  • emptyValue (default empty string): sets the string representation of an empty value.
  • nanValue (default NaN): sets the string representation of a non-number value.
  • positiveInf (default Inf): sets the string representation of a positive infinity value.
  • negativeInf (default -Inf): sets the string representation of a negative infinity value.
  • dateFormat (default yyyy-MM-dd): sets the string that indicates a date format. Custom date formats follow the formats at Datetime patterns. This applies to date type.
  • timestampFormat (default yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX]): sets the string that indicates a timestamp format. Custom date formats follow the formats at Datetime patterns. This applies to timestamp type.
  • maxColumns (default 20480): defines a hard limit of how many columns a record can have.
  • maxCharsPerColumn (default -1): defines the maximum number of characters allowed for any specified value being read. By default, it is -1 meaning unlimited length
  • unescapedQuoteHandling (default STOP_AT_DELIMITER): defines how the CSV parser handles values with unescaped quotes.
    • 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.
    • STOP_AT_DELIMITER: If unescaped quotes are found in the input, the content parsed for the specified value is skipped and the value set in nullValue is produced instead.
    • RAISE_ERROR: If unescaped quotes are found in the input, a TextParsingException is thrown.
  • mode (default PERMISSIVE): allows a mode for dealing with corrupt records during parsing. It supports the following case-insensitive modes. Spark tries to parse only required columns in CSV under column pruning. Therefore, corrupt records can be different based on required set of fields. This behavior can be controlled by spark.sql.csv.parser.columnPruning.enabled (enabled by default).
    • PERMISSIVE: when it meets a corrupted record, puts the malformed string into a field configured by columnNameOfCorruptRecord, and sets malformed fields to null. To keep corrupt records, an user can set a string type field named columnNameOfCorruptRecord in an user-defined schema. If a schema does not have the field, it drops corrupt records during parsing. A record with fewer or 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, sets null to extra fields. When the record has more tokens than the length of the schema, it drops extra tokens.
    • FAILFAST: throws an exception when it meets corrupted records.
  • columnNameOfCorruptRecord (default is the value specified in spark.sql.columnNameOfCorruptRecord): allows renaming the new field having malformed string created by PERMISSIVE mode. This overrides spark.sql.columnNameOfCorruptRecord.
  • multiLine (default false): parse one record, which may span multiple lines.
  • locale (default en-US): sets a locale as language tag in IETF BCP 47 format. For instance, this is used while parsing dates and timestamps.
  • lineSep (default covers all \r, \r\n, and \n): defines the line separator that should be used for parsing. Maximum length is 1 character.
  • pathGlobFilter: 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.

Examples

> SELECT from_csv('1, 0.8', 'a INT, b DOUBLE');
 {1,0.8}
> SELECT from_csv('26/08/2015', 'time Timestamp', map('timestampFormat', 'dd/MM/yyyy'));
 {"time":2015-08-26 00:00:00}