7.7 KiB
id | title | sidebar_label |
---|---|---|
tutorial-compaction | Tutorial: Compacting segments | Compacting segments |
This tutorial demonstrates how to compact existing segments into fewer but larger segments in Apache Druid.
There is some per-segment memory and processing overhead during query processing. Therefore, it can be beneficial to reduce the total number of segments. See Segment size optimization for details.
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 of HOUR
and creates between one and three segments per hour.
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
:
bin/post-index-task --file quickstart/tutorial/compaction-init-index.json --url http://localhost:8081
maxRowsPerSegment
in the tutorial ingestion spec is set to 1000 to generate multiple segments per hour for demonstration purposes. Do not use this spec in production.
After the ingestion completes, navigate to http://localhost:8888/unified-console.html#datasources in a browser to see the new datasource in the Druid console.
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:
Run a COUNT query on the datasource to verify there are 39,244 rows:
dsql> select count(*) from "compaction-tutorial";
┌────────┐
│ EXPR$0 │
├────────┤
│ 39244 │
└────────┘
Retrieved 1 row in 1.38s.
Compact the data
Now you compact these 51 small segments and retain the segment granularity of HOUR
.
The Druid distribution includes a compaction task spec for this tutorial datasource at quickstart/tutorial/compaction-keep-granularity.json
:
{
"type": "compact",
"dataSource": "compaction-tutorial",
"interval": "2015-09-12/2015-09-13",
"tuningConfig" : {
"type" : "index_parallel",
"partitionsSpec": {
"type": "dynamic"
},
"maxRowsInMemory" : 25000
}
}
This compacts all segments for the interval 2015-09-12/2015-09-13
in the compaction-tutorial
datasource.
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.
Submit the compaction task now:
bin/post-index-task --file quickstart/tutorial/compaction-keep-granularity.json --url http://localhost:8081
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.
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 Druid console displays both sets of segments, queries only read from the new compacted segments.
Run a COUNT query on compaction-tutorial
again to verify the number of rows remains 39,244:
dsql> select count(*) from "compaction-tutorial";
┌────────┐
│ EXPR$0 │
├────────┤
│ 39244 │
└────────┘
Retrieved 1 row in 1.30s.
After the Coordinator has been running for at least 15 minutes, the segments view only shows the new 24 segments, one for each hour:
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 create DAY
granularity segments at quickstart/tutorial/compaction-day-granularity.json
:
{
"type": "compact",
"dataSource": "compaction-tutorial",
"interval": "2015-09-12/2015-09-13",
"tuningConfig" : {
"type" : "index_parallel",
"partitionsSpec": {
"type": "dynamic"
},
"maxRowsInMemory" : 25000,
"forceExtendableShardSpecs" : true
},
"granularitySpec" : {
"segmentGranularity" : "DAY",
"queryGranularity" : "none"
}
}
Note that segmentGranularity
is set to DAY
in this compaction task spec.
Submit this task now:
bin/post-index-task --file quickstart/tutorial/compaction-day-granularity.json --url http://localhost:8081
It takes some time before the Coordinator marks the old input segments as unused, so you may see an intermediate state with 25 total segments. Eventually, only one DAY granularity segment remains:
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.