-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
skip kafka records that don't conform to a standard
- Loading branch information
1 parent
50dc727
commit 7355fc9
Showing
5 changed files
with
86 additions
and
25 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
common/src/main/scala/hmda/messages/HmdaMessageFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package hmda.messages | ||
|
||
import akka.kafka.ConsumerMessage.{ CommittableMessage, CommittableOffset } | ||
import akka.stream.scaladsl.Source | ||
import com.typesafe.scalalogging.StrictLogging | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.util.Try | ||
|
||
object HmdaMessageFilter extends StrictLogging { | ||
|
||
case class StandardMsgKey(lei: String, year: Int, quarter: Option[String]) | ||
|
||
def parse(key: String): Option[StandardMsgKey] = { | ||
Try { | ||
// lei1:lei2-year-q1 | ||
val regex = "^(?<lei1>[A-Z0-9]+):(?<lei2>[A-Z0-9]+)-(?<year>[0-9]{4})(-(?<quarter>q[0-9]{1}))?$".r | ||
for { | ||
onlyMatch <- regex.findFirstMatchIn(key) | ||
lei1 = onlyMatch.group("lei1") | ||
lei2 = onlyMatch.group("lei2") | ||
year = onlyMatch.group("year") | ||
quarterOpt = Option(onlyMatch.group("quarter")) | ||
_ <- if (lei1 == lei2) Some(()) else None | ||
year <- Try(year.toInt).toOption | ||
} yield StandardMsgKey(lei1, year, quarterOpt) | ||
}.toOption.flatten // regex api is not the safest one and we don't want it to throw accidentally | ||
} | ||
|
||
type Processor = CommittableMessage[String, String] => Future[CommittableOffset] | ||
|
||
def processOnlyValidKeys[V](f: Processor)(implicit ec: ExecutionContext): Processor = msg => { | ||
parse(msg.record.key()) match { | ||
case Some(_) => f(msg) | ||
case None => | ||
logger.warn(s"Kafka message has invalid key and will be committed without being processed. Msg: ${msg}") | ||
Future(msg.committableOffset) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
common/src/test/scala/hmda/message/HmdaMessageFilterTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package hmda.messages | ||
|
||
import hmda.messages.HmdaMessageFilter.StandardMsgKey | ||
import org.scalatest.FunSuite | ||
|
||
class HmdaMessageFilterTest extends FunSuite { | ||
|
||
test("basic") { | ||
testParse("LEI:LEI-2020", Some(StandardMsgKey("LEI", 2020, None))) | ||
testParse("LEI:LEI-2020-q1", Some(StandardMsgKey("LEI", 2020, Some("q1")))) | ||
} | ||
|
||
private def testParse(key: String, expectedResult: Option[StandardMsgKey]) = { | ||
assert(HmdaMessageFilter.parse(key) == expectedResult) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters