Ejemplo n.º 1
0
    def encode_produce_request(cls, payloads=(), acks=1, timeout=1000):
        """
        Encode a ProduceRequest struct

        Arguments:
            payloads: list of ProduceRequestPayload
            acks: How "acky" you want the request to be
                1: written to disk by the leader
                0: immediate response
                -1: waits for all replicas to be in sync
            timeout: Maximum time (in ms) the server will wait for replica acks.
                This is _not_ a socket timeout

        Returns: ProduceRequest
        """
        if acks not in (1, 0, -1):
            raise ValueError('ProduceRequest acks (%s) must be 1, 0, -1' %
                             acks)

        return kafka.protocol.produce.ProduceRequest[0](
            required_acks=acks,
            timeout=timeout,
            topics=[(topic, [(partition, [
                (0, 0,
                 kafka.protocol.message.Message(msg.value,
                                                key=msg.key,
                                                magic=msg.magic,
                                                attributes=msg.attributes))
                for msg in payload.messages
            ]) for partition, payload in topic_payloads.items()])
                    for topic, topic_payloads in group_by_topic_and_partition(
                        payloads).items()])
Ejemplo n.º 2
0
 def encode_offset_request(cls, payloads=()):
     return kafka.protocol.offset.OffsetRequest[0](
         replica_id=-1,
         topics=[(topic,
                  [(partition, payload.time, payload.max_offsets)
                   for partition, payload in six.iteritems(topic_payloads)])
                 for topic, topic_payloads in six.iteritems(
                     group_by_topic_and_partition(payloads))])
Ejemplo n.º 3
0
    def encode_offset_commit_request(cls, group, payloads):
        """
        Encode an OffsetCommitRequest struct

        Arguments:
            group: string, the consumer group you are committing offsets for
            payloads: list of OffsetCommitRequestPayload
        """
        return kafka.protocol.commit.OffsetCommitRequest[0](
            consumer_group=group,
            topics=[(topic,
                     [(partition, payload.offset, payload.metadata)
                      for partition, payload in six.iteritems(topic_payloads)])
                    for topic, topic_payloads in six.iteritems(
                        group_by_topic_and_partition(payloads))])
Ejemplo n.º 4
0
    def encode_offset_fetch_request(cls, group, payloads, from_kafka=False):
        """
        Encode an OffsetFetchRequest struct. The request is encoded using
        version 0 if from_kafka is false, indicating a request for Zookeeper
        offsets. It is encoded using version 1 otherwise, indicating a request
        for Kafka offsets.

        Arguments:
            group: string, the consumer group you are fetching offsets for
            payloads: list of OffsetFetchRequestPayload
            from_kafka: bool, default False, set True for Kafka-committed offsets
        """
        version = 1 if from_kafka else 0
        return kafka.protocol.commit.OffsetFetchRequest[version](
            consumer_group=group,
            topics=[(topic, list(topic_payloads.keys()))
                    for topic, topic_payloads in six.iteritems(
                        group_by_topic_and_partition(payloads))])
Ejemplo n.º 5
0
    def encode_fetch_request(cls,
                             payloads=(),
                             max_wait_time=100,
                             min_bytes=4096):
        """
        Encodes a FetchRequest struct

        Arguments:
            payloads: list of FetchRequestPayload
            max_wait_time (int, optional): ms to block waiting for min_bytes
                data. Defaults to 100.
            min_bytes (int, optional): minimum bytes required to return before
                max_wait_time. Defaults to 4096.

        Return: FetchRequest
        """
        return kafka.protocol.fetch.FetchRequest[0](
            replica_id=-1,
            max_wait_time=max_wait_time,
            min_bytes=min_bytes,
            topics=[(topic, [(partition, payload.offset, payload.max_bytes)
                             for partition, payload in topic_payloads.items()])
                    for topic, topic_payloads in group_by_topic_and_partition(
                        payloads).items()])