Export data in any way with IntelliJ-based IDEs

In DataGrip, or any other JetBrains IDE featuring database support, there is a powerful engine to export data in many formats. You can even create your own export format.

ExportSq1

Let’s look closer at this engine.

Export directions

Any table/view or result-set can be exported to a file or copied to the clipboard.

To export to file:
      — Context menu on a table/view and → Dump data to file.
      — Context menu on a query and → Execute to file.
      — In the toolbar of the result-set or data editor, select Dump data button → To File…

DumpToFile

To export to clipboard:

      — Select the data you need within a result-set or data editor and press Copy or Ctrl/Cmd+C.
      — In the toolbar of a result-set or data editor, select Dump data button → To Clipboard

Default extractors

We’ll talk about copying data to the clipboard from the data editor, but the same applies to other described ways to export data.

Look to the left of the Dump data button for a drop-down where you can choose the extractor — the format to export data in.

ChooseSQLInserts

There are several built-in formats as you can see. Some of them let you export data as a set of INSERT/UPDATE statements, while others export data as text such as CSV, JSON, HTML, etc. For more details on how they work, please visit this page.

Still, users may need to extract data in many different ways.

Twitter

Creating a DSV-based extractor

Let’s see how we can extend the default functionality.

To create your own format based on CSV (or any DSV format), select Configure CSV formats…

ConfigureDSV

In this dialog, in addition to changing the existing CSV and TSV extractors, you can create your own. For example, Confluence Wiki Markup.

ConfluenceWikiMarkup

Once created, it appears among the other extractors.

ConfluenceWikiMarkuipInMenu

Creating any text extractor with scripting

For the more complicated cases, consider using scripting extractors. Several of them are already there such as CSV-Groovy.csv.groovy, HTML-Groove.html.groovy, and others. These scripts are written in Groovy, but they can also be written in JavaScript. Our examples use Groovy.

Looking closely at the file name, CSV-Groovy.csv.groovy:

      CSV-Groovy is just the name of the script;
      csv is the extension of the result file if you extract to file;
      groovy is the extension of the script itself. It helps IntelliJ IDEA highlight your code if that’s where you create/edit your scripts.

Scripts are usually located in Scratches and Consoles/Extensions/Database Tools and SQL/data/extractors. Or you can select Go to scripts directory in the extractor menu to navigate there.

ChooseGoToSCriptsDirewctory

You should edit an existing extractor or just add your own to this folder.

Let’s create an extractor which will dump your data to CSV format, but to just one row. It can be useful if you are going to paste these values into an IN operator in a WHERE clause.

Here is the diff view of two scripts: the existing CSV-Groovy.csv.groovy and our new one which we’ll name CSV-ToOneRow-Groovy.csv.groovy.

ScriptsDif

Note the available context bindings:

These objects are equal when dumping the whole page to file.

Where:

 

DasTable has two important methods:

Before v2017.3

Since v2017.3

 

Additional information about the API can be found here.

When you create or edit Groovy scripts in IntelliJ IDEA, and have Groovy installed, coding assistance is available.

CodingAssistance

Once the new script file is in the folder, you can use the extractor!

ChooseCSVGToOneRow

Copy these values and paste them into the query.

ToOneRowSQL

Here’s another example based on SQL-Insert-Statements.sql.groovy. MySQL and PostgreSQL allow using multi-row syntax. To use this type of extractor, create a new SQL-Inserts-MultirowSynthax.sql.groovy file in the scripting folder.

SQLInsertsMultiRowExtractor

Again, simply select it in the menu.

ChooseSQLMultiRow
The result:

InserMultiRowSQL

There are already some user-generated extractors on GitHub which everybody can use:

Extractor to copy tab separated query results to paste into Excel with comma decimal separator.
DBunit XML.
Txt-file with fixed length columns. (Here’s a very similar one).
Php array.
Markdown.

We hope this tutorial will help you tweak DataGrip to your specific needs and encourage you to share extractors for all to use.

The DataGrip Team

