|
| 1 | +--- |
| 2 | +title: Use log compaction |
| 3 | +description: Learn how to use log compaction. |
| 4 | +ms.topic: how-to |
| 5 | +ms.custom: log-compaction |
| 6 | +ms.date: 10/7/2022 |
| 7 | +--- |
| 8 | + |
| 9 | +# Use log compaction |
| 10 | +This article shows you how to create log compaction feature in Event Hubs. To understand the details of log compaction, see [Log Compaction](log-compaction.md). |
| 11 | + |
| 12 | +In this article you'll, follow these key steps: |
| 13 | +- Create a compacted event hub/Kafka topic. |
| 14 | +- Publish events to a compacted event hub. |
| 15 | +- Consume events from a compacted event hub. |
| 16 | + |
| 17 | +> [!NOTE] |
| 18 | +> Log compation feature is available only in **premium** and **dedicated** tiers. |
| 19 | +
|
| 20 | +## Create a compacted event hub/Kafka topic |
| 21 | +This section shows you how to create a compacted event hub using Azure portal and an Azure Resource Manager (ARM) template. |
| 22 | + |
| 23 | +### [Azure portal](#tab/portal) |
| 24 | +You can create a compacted event hub using the Azure portal by following these steps. |
| 25 | + |
| 26 | +1. Navigate to your Event Hubs namespace. |
| 27 | +1. On the Event Hubs Namespace page, select Event Hubs in the left menu. |
| 28 | +1. At the top of the window, select + Event Hubs. |
| 29 | + :::image type="content" source="./media/event-hubs-quickstart-portal/create-event-hub4.png" alt-text="Screenshot of the Application Groups page in the Azure portal."::: |
| 30 | +1. Type a *name* for your event hub, and specify the *partition count*. Since we're creating a compacted event hub, select *compaction policy* as *compaction* and provide the desired value for *tombstone retention time*. |
| 31 | + :::image type="content" source="./media/event-hubs-log-compaction/enabling-compaction.png" alt-text="Screenshot of the Application Groups page in the Azure portal."::: |
| 32 | +1. Select *create* and create the compacted event hub. |
| 33 | + |
| 34 | +### [ARM template](#tab/arm) |
| 35 | +The following example shows how to create a compacted event hub/Kafka topic using an ARM template. |
| 36 | + |
| 37 | +```json |
| 38 | +"resources": [ |
| 39 | + { |
| 40 | + "apiVersion": "2017-04-01", |
| 41 | + "name": "[parameters('eventHubName')]", |
| 42 | + "type": "eventhubs", |
| 43 | + "dependsOn": [ |
| 44 | + "[resourceId('Microsoft.EventHub/namespaces/', parameters('eventHubNamespaceName'))]" |
| 45 | + ], |
| 46 | + "properties": { |
| 47 | + "partitionCount": "[parameters('partitionCount')]", |
| 48 | + "retentionDescription": { |
| 49 | + "cleanupPolicy": "compact", |
| 50 | + "tombstoneRetentionTimeInHours": "24" |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +] |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Triggering compaction |
| 61 | +Event Hubs service determines when the compaction job of a given compacted event hub should be executed. Compacted event hub reaches the compaction threshold when there are considerable number of events or the total size of a given event log grows significantly. |
| 62 | + |
| 63 | +## Publish event to a compacted topic |
| 64 | +Publishing events to a compacted event hub is the same as publishing events to a regular event hub. As the client application you only need to determine the compaction key, which you set using partition key. |
| 65 | + |
| 66 | +### Using Event Hubs SDK(AMQP) |
| 67 | +With Event Hubs SDK, you can set partition key and publish events as shown below: |
| 68 | + |
| 69 | +```csharp |
| 70 | +var enqueueOptions = new EnqueueEventOptions |
| 71 | +{ |
| 72 | + PartitionKey = "Key-1" |
| 73 | + |
| 74 | +}; |
| 75 | +await producer.EnqueueEventAsync(eventData, enqueueOptions); |
| 76 | +``` |
| 77 | + |
| 78 | +### Using Kafka |
| 79 | +With Kafka you can set the partition key when you create the `ProducerRecord` as shown below: |
| 80 | +```java |
| 81 | +ProducerRecord<String, String> record = new ProducerRecord<String, String>(TOPIC, "Key-1" , "Value-1"); |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +## Consuming events from a compacted topic |
| 86 | +There are no changes required at the consumer side to consume events from a compacted event hub. So, you can use any of the existing consumer applications to consume data from a compacted event hub. |
| 87 | + |
| 88 | +## Next steps |
| 89 | + |
| 90 | +- For conceptual information on how log compaction work, see [Log compaction](log-compaction.md). |
0 commit comments