---
title: DataStreamWriter
description: "DataStreamWriter class in PySpark: Interface used to write a streaming DataFrame to external storage systems (for example, file systems and key-value stores). Use df.writeStream to access this."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter
---

# DataStreamWriter

Interface used to write a streaming DataFrame to external storage systems (for example, file systems and key-value stores). Use `df.writeStream` to access this.

## Syntax

```python
# Access through DataFrame
df.writeStream
```

## Methods

| Method                                                                                                                  | Description                                                                                                         |
| ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| [`outputMode(outputMode)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/outputMode.md) | Specifies how data of a streaming DataFrame is written to the sink. Options are `append`, `complete`, and `update`. |
| [`format(source)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/format.md)             | Specifies the output data source format.                                                                            |
| [`option(key, value)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/option.md)         | Adds an output option for the underlying data source.                                                               |
| [`options(**options)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/options.md)        | Adds multiple output options for the underlying data source.                                                        |
| [`partitionBy(*cols)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/partitionBy.md)    | Partitions the output by the given columns on the file system.                                                      |
| [`clusterBy(*cols)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/clusterBy.md)        | Clusters the output by the given columns.                                                                           |
| [`queryName(queryName)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/queryName.md)    | Specifies the name of the streaming query.                                                                          |
| [`trigger(**kwargs)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/trigger.md)         | Sets the trigger for the streaming query execution.                                                                 |
| [`foreach(f)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/foreach.md)                | Sets the output of the streaming query to be processed by the given function or object.                             |
| [`foreachBatch(func)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/foreachBatch.md)   | Sets the output of each microbatch to be processed by the given function.                                           |
| [`start(path)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/start.md)                 | Starts the execution of the streaming query and returns a `StreamingQuery` object.                                  |
| [`table(tableName)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/table.md)            | Alias for `toTable()`. Writes data to the specified table and returns a `StreamingQuery` object.                    |
| [`toTable(tableName)`](https://docs.databricks.com/aws/en/pyspark/reference/classes/datastreamwriter/toTable.md)        | Starts the execution of the streaming query, continually outputting results to the given table.                     |

## Examples

Load a rate stream, apply a transformation, write to the console, and stop after 3 seconds.

```python
import time
df = spark.readStream.format("rate").load()
df = df.selectExpr("value % 3 as v")
q = df.writeStream.format("console").start()
time.sleep(3)
q.stop()
```
