3434import edu .emory .mathcs .backport .java .util .Collections ;
3535import lombok .Getter ;
3636import lombok .extern .slf4j .Slf4j ;
37+ import java .time .Instant ;
38+ import java .time .LocalDateTime ;
39+ import java .time .ZoneId ;
40+ import java .time .format .DateTimeFormatter ;
41+ import java .time .format .DateTimeParseException ;
3742import org .apache .kafka .clients .producer .RecordMetadata ;
3843import org .apache .kafka .connect .errors .RetriableException ;
3944import org .apache .kafka .connect .source .SourceRecord ;
4247import java .io .IOException ;
4348import java .time .Instant ;
4449import java .util .ArrayList ;
50+ import java .util .Date ;
4551import java .util .List ;
4652import java .util .Map ;
4753import java .util .function .Function ;
5561@ Slf4j
5662public class HttpSourceTask extends SourceTask {
5763
64+ public static final String AUTOTIMESTAMP = "AUTOTIMESTAMP" ;
5865 private final Function <Map <String , String >, HttpSourceConnectorConfig > configFactory ;
5966
6067 private TimerThrottler throttler ;
@@ -74,6 +81,10 @@ public class HttpSourceTask extends SourceTask {
7481 private String nextPageOffsetField ;
7582 private String hasNextPageField ;
7683
84+ private String autoDateInitialOffset ;
85+ private String sautoDateIncrement ;
86+ private String sautoDateBackoff ;
87+
7788 @ Getter
7889 private Offset offset ;
7990
@@ -99,6 +110,10 @@ public void start(Map<String, String> settings) {
99110 offset = loadOffset (config .getInitialOffset ());
100111 nextPageOffsetField = config .getNextPageOffsetField ();
101112 hasNextPageField = config .getHasNextPageField ();
113+
114+ autoDateInitialOffset = config .getAutoDateInitialOffset ();
115+ sautoDateIncrement = config .getAutoDateIncrement ();
116+ sautoDateBackoff = config .getAutoDateBackoff ();
102117 }
103118
104119 private Offset loadOffset (Map <String , String > initialOffset ) {
@@ -110,12 +125,34 @@ private Offset loadOffset(Map<String, String> initialOffset) {
110125 public List <SourceRecord > poll () throws InterruptedException {
111126
112127 throttler .throttle (offset .getTimestamp ().orElseGet (Instant ::now ));
128+ List <SourceRecord > allRecords = new ArrayList <>();
129+ DateTimeFormatter formatter = DateTimeFormatter .ISO_DATE_TIME ;
130+ long autoOffset = 0 ;
131+ long autoDateIncrement = 0 ;
132+ long autoDateBackoff = 0 ;
133+
134+ if ( autoDateInitialOffset != null && !autoDateInitialOffset .isEmpty ()) {
135+ try {
136+ LocalDateTime dateTime = LocalDateTime .parse (autoDateInitialOffset , formatter );
137+ Instant timestamp = dateTime .atZone (ZoneId .of ("UTC" )).toInstant ();
138+ autoDateIncrement = Long .parseLong (sautoDateIncrement );
139+ autoDateBackoff = Long .parseLong (sautoDateBackoff );
140+ autoOffset = timestamp .toEpochMilli ();
141+ } catch (Exception e ) {
142+ e .printStackTrace ();
143+ }
144+
145+ if (offset .toMap ().containsKey (AUTOTIMESTAMP )) {
146+ autoOffset = (Long ) offset .toMap ().get (AUTOTIMESTAMP );
147+ }
148+
149+ offset .setValue (AUTOTIMESTAMP , autoOffset );
150+ }
113151 offset .setValue (nextPageOffsetField , "" );
114152 offset .setValue (hasNextPageField , "" );
115153 String hasNextPageFlag = "true" ;
116154 String nextPageValue = "" ;
117155
118- List <SourceRecord > allRecords = new ArrayList <>();
119156 while (hasNextPageFlag .matches ("true" )) {
120157 HttpRequest request = requestFactory .createRequest (offset );
121158
@@ -139,17 +176,28 @@ public List<SourceRecord> poll() throws InterruptedException {
139176 } else {
140177 hasNextPageFlag = "" ;
141178 }
142-
143179 } else {
144180 hasNextPageFlag = "" ;
145181 }
146- Thread .sleep (1000 );
182+ Thread .sleep (300 );
147183 }
148184
149185 List <SourceRecord > unseenRecords = recordSorter .sort (allRecords ).stream ()
150186 .filter (recordFilterFactory .create (offset ))
151187 .collect (toList ());
152188
189+ if (autoDateInitialOffset != null && !autoDateInitialOffset .isEmpty ()) {
190+ autoOffset = autoOffset + autoDateIncrement - autoDateBackoff ;
191+ if (autoOffset > new Date ().getTime ()) {
192+ autoOffset = new Date ().getTime () - autoDateBackoff ;
193+ }
194+ for (SourceRecord s : allRecords ) {
195+ ((Map <String ,Long >)s .sourceOffset ()).put (AUTOTIMESTAMP , Long .valueOf (autoOffset ));
196+ }
197+ offset .setValue (AUTOTIMESTAMP , autoOffset );
198+ log .info ("AutoOffset Patch {}" , offset .toString ());
199+ }
200+
153201 log .info ("Request for offset {} yields {}/{} new records" , offset .toMap (), unseenRecords .size (), allRecords .size ());
154202
155203 confirmationWindow = new ConfirmationWindow <>(extractOffsets (unseenRecords ));
0 commit comments