Question: I'm new to Kafka Streams and I'm using it to make an exact copy of a topic into another with a different name. This topic has several partitions and my producers are using custom partitioners. The output topic is created beforehand with the same number of partitions of the input topic.

In my app, I did (I'm using Kotlin):

```
val builder = StreamsBuilder()
builder
    .stream<Any, Any>(inputTopic)
    .to(outputTopic)

```

This works, except for the partitions (because of course I'm using a custom partitioner). Is there a simple way to copy input records to the output topic using the same partition of the input record?

I checked the Processor API that allows to access the partition of the input record through a `ProcessorContext` but I was unable to manually set the partition of the output record.

Apparently, I could use a custom partitioner in the sink, but that would imply deserializing and serializing the records to recalculate the output partition with my custom partitioner.

Answer: 
`Produced` (that is one of the `KStream::to` arguments) has `StreamPartitioner` as one of its member.

You could try following code:

```java
builder.stream("input", Consumed.with(Serdes.ByteArray(), Serdes.ByteArray()))
  .to("output", Produced.with(Serdes.ByteArray(), Serdes.ByteArray(), (topicName, key, value, numberOfPartitions) -> calculatePartition(topicName, key, value, numberOfPartitions));

```

In above code only `ByteArray` `Serdes` are used so any special serialization or deserialization happens.
Firstly, messages are distributed among partitions based on Key. A message with similar key would always go in the same partition.

So if your messages have keys then you don't need to worry about it at all. As long as you have similar number of partitions as your original topic; it would be taken care of.

Secondly, if you are copying data to another topic as it is then you should consider using the original topic instead. Kafka has notion of consumer-groups.

For example, you have a topic 'transactions' then you can have consumer-groups i.e. 'credit card processor', 'mortgage payment processor', 'apple pay processor' and so on. Consumer-groups would read the same topic and filter out events that are meaningful to them and process them. 

You can also create 3 topics and achieve the same result. Though, it's not an optimal solution. You can find more information at <https://kafka.apache.org/documentation/>. 

[![enter image description here](https://i.stack.imgur.com/AzGop.png)](https://i.stack.imgur.com/AzGop.png)