-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ElasticSearch connector with date in index and custom document type (#…
…173) * ElasticSearch connector supports date in index name and custom document type * Fixed Jackson version problem between Kafka Streams 0.10.2.0 and ES libraries * Testing with date in ES index name * Default config values in ElasticSearch connector set to null
- Loading branch information
Showing
15 changed files
with
310 additions
and
16 deletions.
There are no files selected for viewing
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
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
37 changes: 37 additions & 0 deletions
37
...n/scala/com/datamountaineer/streamreactor/connect/elastic/indexname/CustomIndexName.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,37 @@ | ||
package com.datamountaineer.streamreactor.connect.elastic.indexname | ||
|
||
import scala.annotation.tailrec | ||
|
||
class InvalidCustomIndexNameException(message: String) extends RuntimeException(message) | ||
|
||
case class CustomIndexName(fragments: Vector[IndexNameFragment]) { | ||
override def toString = fragments.map(_.getFragment).mkString | ||
} | ||
|
||
object CustomIndexName { | ||
|
||
@tailrec | ||
private def parseIndexName(remainingChars: Vector[Char], currentFragment: StringBuilder, results: Vector[Option[IndexNameFragment]]): Vector[IndexNameFragment] = | ||
remainingChars match { | ||
case head +: rest => head match { | ||
case DateTimeFragment.OpeningChar => | ||
val (dateTimeFormat, afterDateTimeFormatIncludingClosingChar) = rest.span { _ != DateTimeFragment.ClosingChar } | ||
val afterDateTimeFormat = afterDateTimeFormatIncludingClosingChar.tail | ||
|
||
val maybeCurrentFragment = currentFragment.mkString.toOption | ||
val maybeDateTimeFormat = dateTimeFormat.mkString.toOption | ||
|
||
val newResultsWithDateTimeFragment = results :+ maybeCurrentFragment.map(TextFragment.apply) :+ maybeDateTimeFormat.map(DateTimeFragment(_)) | ||
|
||
parseIndexName(afterDateTimeFormat, new StringBuilder, newResultsWithDateTimeFragment) | ||
case DateTimeFragment.ClosingChar => throw new InvalidCustomIndexNameException(s"Found closing '${DateTimeFragment.ClosingChar}' but no opening character") | ||
case anyOtherChar => parseIndexName(rest, currentFragment.append(anyOtherChar), results) | ||
} | ||
case Vector() => | ||
val maybeCurrentFragment = currentFragment.mkString.toOption | ||
(results :+ maybeCurrentFragment.map(TextFragment.apply)).flatten | ||
} | ||
|
||
def parseIndexName(indexName: String): CustomIndexName = | ||
CustomIndexName(parseIndexName(indexName.toVector, new StringBuilder, Vector.empty)) | ||
} |
25 changes: 25 additions & 0 deletions
25
...scala/com/datamountaineer/streamreactor/connect/elastic/indexname/IndexNameFragment.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,25 @@ | ||
package com.datamountaineer.streamreactor.connect.elastic.indexname | ||
|
||
import java.time.Clock | ||
import java.time.LocalDateTime._ | ||
import java.time.format.DateTimeFormatter._ | ||
|
||
object ClockProvider { | ||
val ClockInstance = Clock.systemUTC() | ||
} | ||
|
||
sealed trait IndexNameFragment { | ||
def getFragment: String | ||
} | ||
|
||
case class TextFragment(text: String) extends IndexNameFragment { | ||
override def getFragment: String = text | ||
} | ||
|
||
case class DateTimeFragment(dateTimeFormat: String, clock: Clock = ClockProvider.ClockInstance) extends IndexNameFragment { | ||
override def getFragment: String = s"${now(clock).format(ofPattern(dateTimeFormat))}" | ||
} | ||
object DateTimeFragment { | ||
val OpeningChar = '{' | ||
val ClosingChar = '}' | ||
} |
8 changes: 8 additions & 0 deletions
8
.../src/main/scala/com/datamountaineer/streamreactor/connect/elastic/indexname/package.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,8 @@ | ||
package com.datamountaineer.streamreactor.connect.elastic | ||
|
||
package object indexname { | ||
|
||
implicit class StringToOption(text: String) { | ||
def toOption: Option[String] = if (text.nonEmpty) Some(text) else None | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
.../test/scala/com/datamountaineer/streamreactor/connect/elastic/TestElasticJsonWriter.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 com.datamountaineer.streamreactor.connect.elastic | ||
|
||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class TestElasticJsonWriter extends FlatSpec with Matchers { | ||
"ElasticJsonWriter" should "create an index name without suffix when suffix not set" in { | ||
ElasticJsonWriter.createIndexName(None)("index_name") shouldBe "index_name" | ||
} | ||
|
||
it should "create an index name with suffix when suffix is set" in { | ||
val formattedDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd")) | ||
ElasticJsonWriter.createIndexName(Some("_suffix_{YYYY-MM-dd}"))("index_name") shouldBe s"index_name_suffix_$formattedDateTime" | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
...test/scala/com/datamountaineer/streamreactor/connect/elastic/indexname/ClockFixture.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,7 @@ | ||
package com.datamountaineer.streamreactor.connect.elastic.indexname | ||
|
||
import java.time.{Clock, Instant, ZoneOffset} | ||
|
||
trait ClockFixture { | ||
val TestClock = Clock.fixed(Instant.parse("2016-10-02T14:00:00.00Z"), ZoneOffset.UTC) | ||
} |
Oops, something went wrong.