我已经在Akka Streams和Alpakka上编写了一些代码,这些代码从Amazon SQS读取并在Elasticsearch中对事件编制索引。一切顺利,性能很棒,但我的索引名称有问题。我有这段代码:
class ElasticSearchIndexFlow(restClient: RestClient) {
private val elasticSettings = ElasticsearchSinkSettings(bufferSize = 10)
def flow: Flow[IncomingMessage[DomainEvent, NotUsed], Seq[IncomingMessageResult[DomainEvent, NotUsed]], NotUsed] =
ElasticsearchFlow.create[DomainEvent](index, "domain-event", elasticSettings)(
restClient,
DomainEventMarshaller.domainEventWrites
)
private def index = {
val now = DateTime.now()
s"de-${now.getYear}.${now.getMonthOfYear}.${now.getDayOfMonth}"
}
}
问题是在运行流程几天后,索引名称不会更改。我想,Akka Streams在引擎盖下创建了一个融合的actor,并且用于获取索引名称的函数index
仅在执行开始时进行评估。
我是否可以根据当前日期使用索引名称索引ES中的事件?
答案 0 :(得分:0)
问题的解决方案是使用IncomingMessage.withIndexName
所以:
def flow: Flow[(DomainEvent, Message), IncomingMessage[DomainEvent, Message], NotUsed] =
Flow[(DomainEvent, Message)].map {
case (domainEvent, message) =>
IncomingMessage(Some(domainEvent.eventId), domainEvent, message)
.withIndexName(indexName(domainEvent.ocurredOn))
}
并且:
def flow: Flow[IncomingMessage[DomainEvent, NotUsed], Seq[IncomingMessageResult[DomainEvent, NotUsed]], NotUsed] =
ElasticsearchFlow.create[DomainEvent]("this-index-name-is-not-used", "domain-event", elasticSettings)(
restClient,
DomainEventMarshaller.domainEventWrites
)