1212import org .apache .kafka .clients .consumer .ConsumerConfig ;
1313import org .apache .kafka .clients .consumer .KafkaConsumer ;
1414import org .apache .kafka .clients .consumer .OffsetAndTimestamp ;
15+ import org .apache .kafka .clients .consumer .OffsetResetStrategy ;
1516import org .apache .kafka .common .TopicPartition ;
1617import org .apache .kafka .common .serialization .StringDeserializer ;
1718
@@ -82,8 +83,12 @@ public KafkaConsumer getKafkaConsumer(String streamName) throws Exception {
8283 if (kafkaSchema == null ) {
8384 throw new Exception ("Kafka Schema not Found for Stream: " + streamName );
8485 }
85- kafkaConsumer = getConsumer (kafkaSchema );
86- kafkaConsumer .subscribe (Collections .singletonList (streamName + ".stream" ));
86+ kafkaConsumer = getConsumer (kafkaSchema , streamName );
87+ TopicPartition topicPartition = new TopicPartition (streamName + ".stream" ,0 );
88+ kafkaConsumer .assign (Collections .singletonList (topicPartition ));
89+ if (kafkaProps .get (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG ).equals (OffsetResetStrategy .EARLIEST .toString ().toLowerCase ())) {
90+ return seekToMidNight (topicPartition );
91+ }
8792 }
8893 catch (Exception e ) {
8994 throw (e );
@@ -106,7 +111,7 @@ public KafkaConsumer getKafkaConsumer(String streamName, long timestamp) throws
106111 if (kafkaSchema == null ) {
107112 throw new Exception ("Kafka Schema not Found for Stream: " + streamName );
108113 }
109- kafkaConsumer = getConsumer (kafkaSchema );
114+ kafkaConsumer = getConsumer (kafkaSchema , streamName );
110115 TopicPartition topicPartition = new TopicPartition (streamName + ".stream" ,0 );
111116 kafkaConsumer .assign (Collections .singleton (topicPartition ));
112117
@@ -137,7 +142,7 @@ public KafkaConsumer getKafkaConsumer(String streamName, long timestamp) throws
137142 */
138143
139144
140- public KafkaAvroConsumer getConsumer (Schema avroSchema ) throws Exception {
145+ public KafkaAvroConsumer getConsumer (Schema avroSchema , String streamName ) throws Exception {
141146 try {
142147 if (!IsItJunit .isJUnitTest ()) {
143148 ConfigProperties .resolveAndExportToSystemProperties (securityProps );
@@ -147,9 +152,9 @@ public KafkaAvroConsumer getConsumer(Schema avroSchema) throws Exception {
147152 kafkaProps .put ("key.deserializer" , StringDeserializer .class .getName ());
148153 kafkaProps .put ("value.deserializer" , AvroDeserializer .class .getName ());
149154 if (!kafkaProps .containsKey (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG )) {
150- kafkaProps .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "earliest" );
155+ kafkaProps .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , OffsetResetStrategy . EARLIEST . toString (). toLowerCase () );
151156 }
152- kafkaProps .put (ConsumerConfig .GROUP_ID_CONFIG , this .clientID + "_" +getDate () + "_" + UUID . randomUUID (). toString ());
157+ kafkaProps .put (ConsumerConfig .GROUP_ID_CONFIG , this .clientID + "_" + streamName + "_" + getDate ());
153158 ConfigProperties .resolve (kafkaProps );
154159 return new KafkaAvroConsumer (kafkaProps , avroSchema );
155160 }
@@ -201,8 +206,12 @@ public KafkaConsumer getNewsConsumer(String topic) throws Exception {
201206 if (newsSchema == null ) {
202207 throw new Exception ("News Schema not Found " );
203208 }
204- kafkaConsumer = getConsumer (newsSchema );
205- kafkaConsumer .subscribe (Collections .singletonList (topic +".stream" ));
209+ kafkaConsumer = getConsumer (newsSchema , topic );
210+ TopicPartition topicPartition = new TopicPartition (topic + ".stream" ,0 );
211+ kafkaConsumer .assign (Collections .singletonList (topicPartition ));
212+ if (kafkaProps .get (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG ).equals (OffsetResetStrategy .EARLIEST .toString ().toLowerCase ())) {
213+ return seekToMidNight (topicPartition );
214+ }
206215 return kafkaConsumer ;
207216 }
208217 catch (Exception e ){
@@ -217,4 +226,32 @@ private String getDate(){
217226 String date = dateformat .format (new Date ());
218227 return date ;
219228 }
229+
230+ private KafkaConsumer seekToMidNight (TopicPartition topicPartition ){
231+ Map <TopicPartition ,Long > timestmaps = new HashMap ();
232+ timestmaps .put (topicPartition , getTodayMidNightTimeStamp ());
233+ Map <TopicPartition , OffsetAndTimestamp > offsetsForTimes = kafkaConsumer .offsetsForTimes (timestmaps );
234+ OffsetAndTimestamp offsetAndTimestamp = null ;
235+ if (offsetsForTimes != null && (offsetAndTimestamp = offsetsForTimes .get (topicPartition )) != null ) {
236+ kafkaConsumer .seek (topicPartition , offsetAndTimestamp .offset ());
237+ } else {
238+ kafkaConsumer .seekToBeginning (Collections .singleton (topicPartition ));
239+ }
240+ return kafkaConsumer ;
241+ }
242+
243+ private long getTodayMidNightTimeStamp (){
244+
245+ TimeZone timeZone = TimeZone .getTimeZone ("America/New_York" );
246+
247+ Calendar today = Calendar .getInstance (timeZone );
248+ today .set (Calendar .HOUR_OF_DAY , 0 );
249+ today .set (Calendar .MINUTE , 0 );
250+ today .set (Calendar .SECOND , 0 );
251+
252+ long timestampFromMidnight = today .getTimeInMillis ();
253+
254+ return timestampFromMidnight ;
255+ }
256+
220257}
0 commit comments