Prerequisites
This tutorial assumes you have already downloaded Apache Druid as described in the single-machine quickstart and have it running on your local machine. If you haven’t already, you should finish the following tutorials first:Load the initial data
This tutorial uses the Wikipedia edits sample data included with the Druid distribution. To load the initial data, you use an ingestion spec that loads batch data with segment granularity ofHOUR and creates between one and three segments per hour.
Submit the ingestion spec
You can review the ingestion spec at
quickstart/tutorial/compaction-init-index.json. Submit the spec as follows to create a datasource called compaction-tutorial:View the datasource
After the ingestion completes, navigate to http://localhost:8888/unified-console.html#datasources in a browser to see the new datasource in the web console.
In the Availability column for the 
In the Availability column for the compaction-tutorial datasource, click the link for 51 segments to view segments information for the datasource.The datasource comprises 51 segments, between one and three segments per hour from the input data:
Compact the data
Now you compact these 51 small segments and retain the segment granularity ofHOUR. The Druid distribution includes a compaction task spec for this tutorial datasource at quickstart/tutorial/compaction-keep-granularity.json:
2015-09-12/2015-09-13 in the compaction-tutorial datasource.
Understanding compaction behavior
The parameters in the
tuningConfig control the maximum number of rows present in each compacted segment and thus affect the number of segments in the compacted set.This datasource only has 39,244 rows. 39,244 is below the default limit of 5,000,000 maxRowsPerSegment for dynamic partitioning. Therefore, Druid only creates one compacted segment per hour.Monitor compaction progress
After the task finishes, refresh the segments view.Over time the Coordinator marks the original 51 segments as unused and subsequently removes them to leave only the new compacted segments.
The new compacted segments have a more recent version than the original segments. Even though the web console displays both sets of segments, queries only read from the new compacted segments.
By default, the Coordinator does not mark segments as unused until the Coordinator has been running for at least 15 minutes. During that time, you may see 75 total segments comprised of the old segment set and the new compacted set:

The new compacted segments have a more recent version than the original segments. Even though the web console displays both sets of segments, queries only read from the new compacted segments.Verify row count after compaction
Run a COUNT query on Expected result:
compaction-tutorial again to verify the number of rows remains 39,244:Compact the data with new segment granularity
You can also change the segment granularity in a compaction task to produce compacted segments with a different granularity from that of the input segments. The Druid distribution includes a compaction task spec to createDAY granularity segments at quickstart/tutorial/compaction-day-granularity.json:
segmentGranularity is set to DAY in this compaction task spec.
Learn more
This tutorial demonstrated how to use a compaction task spec to manually compact segments and how to optionally change the segment granularity for segments.- For more details, see Compaction.
- To learn about the benefits of compaction, see Segment optimization.



