Interface TopicCompactionStrategy<T>


@Private @Unstable public interface TopicCompactionStrategy<T>
Defines a custom strategy to compact messages in a topic. This strategy can be passed to Topic Compactor and Table View to compact messages in a custom way. Examples: TopicCompactionStrategy strategy = new MyTopicCompactionStrategy(); // Run topic compaction by the compaction strategy. // While compacting messages for each key, // it will choose messages only if TopicCompactionStrategy.shouldKeepLeft(prev, cur) returns false. StrategicTwoPhaseCompactor compactor = new StrategicTwoPhaseCompactor(...); compactor.compact(topic, strategy); // Run table view by the compaction strategy. // While updating messages in the table view <key,value> map, // it will choose messages only if TopicCompactionStrategy.shouldKeepLeft(prev, cur) returns false. TableView tableView = pulsar.getClient().newTableViewBuilder(strategy.getSchema()) .topic(topic) .loadConf(Map.of( "topicCompactionStrategyClassName", strategy.getClass().getCanonicalName())) .create();
  • Field Details

  • Method Details

    • getSchema

      Schema<T> getSchema()
      Returns the schema object for this strategy.
      Returns:
    • shouldKeepLeft

      boolean shouldKeepLeft(T prev, T cur)
      Tests if the compaction needs to keep the left(previous message) compared to the right(current message) for the same key.
      Parameters:
      prev - previous message value
      cur - current message value
      Returns:
      True if it needs to keep the previous message and ignore the current message. Otherwise, False.
    • handleSkippedMessage

      default void handleSkippedMessage(String key, T cur)
    • load

      static TopicCompactionStrategy load(String tag, String topicCompactionStrategyClassName)
    • getInstance

      static TopicCompactionStrategy getInstance(String tag)