This entry was posted in Features, Tutorial, Uncategorized and tagged . Bookmark the permalink.

20 Responses to Export data in any way with IntelliJ-based IDEs

  1. Mike says:

    At the head of the article is written «or any other JetBrains IDE featuring database support».
    I tried to find the same menus in IDEA 2017.3 EAP Ultimate Edition, but didn’t find it.

    The question is does IntelliJ IDEA 2017.3 EAP (Ultimate Edition) contains the features, described here? If yes, how can I convert XML to SQL with IDEA?

    Thanks.

    • Maksim Sobolevskiy says:

      Can you please send us a screenshot of the absence of this menu? It should be there. Now you cannot convert XML to SQL, because this is import, not export. We considering extending import functionality as well.

  2. Peter says:

    I would like to format dates to dd-mm-yyyy (the standard here). Is there a way to obtain the columns ‘raw’ value and apply a format or do I have to resort to cut+paste the string value from the FORMATTER.format ?
    I think it is a pity that FORMATTER.format does not take any localisation into consideration.

  3. Mahesh says:

    What methods are available for DataRow and DataColun? Are there any JavaDoc available for the objects in data extractors?

    • Maksim Sobolevskiy says:

      Sorry, there is no javadoc for them. But, here are the interfaces:


      public interface DataRow {
      int rowNumber();
      boolean first();
      boolean last();
      @NotNull
      Listdata();
      @Nullable
      Object value(@NotNull DataColumn column);
      }>



      public interface DataColumn {
      int columnNumber();
      @NotNull
      String name();
      }

  4. Dan says:

    What are the limits on the size of the export? what if my SQL result is 1 MM rows or 1TB? (made up numbers, not sure where it starts being a problem).

    How do we export such sizes? how do you suggest scheduling exports as opposed to manually triggered?

    • Maksim Sobolevskiy says:

      What export do you mean? To CSV? Or with the help of extractor?

      • Dan says:

        1/ Could I use a “copy” or “UNLOAD” (similar to unload supported by REDSHIFT” and export the data (the result of the SQL) to external target location? if yes, any size limits?

        2/ if I use an extractor, I have to trigger it via the UI right? any limits there?

        is it all running internal or is it using a JDBC/ODBC connection? since those connections I am sure have some limits on the size they can support unless you implement chunking.

  5. Daniel says:

    Is it possible to use the “Add table definiton” option with a custom extractor? I’m trying to do multirow syntax _with_ create table definition. Not seeing a way to do that…

  6. Matthias Vill says:

    Is there a way to get a column’s type? Probably via TABLE? The DataColumn seems to lack this and DataGrip does not ship a groovy extension :(

    • Maksim Sobolevskiy says:

      1. TABLE.getDasChildren(com.intellij.database.model.ObjectKind.COLUMN) will give you Iterable
      2. Cast it to DasColumn
      3. Then getDataType().getSpecification()

      • Matthias Vill says:

        Thank you! Ended up with this:

        COL_TYPES = new HashMap()
        TABLE.getDasChildren(com.intellij.database.model.ObjectKind.COLUMN).each { column ->
        COL_TYPES.put(column.getName(), column.getDataType().getSpecification())
        }

        Now is there a way to get the DIALECT-specific quoting of table-/column-names?

        • Maksim Sobolevskiy says:

          @Contract(“null,_,_->null;!null,_,_->!null”)
          String quoteIdentifier(String identifier, boolean force, boolean suppress);

          DIALECT.quoteIdentifier()

          • Matthias Vill says:

            Thank you again!

            Is there something similar to TABLE for multi-table selects? When hovering over the result-column-headers types are provided, but I don’t see how I could get to them from a groovy-export-script.

          • Maksim Sobolevskiy says:

            Finally, that is the stage where my answer is “No, it’s impossible” :)

  7. Oscar Camacho says:

    Hi!
    I’m currently trying to create custom reports with this tool.
    I need to get the row count in all reports. Any ideas on how to do this?

    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *