Class

org.apache.spark.sql

DataFrameWriter

Related Doc: package sql

Permalink

final class DataFrameWriter[T] extends AnyRef

Interface used to write a Dataset to external storage systems (e.g. file systems, key-value stores, etc). Use Dataset.write to access this.

Source
DataFrameWriter.scala
Since

1.4.0

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. DataFrameWriter
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def bucketBy(numBuckets: Int, colName: String, colNames: String*): DataFrameWriter[T]

    Permalink

    Buckets the output by the given columns.

    Buckets the output by the given columns. If specified, the output is laid out on the file system similar to Hive's bucketing scheme.

    This is applicable for Parquet, JSON and ORC.

    Annotations
    @varargs()
    Since

    2.0

  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. def csv(path: String): Unit

    Permalink

    Saves the content of the DataFrame in CSV format at the specified path.

    Saves the content of the DataFrame in CSV format at the specified path. This is equivalent to:

    format("csv").save(path)

    You can set the following CSV-specific option(s) for writing CSV files:

    • sep (default ,): sets the single character as a separator for each field and value.
    • quote (default "): sets the single character used for escaping quoted values where the separator can be part of the value.
    • escape (default \): sets the single character used for escaping quotes inside an already quoted value.
    • escapeQuotes (default true): a flag indicating whether values containing quotes should always be enclosed in quotes. Default is to escape all values containing a quote character.
    • quoteAll (default false): A flag indicating whether all values should always be enclosed in quotes. Default is to only escape values containing a quote character.
    • header (default false): writes the names of columns as the first line.
    • nullValue (default empty string): sets the string representation of a null value.
    • compression (default null): compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).
    Since

    2.0.0

  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def format(source: String): DataFrameWriter[T]

    Permalink

    Specifies the underlying output data source.

    Specifies the underlying output data source. Built-in options include "parquet", "json", etc.

    Since

    1.4.0

  12. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  14. def insertInto(tableName: String): Unit

    Permalink

    Inserts the content of the DataFrame to the specified table.

    Inserts the content of the DataFrame to the specified table. It requires that the schema of the DataFrame is the same as the schema of the table.

    Note: Unlike saveAsTable, insertInto ignores the column names and just uses position-based resolution. For example:

    scala> Seq((1, 2)).toDF("i", "j").write.mode("overwrite").saveAsTable("t1")
    scala> Seq((3, 4)).toDF("j", "i").write.insertInto("t1")
    scala> Seq((5, 6)).toDF("a", "b").write.insertInto("t1")
    scala> sql("select * from t1").show
    +---+---+
    |  i|  j|
    +---+---+
    |  5|  6|
    |  3|  4|
    |  1|  2|
    +---+---+

    Because it inserts data to an existing table, format or options will be ignored.

    Since

    1.4.0

  15. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  16. def jdbc(url: String, table: String, connectionProperties: Properties): Unit

    Permalink

    Saves the content of the DataFrame to an external database table via JDBC.

    Saves the content of the DataFrame to an external database table via JDBC. In the case the table already exists in the external database, behavior of this function depends on the save mode, specified by the mode function (default to throwing an exception).

    Don't create too many partitions in parallel on a large cluster; otherwise Spark might crash your external database systems.

    url

    JDBC database url of the form jdbc:subprotocol:subname

    table

    Name of the table in the external database.

    connectionProperties

    JDBC database connection arguments, a list of arbitrary string tag/value. Normally at least a "user" and "password" property should be included. "batchsize" can be used to control the number of rows per insert.

    Since

    1.4.0

  17. def json(path: String): Unit

    Permalink

    Saves the content of the DataFrame in JSON format at the specified path.

    Saves the content of the DataFrame in JSON format at the specified path. This is equivalent to:

    format("json").save(path)

    You can set the following JSON-specific option(s) for writing JSON files:

    • compression (default null): compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).
    Since

    1.4.0

  18. def mode(saveMode: String): DataFrameWriter[T]

    Permalink

    Specifies the behavior when data or table already exists.

    Specifies the behavior when data or table already exists. Options include:

    • overwrite: overwrite the existing data.
    • append: append the data.
    • ignore: ignore the operation (i.e. no-op).
    • error: default option, throw an exception at runtime.
    Since

    1.4.0

  19. def mode(saveMode: SaveMode): DataFrameWriter[T]

    Permalink

    Specifies the behavior when data or table already exists.

    Specifies the behavior when data or table already exists. Options include:

    • SaveMode.Overwrite: overwrite the existing data.
    • SaveMode.Append: append the data.
    • SaveMode.Ignore: ignore the operation (i.e. no-op).
    • SaveMode.ErrorIfExists: default option, throw an exception at runtime.
    Since

    1.4.0

  20. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  21. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  22. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  23. def option(key: String, value: Double): DataFrameWriter[T]

    Permalink

    Adds an output option for the underlying data source.

    Adds an output option for the underlying data source.

    Since

    2.0.0

  24. def option(key: String, value: Long): DataFrameWriter[T]

    Permalink

    Adds an output option for the underlying data source.

    Adds an output option for the underlying data source.

    Since

    2.0.0

  25. def option(key: String, value: Boolean): DataFrameWriter[T]

    Permalink

    Adds an output option for the underlying data source.

    Adds an output option for the underlying data source.

    Since

    2.0.0

  26. def option(key: String, value: String): DataFrameWriter[T]

    Permalink

    Adds an output option for the underlying data source.

    Adds an output option for the underlying data source.

    Since

    1.4.0

  27. def options(options: Map[String, String]): DataFrameWriter[T]

    Permalink

    Adds output options for the underlying data source.

    Adds output options for the underlying data source.

    Since

    1.4.0

  28. def options(options: Map[String, String]): DataFrameWriter[T]

    Permalink

    (Scala-specific) Adds output options for the underlying data source.

    (Scala-specific) Adds output options for the underlying data source.

    Since

    1.4.0

  29. def orc(path: String): Unit

    Permalink

    Saves the content of the DataFrame in ORC format at the specified path.

    Saves the content of the DataFrame in ORC format at the specified path. This is equivalent to:

    format("orc").save(path)

    You can set the following ORC-specific option(s) for writing ORC files:

    • compression (default snappy): compression codec to use when saving to file. This can be one of the known case-insensitive shorten names(none, snappy, zlib, and lzo). This will override orc.compress.
    Since

    1.5.0

    Note

    Currently, this method can only be used after enabling Hive support

  30. def parquet(path: String): Unit

    Permalink

    Saves the content of the DataFrame in Parquet format at the specified path.

    Saves the content of the DataFrame in Parquet format at the specified path. This is equivalent to:

    format("parquet").save(path)

    You can set the following Parquet-specific option(s) for writing Parquet files:

    • compression (default is the value specified in spark.sql.parquet.compression.codec): compression codec to use when saving to file. This can be one of the known case-insensitive shorten names(none, snappy, gzip, and lzo). This will override spark.sql.parquet.compression.codec.
    Since

    1.4.0

  31. def partitionBy(colNames: String*): DataFrameWriter[T]

    Permalink

    Partitions the output by the given columns on the file system.

    Partitions the output by the given columns on the file system. If specified, the output is laid out on the file system similar to Hive's partitioning scheme. As an example, when we partition a dataset by year and then month, the directory layout would look like:

    • year=2016/month=01/
    • year=2016/month=02/

    Partitioning is one of the most widely used techniques to optimize physical data layout. It provides a coarse-grained index for skipping unnecessary data reads when queries have predicates on the partitioned columns. In order for partitioning to work well, the number of distinct values in each column should typically be less than tens of thousands.

    This was initially applicable for Parquet but in 1.5+ covers JSON, text, ORC and avro as well.

    Annotations
    @varargs()
    Since

    1.4.0

  32. def save(): Unit

    Permalink

    Saves the content of the DataFrame as the specified table.

    Saves the content of the DataFrame as the specified table.

    Since

    1.4.0

  33. def save(path: String): Unit

    Permalink

    Saves the content of the DataFrame at the specified path.

    Saves the content of the DataFrame at the specified path.

    Since

    1.4.0

  34. def saveAsTable(tableName: String): Unit

    Permalink

    Saves the content of the DataFrame as the specified table.

    Saves the content of the DataFrame as the specified table.

    In the case the table already exists, behavior of this function depends on the save mode, specified by the mode function (default to throwing an exception). When mode is Overwrite, the schema of the DataFrame does not need to be the same as that of the existing table.

    When mode is Append, if there is an existing table, we will use the format and options of the existing table. The column order in the schema of the DataFrame doesn't need to be same as that of the existing table. Unlike insertInto, saveAsTable will use the column names to find the correct column positions. For example:

    scala> Seq((1, 2)).toDF("i", "j").write.mode("overwrite").saveAsTable("t1")
    scala> Seq((3, 4)).toDF("j", "i").write.mode("append").saveAsTable("t1")
    scala> sql("select * from t1").show
    +---+---+
    |  i|  j|
    +---+---+
    |  1|  2|
    |  4|  3|
    +---+---+

    When the DataFrame is created from a non-partitioned HadoopFsRelation with a single input path, and the data source provider can be mapped to an existing Hive builtin SerDe (i.e. ORC and Parquet), the table is persisted in a Hive compatible format, which means other systems like Hive will be able to read this table. Otherwise, the table is persisted in a Spark SQL specific format.

    Since

    1.4.0

  35. def sortBy(colName: String, colNames: String*): DataFrameWriter[T]

    Permalink

    Sorts the output in each bucket by the given columns.

    Sorts the output in each bucket by the given columns.

    This is applicable for Parquet, JSON and ORC.

    Annotations
    @varargs()
    Since

    2.0

  36. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  37. def text(path: String): Unit

    Permalink

    Saves the content of the DataFrame in a text file at the specified path.

    Saves the content of the DataFrame in a text file at the specified path. The DataFrame must have only one column that is of string type. Each row becomes a new line in the output file. For example:

    // Scala:
    df.write.text("/path/to/output")
    
    // Java:
    df.write().text("/path/to/output")

    You can set the following option(s) for writing text files:

    • compression (default null): compression codec to use when saving to file. This can be one of the known case-insensitive shorten names (none, bzip2, gzip, lz4, snappy and deflate).
    Since

    1.6.0

  38. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  39. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  40. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  41. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped