Query and download data

Updated at:

Ad hoc queries allow you to create and run custom query statements and download the query results based on your business needs. For example, after you complete the development of a compute task, you can use an ad hoc query to verify that the task runs as expected. This topic describes how to create an ad hoc query and download the query results.

Background

Ad hoc query tasks use the compute engine configured for your current project to query data. If you use a Hadoop compute engine, you can enable the Impala Task feature. After you enable this feature, you can create not only Hive SQL ad hoc queries but also Impala SQL ad hoc queries. Because Impala uses in-memory computing, its ad hoc queries offer faster response times than Hive SQL ad hoc queries. Dataphin also allows you to quickly switch between Impala SQL and Hive SQL task types, so you do not need to rewrite the same query code for existing tasks. For more information, see Appendix: Switching task types.

Prerequisites

To create an Impala SQL ad hoc query, you must enable Impala Task in the Hadoop compute engine. For more information, see Create a Hadoop compute engine.

To download query results, ensure you have download permissions for the project and that data download (for both full data and sample data) is enabled for the project. For more information, see Manage permissions and compute engines for a project, and Data download configuration.

Limitations

  • To use the query acceleration feature, you must purchase the query acceleration service.

  • The Basic Edition does not support downloading the full data from ad hoc query results.

Create an ad hoc query

  1. In the top navigation bar of the Dataphin homepage, choose R&D > Data Development.

  2. In the top navigation bar, select a Project. If you are in Dev-Prod mode, you also need to select an Environment.

  3. In the left-side navigation pane, choose Ad-hoc Query. In the ad hoc query list, click the image icon. You can create a Compute Engine Ad-hoc Query or a Database SQL Ad-hoc Query.

  4. In the Create Ad-hoc Query dialog box, configure the following parameters.

    Note

    If your compute engine is Hadoop and the Impala task feature is enabled, you can create a Hive SQL Ad-hoc Query or an Impala SQL Ad-hoc Query.

    Parameter

    Description

    Name

    Enter a name for the ad hoc query.

    The name can be up to 256 characters in length and cannot contain vertical bars (|), forward slashes (/), backslashes (\), colons (:), question marks (?), angle brackets (<>), asterisks (*), or double quotation marks (").

    Description

    Enter a brief description for the ad hoc query.

    Select Directory

    Select the directory where the task is stored. The default directory is Temporary Code.

    If no directory exists, you can create a New Folder. To do so, follow these steps:

    1. Above the compute task list on the left, click the image icon to open the New Folder dialog box.

    2. In the New Folder dialog box, enter a Name for the folder and, if necessary, select its location under Select Directory.

    3. Click OK.

    Data Source Type

    Select a data source type for the SQL task. For more information about the supported data source types, see the Offline Development - Database SQL column in Data sources supported by Dataphin.

    Note

    This parameter is available only when you create a Database SQL Ad-hoc Query.

    Data Source

    Select the data source for the ad hoc query. If no data source is available, click + New Data Source to create one.

    Note

    This parameter is available only when you create a Database SQL Ad-hoc Query.

    Catalog

    If the data source type is Presto or Trino, you must also configure the Catalog after you configure the data source.

    Database/Schema

    If the data source type is MySQL, PostgreSQL, AnalyticDB for PostgreSQL, Oracle, Presto, GaussDB (DWS), Microsoft SQL Server, ClickHouse, Hologres, Doris, openGauss, StarRocks, DM, OceanBase (Oracle Mode), SelectDB, Trino, PolarDB-X 2.0, TDSQL for MySQL, TDSQL for PostgreSQL, TDengine, or GBase 8a, you must also configure the schema after you configure the data source.

  5. Click OK. The ad hoc query file is created.

  6. (Optional) You can enable and configure query acceleration. After you enable it, you must select an acceleration method.

    Acceleration Source: Accelerates queries by using a dedicated acceleration source. This method requires you to select an acceleration source and a resource group.

    • Acceleration Source: You can select an acceleration source that is bound to the compute engine of the current project. If no acceleration source is available, click New Acceleration Source to create one.

    • Resource Group (Optional): You can select a configured resource group under the selected acceleration source.

    Note

    Ensure your SQL statement complies with the syntax specifications. For example, if the acceleration source type is StarRocks, the ad hoc query runs using StarRocks syntax.

    MCQA: Accelerates queries using MaxCompute query acceleration (MCQA). Each tenant has limits on the number of MCQA jobs and their concurrency, which may cause acceleration to fail or execution errors to occur. For more information, see Query Acceleration (MCQA). You can disable MCQA by choosing Management Center > System Settings > Development Platform > Query Acceleration.

    The supported acceleration methods vary depending on the compute engine and scenario.

    If the current tenant uses MaxCompute as the compute engine:

    • If you have not purchased query acceleration but have enabled Query Acceleration in the development platform, the acceleration method is MCQA.

    • If you have purchased query acceleration, enabled Query Acceleration in the development platform, and the compute engine for the current project is not bound to an acceleration source, the acceleration method is MCQA.

    • If you have purchased query acceleration and the compute engine for the current project is bound to an acceleration source, you can select Acceleration Source or MCQA as the acceleration method.

    If the current tenant uses Hadoop as the compute engine: If you have purchased query acceleration and the cluster for the project's compute engine is bound to an acceleration source, the acceleration method for Hive SQL, Impala SQL, and Spark SQL ad hoc queries is Acceleration Source.

  7. Write the code to query data based on the project's compute engine, the database SQL type, or the acceleration source type.

  8. After you finish writing the code, click Run at the top of the editor.

  9. After the query runs successfully, view the results in the Console.

Download the result data

  1. After the query runs successfully, click the image icon in the upper-right corner of the console to download the results.

  2. In the Data Download dialog box, select a Download Data Range and a Download Format.

    • Download Data Range: You can download Full Data or Sample Data.

      • Full Data Download: Downloading full data may take a long time. You can check the Message Center or view the run history to track the download progress.

        Important

        A full data download creates a temporary table based on the query statement. For Spark SQL ad hoc queries on a Hadoop compute engine, the temporary table created for a full data download uses the default storage format configured in Management Center > System Settings > Development Platform > Table Management Settings.

        For example, if the default storage format in Table Management Settings is hudi, the system adds a using hudi clause when it creates the temporary table. If the default storage format is Engine Default, the system does not specify a storage format when it creates the temporary table.

      • Sample Data Download: By default, queries return only a sample of the data. You can configure the record limit by choosing Management Center > Standard Settings > Data Download. For more information, see Data download configuration.

    • Download Format: If you select Sample Data for the download range, you can select CSV or Excel as the download format. If you select Full Data, only CSV is supported.

  3. Click OK to start the data download.

    If you select Full Data, the system begins preparing the data after you click OK. When preparation is complete, click OK again to start the download.

Note
  • If download approval is enabled in Standard Settings, you must submit a Data Download Request to download data. After the request is approved, you can view the approval task and download the data from the current results or the run history. For more information, see Data download approval configuration.

  • If the watermarking feature for data downloads is enabled, watermarks are automatically added to downloaded Excel files. Downloaded data files are retained for 30 days. For more information, see Data download approval configuration.

Appendix: Switching task types

If your project uses a Hadoop compute engine and the Impala Task is enabled, you can switch between the Impala SQL and Hive SQL task types.

  1. In the ad hoc query list, find the target Impala SQL or Hive SQL task. Click the image icon next to the task name and select Modify Type.

  2. In the Modify Type dialog box, select the target type. The following figure shows how to switch an Impala SQL task type to a Hive SQL task type.image.png

  3. Click OK to switch the task type.