@@ -862,33 +862,37 @@ def metrics(self, raw=False):
862
862
return metrics
863
863
864
864
def offsets_for_times (self , timestamps ):
865
- """
866
- Look up the offsets for the given partitions by timestamp. The returned
867
- offset for each partition is the earliest offset whose timestamp is
868
- greater than or equal to the given timestamp in the corresponding
869
- partition.
865
+ """Look up the offsets for the given partitions by timestamp. The
866
+ returned offset for each partition is the earliest offset whose
867
+ timestamp is greater than or equal to the given timestamp in the
868
+ corresponding partition.
870
869
871
870
This is a blocking call. The consumer does not have to be assigned the
872
871
partitions.
873
872
874
873
If the message format version in a partition is before 0.10.0, i.e.
875
874
the messages do not have timestamps, ``None`` will be returned for that
876
- partition.
875
+ partition. ``None`` will also be returned for the partition if there
876
+ are no messages in it.
877
877
878
878
Note:
879
- Notice that this method may block indefinitely if the partition
880
- does not exist.
879
+ This method may block indefinitely if the partition does not exist.
881
880
882
881
Arguments:
883
882
timestamps (dict): ``{TopicPartition: int}`` mapping from partition
884
883
to the timestamp to look up. Unit should be milliseconds since
885
884
beginning of the epoch (midnight Jan 1, 1970 (UTC))
886
885
886
+ Returns:
887
+ ``{TopicPartition: OffsetAndTimestamp}``: mapping from partition
888
+ to the timestamp and offset of the first message with timestamp
889
+ greater than or equal to the target timestamp.
890
+
887
891
Raises:
888
- ValueError: if the target timestamp is negative
889
- UnsupportedVersionError: if the broker does not support looking
892
+ ValueError: If the target timestamp is negative
893
+ UnsupportedVersionError: If the broker does not support looking
890
894
up the offsets by timestamp.
891
- KafkaTimeoutError: if fetch failed in request_timeout_ms
895
+ KafkaTimeoutError: If fetch failed in request_timeout_ms
892
896
"""
893
897
if self .config ['api_version' ] <= (0 , 10 , 0 ):
894
898
raise UnsupportedVersionError (
@@ -903,6 +907,67 @@ def offsets_for_times(self, timestamps):
903
907
return self ._fetcher .get_offsets_by_times (
904
908
timestamps , self .config ['request_timeout_ms' ])
905
909
910
+ def beginning_offsets (self , partitions ):
911
+ """Get the first offset for the given partitions.
912
+
913
+ This method does not change the current consumer position of the
914
+ partitions.
915
+
916
+ Note:
917
+ This method may block indefinitely if the partition does not exist.
918
+
919
+ Arguments:
920
+ partitions (list): List of TopicPartition instances to fetch
921
+ offsets for.
922
+
923
+ Returns:
924
+ ``{TopicPartition: int}``: The earliest available offsets for the
925
+ given partitions.
926
+
927
+ Raises:
928
+ UnsupportedVersionError: If the broker does not support looking
929
+ up the offsets by timestamp.
930
+ KafkaTimeoutError: If fetch failed in request_timeout_ms.
931
+ """
932
+ if self .config ['api_version' ] <= (0 , 10 , 0 ):
933
+ raise UnsupportedVersionError (
934
+ "offsets_for_times API not supported for cluster version {}"
935
+ .format (self .config ['api_version' ]))
936
+ offsets = self ._fetcher .beginning_offsets (
937
+ partitions , self .config ['request_timeout_ms' ])
938
+ return offsets
939
+
940
+ def end_offsets (self , partitions ):
941
+ """Get the last offset for the given partitions. The last offset of a
942
+ partition is the offset of the upcoming message, i.e. the offset of the
943
+ last available message + 1.
944
+
945
+ This method does not change the current consumer position of the
946
+ partitions.
947
+
948
+ Note:
949
+ This method may block indefinitely if the partition does not exist.
950
+
951
+ Arguments:
952
+ partitions (list): List of TopicPartition instances to fetch
953
+ offsets for.
954
+
955
+ Returns:
956
+ ``{TopicPartition: int}``: The end offsets for the given partitions.
957
+
958
+ Raises:
959
+ UnsupportedVersionError: If the broker does not support looking
960
+ up the offsets by timestamp.
961
+ KafkaTimeoutError: If fetch failed in request_timeout_ms
962
+ """
963
+ if self .config ['api_version' ] <= (0 , 10 , 0 ):
964
+ raise UnsupportedVersionError (
965
+ "offsets_for_times API not supported for cluster version {}"
966
+ .format (self .config ['api_version' ]))
967
+ offsets = self ._fetcher .end_offsets (
968
+ partitions , self .config ['request_timeout_ms' ])
969
+ return offsets
970
+
906
971
def _use_consumer_group (self ):
907
972
"""Return True iff this consumer can/should join a broker-coordinated group."""
908
973
if self .config ['api_version' ] < (0 , 9 ):
0 commit comments