-
Notifications
You must be signed in to change notification settings - Fork 367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ElasticSearch connector with date in index and custom document type #173
Merged
stheppi
merged 4 commits into
lensesio:master
from
ConnectedHomes:elastic-with-date-in-index
May 4, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
441a5a3
ElasticSearch connector supports date in index name and custom docume…
robvadai e08de12
Fixed Jackson version problem between Kafka Streams 0.10.2.0 and ES l…
robvadai 9bc01bd
Testing with date in ES index name
robvadai 1a8aab4
Default config values in ElasticSearch connector set to null
robvadai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robvadai
Shouldn't we add support for this in KCQL? The reason i am asking is for scenarios where you route messages from topic1 to index1 and topic2 to index2 and maybe you want one of the index to have suffix. Furthermore you might use different document type for messages coming from two different topics.
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KCQL example: INSERT INTO $THE_INDEX SELECT * FROM $THE_TOPIC [WITHDOCTYPE($docType)] [WITHINDEXSUFFIX($suffix)] [AUTOCREATE]
Autocreate - already exists in the KCQL grammar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.