Tutorial: Query from deep storage

Query from deep storage allows you to query segments that are stored only in deep storage, which provides lower costs than if you were to load everything onto Historical processes. The tradeoff is that queries from deep storage may take longer to complete.

This tutorial walks you through loading example data, configuring load rules so that not all the segments get loaded onto Historical processes, and querying data from deep storage.

To run the queries in this tutorial, replace ROUTER:PORT with the location of the Router process and its port number. For example, use localhost:8888 for the quickstart deployment.

For more general information, see Query from deep storage.

If you are trying this feature on an existing cluster, make sure query from deep storage prerequisites are met.

Load example data

Use the Load data wizard or the following SQL query to ingest the wikipedia sample datasource bundled with Druid. If you use the wizard, make sure you change the partitioning to be by hour.

Partitioning by hour provides more segment granularity, so you can selectively load segments onto Historicals or keep them in deep storage.

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. )
  8. ) EXTEND ("isRobot" VARCHAR, "channel" VARCHAR, "timestamp" VARCHAR, "flags" VARCHAR, "isUnpatrolled" VARCHAR, "page" VARCHAR, "diffUrl" VARCHAR, "added" BIGINT, "comment" VARCHAR, "commentLength" BIGINT, "isNew" VARCHAR, "isMinor" VARCHAR, "delta" BIGINT, "isAnonymous" VARCHAR, "user" VARCHAR, "deltaBucket" BIGINT, "deleted" BIGINT, "namespace" VARCHAR, "cityName" VARCHAR, "countryName" VARCHAR, "regionIsoCode" VARCHAR, "metroCode" BIGINT, "countryIsoCode" VARCHAR, "regionName" VARCHAR))
  9. SELECT
  10. TIME_PARSE("timestamp") AS "__time",
  11. "isRobot",
  12. "channel",
  13. "flags",
  14. "isUnpatrolled",
  15. "page",
  16. "diffUrl",
  17. "added",
  18. "comment",
  19. "commentLength",
  20. "isNew",
  21. "isMinor",
  22. "delta",
  23. "isAnonymous",
  24. "user",
  25. "deltaBucket",
  26. "deleted",
  27. "namespace",
  28. "cityName",
  29. "countryName",
  30. "regionIsoCode",
  31. "metroCode",
  32. "countryIsoCode",
  33. "regionName"
  34. FROM "ext"
  35. PARTITIONED BY HOUR

Configure a load rule

The load rule configures Druid to keep any segments that fall within the following interval only in deep storage:

  1. 2016-06-27T00:00:00.000Z/2016-06-27T02:59:00.000Z

The JSON form of the rule is as follows:

  1. [
  2. {
  3. "interval": "2016-06-27T00:00:00.000Z/2016-06-27T02:59:00.000Z",
  4. "tieredReplicants": {},
  5. "useDefaultTierForNull": false,
  6. "type": "loadByInterval"
  7. }
  8. ]

The rest of the segments use the default load rules for the cluster. For the quickstart, that means all the other segments get loaded onto Historical processes.

You can configure the load rules through the API or the Druid console. To configure the load rules through the Druid console, go to Datasources > … in the Actions column > Edit retention rules. Then, paste the provided JSON into the JSON tab:

Query from deep storage - 图1

Verify the replication factor

Segments that are only available from deep storage have a replication_factor of 0 in the Druid system table. You can verify that your load rule worked as intended using the following query:

  1. SELECT "segment_id", "replication_factor", "num_replicas" FROM sys."segments" WHERE datasource = 'wikipedia'

You can also verify it through the Druid console by checking the Replication factor column in the Segments view.

Note that the number of replicas and replication factor may differ temporarily as Druid processes your retention rules.

Query from deep storage

Now that there are segments that are only available from deep storage, run the following query:

  1. SELECT page FROM wikipedia WHERE __time < TIMESTAMP'2016-06-27 00:10:00' LIMIT 10

