This tutorial demonstrates how to query data in Apache Druid using SQL.

It assumes that you’ve completed the Quickstart or one of the following tutorials, since we’ll query datasources that you would have created by following one of them:

There are various ways to run Druid SQL queries: from the Druid console, using a command line utility and by posting the query by HTTP. We’ll look at each of these.

Query SQL from the Druid console

The Druid console includes a view that makes it easier to build and test queries, and view their results.

  1. Start up the Druid cluster, if it’s not already running, and open the Druid console in your web browser.

  2. Click Query from the header to open the Query view:

    Query view

    You can always write queries directly in the edit pane, but the Query view also provides facilities to help you construct SQL queries, which we will use to generate a starter query.

  3. Expand the wikipedia datasource tree in the left pane. We’ll create a query for the page dimension.

  4. Click page and then Show:page from the menu:

    Query select page

    A SELECT query appears in the query edit pane and immediately runs. However, in this case, the query returns no data, since by default the query filters for data from the last day, while our data is considerably older than that. Let’s remove the filter.

  5. In the datasource tree, click __time and Remove Filter.

    Clear WHERE filter

  6. Click Run to run the query.

    You should now see two columns of data, a page name and the count:

    Query results

    Notice that the results are limited in the console to about a hundred, by default, due to the Smart query limit feature. This helps users avoid inadvertently running queries that return an excessive amount of data, possibly overwhelming their system.

  7. Let’s edit the query directly and take a look at a few more query building features in the editor. Click in the query edit pane and make the following changes:

    1. Add a line after the first column, "page" and Start typing the name of a new column, "countryName". Notice that the autocomplete menu suggests column names, functions, keywords, and more. Choose “countryName” and add the new column to the GROUP BY clause as well, either by name or by reference to its position, 2.

    2. For readability, replace Count column name with Edits, since the COUNT() function actually returns the number of edits for the page. Make the same column name change in the ORDER BY clause as well.

      The COUNT() function is one of many functions available for use in Druid SQL queries. You can mouse over a function name in the autocomplete menu to see a brief description of a function. Also, you can find more information in the Druid documentation; for example, the COUNT() function is documented in Aggregation functions.

    The query should now be:

    1. SELECT
    2. "page",
    3. "countryName",
    4. COUNT(*) AS "Edits"
    5. FROM "wikipedia"
    6. GROUP BY 1, 2
    7. ORDER BY "Edits" DESC

    When you run the query again, notice that we’re getting the new dimension,countryName, but for most of the rows, its value is null. Let’s show only rows with a countryName value.

  8. Click the countryName dimension in the left pane and choose the first filtering option. It’s not exactly what we want, but we’ll edit it by hand. The new WHERE clause should appear in your query.

  9. Modify the WHERE clause to exclude results that do not have a value for countryName:

    1. WHERE "countryName" IS NOT NULL

    Run the query again. You should now see the top edits by country:

    Finished query

  10. Under the covers, every Druid SQL query is translated into a query in the JSON-based Druid native query format before it runs on data nodes. You can view the native query for this query by clicking ... and Explain SQL Query.

    While you can use Druid SQL for most purposes, familiarity with native query is useful for composing complex queries and for troubleshooting performance issues. For more information, see Native queries.

    Explain query

    Another way to view the explain plan is by adding EXPLAIN PLAN FOR to the front of your query, as follows:

    1. EXPLAIN PLAN FOR
    2. SELECT
    3. "page",
    4. "countryName",
    5. COUNT(*) AS "Edits"
    6. FROM "wikipedia"
    7. WHERE "countryName" IS NOT NULL
    8. GROUP BY 1, 2
    9. ORDER BY "Edits" DESC

    This is particularly useful when running queries from the command line or over HTTP.

  11. Finally, click ... and Edit context to see how you can add additional parameters controlling the execution of the query execution. In the field, enter query context options as JSON key-value pairs, as described in Context flags.

That’s it! We’ve built a simple query using some of the query builder features built into the Druid Console. The following sections provide a few more example queries you can try. Also, see Other ways to invoke SQL queries to learn how to run Druid SQL from the command line or over HTTP.

More Druid SQL examples

Here is a collection of queries to try out:

Query over time

  1. SELECT FLOOR(__time to HOUR) AS HourTime, SUM(deleted) AS LinesDeleted
  2. FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00'
  3. GROUP BY 1

Query example

General group by

  1. SELECT channel, page, SUM(added)
  2. FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00'
  3. GROUP BY channel, page
  4. ORDER BY SUM(added) DESC

Query example

Other ways to invoke SQL queries

Query SQL via dsql

For convenience, the Druid package includes a SQL command-line client, located at bin/dsql in the Druid package root.

Let’s now run bin/dsql; you should see the following prompt:

  1. Welcome to dsql, the command-line client for Druid SQL.
  2. Type "\h" for help.
  3. dsql>

To submit the query, paste it to the dsql prompt and press enter:

  1. dsql> SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10;
  2. ┌──────────────────────────────────────────────────────────┬───────┐
  3. page Edits
  4. ├──────────────────────────────────────────────────────────┼───────┤
  5. Wikipedia:Vandalismusmeldung 33
  6. User:Cyde/List of candidates for speedy deletion/Subpage 28
  7. Jeremy Corbyn 27
  8. Wikipedia:Administrators' noticeboard/Incidents │ 21 │
  9. │ Flavia Pennetta │ 20 │
  10. │ Total Drama Presents: The Ridonculous Race │ 18 │
  11. │ User talk:Dudeperson176123 │ 18 │
  12. │ Wikipédia:Le Bistro/12 septembre 2015 │ 18 │
  13. │ Wikipedia:In the news/Candidates │ 17 │
  14. │ Wikipedia:Requests for page protection │ 17 │
  15. └──────────────────────────────────────────────────────────┴───────┘
  16. Retrieved 10 rows in 0.06s.

Query SQL over HTTP

You can submit queries directly to the Druid Broker over HTTP.

The tutorial package includes an example file that contains the SQL query shown above at quickstart/tutorial/wikipedia-top-pages-sql.json. Let’s submit that query to the Druid Broker:

  1. curl -X 'POST' -H 'Content-Type:application/json' -d @quickstart/tutorial/wikipedia-top-pages-sql.json http://localhost:8888/druid/v2/sql

The following results should be returned:

  1. [
  2. {
  3. "page": "Wikipedia:Vandalismusmeldung",
  4. "Edits": 33
  5. },
  6. {
  7. "page": "User:Cyde/List of candidates for speedy deletion/Subpage",
  8. "Edits": 28
  9. },
  10. {
  11. "page": "Jeremy Corbyn",
  12. "Edits": 27
  13. },
  14. {
  15. "page": "Wikipedia:Administrators' noticeboard/Incidents",
  16. "Edits": 21
  17. },
  18. {
  19. "page": "Flavia Pennetta",
  20. "Edits": 20
  21. },
  22. {
  23. "page": "Total Drama Presents: The Ridonculous Race",
  24. "Edits": 18
  25. },
  26. {
  27. "page": "User talk:Dudeperson176123",
  28. "Edits": 18
  29. },
  30. {
  31. "page": "Wikipédia:Le Bistro/12 septembre 2015",
  32. "Edits": 18
  33. },
  34. {
  35. "page": "Wikipedia:In the news/Candidates",
  36. "Edits": 17
  37. },
  38. {
  39. "page": "Wikipedia:Requests for page protection",
  40. "Edits": 17
  41. }
  42. ]

Further reading

See the Druid SQL documentation for more information on using Druid SQL queries.

See the Queries documentation for more information on Druid native queries.