You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@apex.apache.org by Chris Stockton <cs...@gmail.com> on 2016/09/24 22:40:35 UTC

repartition between operators

If I have a KafkaInputOperator that is reading from 2 partitions and I
setup a stream to a one-to-one operator, how would I read the messages from
each Kafka partition, extract a particular field (say user id) and then
send all the keys to a parallel downstream operator so that the keys are
grouped together.

Something like this:

Messages coming in on kafka are json of the format {'user_id': 123,
'value': 'abc'} where the user_id is some number.  The messages are not
partitioned on the Kafka topic according to the user_id.

In coming msgs:            repartition by id

id=1, id=2 ==>  K1 -> JsonToPojo --   --> PoJo(1), PoJo(3), PoJo(1)
                                   \ /
                                    X
                                   / \
id=1, id=4 ==>  K2 -> JsonToPojo --   --> PoJo(4)

Where I transform the Json messages into a POJO like:

class PoJo {
  int id;
  String value;
  ...
}

Would 'X' need to be a combination of a StreamMerger and some kind of
custom partitioner, or could the JsonToPojo operator directly partition
it's output based on the id value?

Re: repartition between operators

Posted by Sandesh Hegde <sa...@datatorrent.com>.
StreamCodec is what Apex uses to distribute the tuples. Default codec uses
the hashcode of the tuple, which is not what you want. You need to
implement custom StreamCodec.

One example is below and another one is attached.
https://github.com/DataTorrent/examples/tree/master/tutorials/partition


On Sat, Sep 24, 2016 at 3:40 PM Chris Stockton <cs...@gmail.com> wrote:

> If I have a KafkaInputOperator that is reading from 2 partitions and I
> setup a stream to a one-to-one operator, how would I read the messages from
> each Kafka partition, extract a particular field (say user id) and then
> send all the keys to a parallel downstream operator so that the keys are
> grouped together.
>
> Something like this:
>
> Messages coming in on kafka are json of the format {'user_id': 123,
> 'value': 'abc'} where the user_id is some number.  The messages are not
> partitioned on the Kafka topic according to the user_id.
>
> In coming msgs:            repartition by id
>
> id=1, id=2 ==>  K1 -> JsonToPojo --   --> PoJo(1), PoJo(3), PoJo(1)
>                                    \ /
>                                     X
>                                    / \
> id=1, id=4 ==>  K2 -> JsonToPojo --   --> PoJo(4)
>
> Where I transform the Json messages into a POJO like:
>
> class PoJo {
>   int id;
>   String value;
>   ...
> }
>
> Would 'X' need to be a combination of a StreamMerger and some kind of
> custom partitioner, or could the JsonToPojo operator directly partition
> it's output based on the id value?
>