Export data to CSV or other delimited files

This guide shows how to export table data from Deephaven to CSV and other delimited files with writeCsv.

Tip

You can also export a table to a CSV file from the Deephaven UI with Table Options > Download CSV.

writeCsv

Note

The Deephaven server resolves file paths. If you run Deephaven in Docker, the path refers to the container's file system. The default Docker Compose setup mounts the ./data directory of your Deephaven deployment as the /data Docker data volume in the container. A file written to /data/TrigFunctions.csv appears at ./data/TrigFunctions.csv on your computer. If you run Deephaven without Docker, replace /data with a directory that exists on the server's machine.

Write a table to a CSV file

Let's create a table to export. It contains 100 rows of trigonometric values, made with emptyTable and update:

The simplest way to use writeCsv is to pass two arguments:

  • The Deephaven source table.
  • The path of the output CSV file.

The first lines of TrigFunctions.csv look like this:

Null values

Null values are common in tables. By default, writeCsv writes them as empty fields.

To see this, let's create a table with null values. The example below uses the ternary conditional operator to fill the SinX column with a large number of nulls.

The SinX column contains many null cells. The example below writes this table to a CSV file called TrigFunctionsWithNulls.csv.

The first lines of TrigFunctionsWithNulls.csv show the null SinX values as empty fields:

To write nulls as (null) instead, use the overload that takes a boolean nullsAsEmpty argument right after the file path, and pass false:

The first lines of TrigFunctionsWithNullStrings.csv show (null) in place of each null SinX value:

Column selection

To write only some columns, pass their names as extra arguments after the file path. The example below writes the X and CosX columns of the sourceWithNulls table created in Null values.

Cosine.csv contains only the X and CosX columns:

Other delimiters, compression, and time zone

By default, writeCsv separates fields with commas. To use a different delimiter, call the overload that takes a separator argument. The same overload also writes bz2-compressed files and writes Instant values in a time zone other than the system default. That overload has this signature:

Between the file path and the separator, it takes these arguments, in order:

  • compressed: whether to compress the file with bz2. When true, writeCsv appends .bz2 to the file path.
  • timeZone: the time zone in which to write Instant values.
  • nullsAsEmpty: whether to write nulls as empty fields.

In this overload, the boolean right after the path is compressed, not nullsAsEmpty as in Null values.

The example below writes the sourceWithNulls table created in Null values as a tab-separated file, with the same defaults that the two-argument call uses: no compression, the system default time zone, and nulls as empty fields.

Write a ticking table

Deephaven tables can tick, updating in real time as new data arrives. writeCsv writes the rows that a table holds when the call runs, so the file is a point-in-time copy of a live table. It reads those rows directly, without taking a snapshot, so a table that updates during the write can produce an inconsistent file. To write a consistent copy, take a snapshot of the table first and write the snapshot.

The example below creates a ticking table with timeTable:

A new time table has no rows until Deephaven runs its first update cycle, which happens after the command that creates the table finishes. Writing the table in that same command produces a file with only the header row. Run the write as a separate command, after the table has ticked:

To keep a file up to date as a table ticks, see How can I periodically write a ticking table to disk?.