Export query results to CSV with Beeline

How Beeline's csv2, tsv2 and dsv output formats turn Hive query results into delimited files, what changes in the process, and when another export path is the better fit.

Beeline is the JDBC command-line client for HiveServer2. It is usually used interactively, but it can also print query results in delimited formats and send them to a file. This review explains what that conversion does to your data, where it holds up, and where it causes problems. It uses Beeline's documented options and general CSV behavior only. No version-specific test results were supplied, so confirm each option against the help output of the Beeline build you actually run.

Short verdict

Beeline's csv2 output is a reasonable choice for small and moderate result sets that a person or script will consume on the client machine. Its main benefit is convenience: one command runs the query and writes a delimited file, with no extra tooling or access to the cluster file system. The main tradeoff is that every row passes through a single client process and its standard output. Large exports can therefore be slow, and the file can be corrupted by stray log lines or by careless post-processing. For very large results, or for data that will be loaded straight back into another table, an export that writes directly to storage is usually more robust. Plain tab-separated or pipe-delimited text is the better choice when field values rarely contain the delimiter and the consumer cannot handle quoted fields.

What the conversion actually changes

Exporting with Beeline does not alter the stored table. It changes how the result set is serialized as text. Each column value is converted to its string form, a delimiter is placed between fields, and a line break ends each row. Depending on the format and settings, Beeline may also add a header row of column names and wrap some values in quotes. Characters are not reversed, mirrored, restyled or substituted. The only character-level changes come from delimiting, quoting and escaping.

The output format is selected with the --outputformat option. The values relevant to delimited export are:

  • csv2: comma-separated values, with quoting applied to values that need it.
  • tsv2: tab-separated values, with the same quoting rules.
  • dsv: delimiter-separated values, using the character given in --delimiterForDSV (the default is the pipe character).
  • csv and tsv: older formats that wrap every value in single quotes. Hive documentation describes them as deprecated in favor of csv2 and tsv2.

Quoting for csv2, tsv2 and dsv is controlled by the disable.quoting.for.sv system property. When quoting is on, a value that contains the delimiter, a double quote or a line break is wrapped in double quotes, and inner double quotes are doubled. When it is off, values are written raw. A comma inside a value then splits that value into two fields for any CSV reader. Defaults for this property have differed between Hive releases. Set it explicitly instead of relying on the default.

A typical export command

The command below runs one query and redirects the result to a file. The connection string, user and query are placeholders. Replace them with your HiveServer2 JDBC URL and your own query.

beeline -u "$HS2_JDBC_URL" -n "$HS2_USER" \
  --outputformat=csv2 \
  --showHeader=true \
  --silent=true \
  --incremental=true \
  -e "SELECT id, name, city FROM customers WHERE active = true" \
  > customers.csv

Each option has a specific job:

  • --silent=true reduces informational messages so they are less likely to mix with the data.
  • --showHeader controls whether the first line contains column names.
  • --incremental=true prints rows as they arrive instead of buffering the whole result to size the columns. This matters for memory use on larger results.
  • -f can replace -e to run a query stored in a file.
  • --nullemptystring=true changes how NULL values are printed, so check what your consumer expects before setting it.

If the system property needs to be set, pass it to the JVM that runs Beeline. One common approach is through the client options environment variable that your Hive distribution reads. Exactly how system properties reach the Beeline JVM depends on the distribution and launcher script, so confirm the mechanism locally.

The illustrative output below is not captured from a real run. It shows what quoting should produce when one value contains a comma and quoting is enabled:

id,name,city
101,Ana Ruiz,Denver
102,"Lee, Jordan",Austin

If quoting were disabled, the second data row would read 102,Lee, Jordan,Austin, which a CSV parser treats as four fields instead of three.

Will the file remain usable after copying or sharing?

Several issues commonly break a Beeline CSV export once it leaves the terminal. Check each one before sharing the file.

  • Mixed streams: Beeline writes most status messages to standard error, but warnings, connection notices or a password prompt can still appear in an output file in some setups. Inspect the first and last lines of every export.
  • Naive delimiter swaps: converting tab-separated output to commas with a simple text replacement ignores quoting. Any value that already contains a comma will shift the columns. Use csv2 directly instead.
  • Embedded line breaks: values with newlines are only safe when quoting is enabled and the consumer is a real CSV parser. Line-based tools such as head, wc or grep will miscount rows.
  • NULL versus empty: decide whether missing values should be empty fields or a literal marker, then document the choice for whoever receives the file.

Spreadsheet applications add a separate set of risks because they interpret values on open:

  • Identifiers with leading zeros, long numeric IDs and date-like strings may be reformatted automatically.
  • Non-ASCII text can display incorrectly if the application guesses the wrong encoding.
  • Values that begin with characters such as an equals sign may be treated as formulas. This is a known risk when files containing untrusted data are shared.

How a given spreadsheet program behaves depends on its version and import settings. Test with the actual program and data your recipients will use rather than assuming one successful open applies everywhere.

For accessibility, a CSV file is plain text with no styling, so reading order follows row and column order. How well a screen reader handles it depends on the application used to open it. No assistive-technology testing was available for this review. If accessibility matters for your audience, test with their tools, and consider sharing a summary or a properly structured table alongside the raw file.

When a different export path fits better

Hive can write query results to a directory with INSERT OVERWRITE DIRECTORY, optionally with LOCAL and a ROW FORMAT DELIMITED clause. This bypasses client-side streaming, which suits large outputs. However, the files are written by the server side rather than on your workstation. Delimited output from this path generally has no header row and does not apply CSV-style quoting, so it carries the delimiter-collision risk described above. Choose it for bulk transfer between systems that agree on a delimiter. Choose Beeline csv2 when you need a quoted, header-bearing file on the client.

Gaps to verify locally

  • The default value of disable.quoting.for.sv in your Hive version.
  • Whether --incremental is already on by default in your build.
  • Which messages, if any, your Beeline build writes to standard output during a run.
  • How your recipients' spreadsheet or analysis tools handle encoding, quoted line breaks and long numeric values.
  • Performance for your data volume. No throughput figures were available for this review.