Load files with SQL-based ingestion

Load files using SQL - 图1info

This page describes SQL-based batch ingestion using the druid-multi-stage-query extension, new in Druid 24.0. Refer to the ingestion methods table to determine which ingestion method is right for you.

This tutorial demonstrates how to generate a query that references externally hosted data using the Connect external data wizard.

The following example uses EXTERN to query a JSON file located at https://druid.apache.org/data/wikipedia.json.gz.

Although you can manually create a query in the UI, you can use Druid to generate a base query for you that you can modify to meet your requirements.

To generate a query from external data, do the following:

  1. In the Query view of the web console, click Connect external data.

  2. On the Select input type screen, choose HTTP(s) and enter the following value in the URIs field: https://druid.apache.org/data/wikipedia.json.gz. Leave the HTTP auth username and password blank.

  3. Click Connect data.

  4. On the Parse screen, you can perform additional actions before you load the data into Druid:

    • Expand a row to see what data it corresponds to from the source.
    • Customize how Druid handles the data by selecting the Input format and its related options, such as adding JSON parser features for JSON files.
  5. When you’re ready, click Done. You’re returned to the Query view where you can see the starter query that will insert the data from the external source into a table named wikipedia.

    Show the query

    1. REPLACE INTO "wikipedia" OVERWRITE ALL
    2. WITH ext AS (SELECT *
    3. FROM TABLE(
    4. EXTERN(
    5. '{"type":"http","uris":["https://druid.apache.org/data/wikipedia.json.gz"]}',
    6. '{"type":"json"}',
    7. '[{"name":"isRobot","type":"string"},{"name":"channel","type":"string"},{"name":"timestamp","type":"string"},{"name":"flags","type":"string"},{"name":"isUnpatrolled","type":"string"},{"name":"page","type":"string"},{"name":"diffUrl","type":"string"},{"name":"added","type":"long"},{"name":"comment","type":"string"},{"name":"commentLength","type":"long"},{"name":"isNew","type":"string"},{"name":"isMinor","type":"string"},{"name":"delta","type":"long"},{"name":"isAnonymous","type":"string"},{"name":"user","type":"string"},{"name":"deltaBucket","type":"long"},{"name":"deleted","type":"long"},{"name":"namespace","type":"string"},{"name":"cityName","type":"string"},{"name":"countryName","type":"string"},{"name":"regionIsoCode","type":"string"},{"name":"metroCode","type":"long"},{"name":"countryIsoCode","type":"string"},{"name":"regionName","type":"string"}]'
    8. )
    9. ))
    10. SELECT
    11. TIME_PARSE("timestamp") AS __time,
    12. isRobot,
    13. channel,
    14. flags,
    15. isUnpatrolled,
    16. page,
    17. diffUrl,
    18. added,
    19. comment,
    20. commentLength,
    21. isNew,
    22. isMinor,
    23. delta,
    24. isAnonymous,
    25. user,
    26. deltaBucket,
    27. deleted,
    28. namespace,
    29. cityName,
    30. countryName,
    31. regionIsoCode,
    32. metroCode,
    33. countryIsoCode,
    34. regionName
    35. FROM ext
    36. PARTITIONED BY DAY
  6. Review and modify the query to meet your needs. For example, you can rename the table or change segment granularity. To partition by something other than ALL, include TIME_PARSE("timestamp") AS __time in your SELECT statement.

    For example, to specify day-based segment granularity, change the partitioning to PARTITIONED BY DAY:

    1. INSERT INTO ...
    2. SELECT
    3. TIME_PARSE("timestamp") AS __time,
    4. ...
    5. ...
    6. PARTITIONED BY DAY
  7. Optionally, select Preview to review the data before you ingest it. A preview runs the query without the REPLACE INTO clause and with an added LIMIT. You can see the general shape of the data before you commit to inserting it. The LIMITs make the query run faster but can cause incomplete results.

  8. Click Run to launch your query. The query returns information including its duration and the number of rows inserted into the table.

Query the data

You can query the wikipedia table after the ingestion completes. For example, you can analyze the data in the table to produce a list of top channels:

  1. SELECT
  2. channel,
  3. COUNT(*)
  4. FROM "wikipedia"
  5. GROUP BY channel
  6. ORDER BY COUNT(*) DESC

With the EXTERN function, you could run the same query on the external data directly without ingesting it first:

Show the query

  1. SELECT
  2. channel,
  3. COUNT(*)
  4. FROM TABLE(
  5. EXTERN(
  6. '{"type": "http", "uris": ["https://druid.apache.org/data/wikipedia.json.gz"]}',
  7. '{"type": "json"}',
  8. '[{"name": "added", "type": "long"}, {"name": "channel", "type": "string"}, {"name": "cityName", "type": "string"}, {"name": "comment", "type": "string"}, {"name": "commentLength", "type": "long"}, {"name": "countryIsoCode", "type": "string"}, {"name": "countryName", "type": "string"}, {"name": "deleted", "type": "long"}, {"name": "delta", "type": "long"}, {"name": "deltaBucket", "type": "string"}, {"name": "diffUrl", "type": "string"}, {"name": "flags", "type": "string"}, {"name": "isAnonymous", "type": "string"}, {"name": "isMinor", "type": "string"}, {"name": "isNew", "type": "string"}, {"name": "isRobot", "type": "string"}, {"name": "isUnpatrolled", "type": "string"}, {"name": "metroCode", "type": "string"}, {"name": "namespace", "type": "string"}, {"name": "page", "type": "string"}, {"name": "regionIsoCode", "type": "string"}, {"name": "regionName", "type": "string"}, {"name": "timestamp", "type": "string"}, {"name": "user", "type": "string"}]'
  9. )
  10. )
  11. GROUP BY channel
  12. ORDER BY COUNT(*) DESC

Further reading

See the following topics to learn more: