在ES中使用Alpakka索引的Akka Streams:索引名称仅在开始执行时进行评估

时间:2018-04-18 12:51:02

标签: elasticsearch akka akka-stream alpakka

我已经在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中的事件?

1 个答案:

答案 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
  )