With the context parameter:

  1. "executionMode": "ASYNC"

For example, run the following curl command:

  1. curl --location 'http://localhost:8888/druid/v2/sql/statements' \
  2. --header 'Content-Type: application/json' \
  3. --data '{
  4. "query":"SELECT page FROM wikipedia WHERE __time < TIMESTAMP'\''2016-06-27 00:10:00'\'' LIMIT 10",
  5. "context":{
  6. "executionMode":"ASYNC"
  7. }
  8. }'

This query looks for records with timestamps that precede 00:10:00. Based on the load rule you configured earlier, this data is only available from deep storage.

When you submit the query from deep storage through the API, you get the following response:

Show the response

  1. {
  2. "queryId": "query-6888b6f6-e597-456c-9004-222b05b97051",
  3. "state": "ACCEPTED",
  4. "createdAt": "2023-07-28T21:59:02.334Z",
  5. "schema": [
  6. {
  7. "name": "page",
  8. "type": "VARCHAR",
  9. "nativeType": "STRING"
  10. }
  11. ],
  12. "durationMs": -1
  13. }

Make sure you note the queryID. You’ll need it to interact with the query.

Compare this to if you were to submit the query to Druid SQL’s regular endpoint, POST /sql:

  1. curl --location 'http://localhost:8888/druid/v2/sql/' \
  2. --header 'Content-Type: application/json' \
  3. --data '{
  4. "query":"SELECT page FROM wikipedia WHERE __time < TIMESTAMP'\''2016-06-27 00:10:00'\'' LIMIT 10",
  5. "context":{
  6. "executionMode":"ASYNC"
  7. }
  8. }'

The response you get back is an empty response cause there are no records on the Historicals that match the query.

Get query status

Replace :queryId with the ID for your query and run the following curl command to get your query status:

  1. curl --location --request GET 'http://localhost:8888/druid/v2/sql/statements/:queryId' \
  2. --header 'Content-Type: application/json' \

Response for a running query

The response for a running query is the same as the response from when you submitted the query except the state is RUNNING instead of ACCEPTED.

Response for a completed query

A successful query also returns a pages object that includes the page numbers (id), rows per page (numRows), and the size of the page (sizeInBytes). You can pass the page number as a parameter when you get results to refine the results you get.

Note that sampleRecords has been truncated for brevity.

Show the response

  1. {
  2. "queryId": "query-6888b6f6-e597-456c-9004-222b05b97051",
  3. "state": "SUCCESS",
  4. "createdAt": "2023-07-28T21:59:02.334Z",
  5. "schema": [
  6. {
  7. "name": "page",
  8. "type": "VARCHAR",
  9. "nativeType": "STRING"
  10. }
  11. ],
  12. "durationMs": 87351,
  13. "result": {
  14. "numTotalRows": 152,
  15. "totalSizeInBytes": 9036,
  16. "dataSource": "__query_select",
  17. "sampleRecords": [
  18. [
  19. "Salo Toraut"
  20. ],
  21. [
  22. "利用者:ワーナー成増/放送ウーマン賞"
  23. ],
  24. [
  25. "Bailando 2015"
  26. ],
  27. ...
  28. ...
  29. ...
  30. ],
  31. "pages": [
  32. {
  33. "id": 0,
  34. "numRows": 152,
  35. "sizeInBytes": 9036
  36. }
  37. ]
  38. }
  39. }

Get query results

Replace :queryId with the ID for your query and run the following curl command to get your query results:

  1. curl --location 'http://ROUTER:PORT/druid/v2/sql/statements/:queryId'

Note that the response has been truncated for brevity.

Show the response

  1. [
  2. {
  3. "page": "Salo Toraut"
  4. },
  5. {
  6. "page": "利用者:ワーナー成増/放送ウーマン賞"
  7. },
  8. {
  9. "page": "Bailando 2015"
  10. },
  11. ...
  12. ...
  13. ...
  14. ]

Further reading