-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue 13019][es-sink] Support event-time-based index name in ES Sink (…
…#14383) Fixes #13019 ### Motivation As described in the original issue, it's a common request to write data to event-time-based indices in logs and metrics use cases, therefore it would be very helpful to have builtin support in the ES sink. ### Modifications *Describe the modifications you've done.* ### Verifying this change - [ ] Make sure that the change passes the CI checks. This change added tests and can be verified as follows: - *Added test cases for index name formatter*
- Loading branch information
1 parent
330fcb9
commit 21c3a0b
Showing
6 changed files
with
210 additions
and
21 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
98 changes: 98 additions & 0 deletions
98
...o/elastic-search/src/main/java/org/apache/pulsar/io/elasticsearch/IndexNameFormatter.java
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,98 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.elasticsearch; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.apache.pulsar.client.api.schema.GenericObject; | ||
import org.apache.pulsar.functions.api.Record; | ||
|
||
/** | ||
* A class that helps to generate the time-based index names. | ||
* | ||
* The formatter finds the pattern %{+<date-format>} in the format string | ||
* and replace them with strings that are formatted from the event time of the record | ||
* using <date-format>. The format follows the rules of {@link java.time.format.DateTimeFormatter}. | ||
* | ||
* For example, suppose the event time of the record is 1645182000000L, the indexName | ||
* is "logs-%{+yyyy-MM-dd}", then the formatted index name would be "logs-2022-02-18". | ||
*/ | ||
public class IndexNameFormatter { | ||
private static final Pattern PATTERN_FIELD_REF = Pattern.compile("%\\{\\+(.+?)}"); | ||
|
||
private final String indexNameFormat; | ||
private final List<String> segments; | ||
private final List<DateTimeFormatter> dateTimeFormatters; | ||
|
||
public IndexNameFormatter(String indexNameFormat) { | ||
this.indexNameFormat = indexNameFormat; | ||
|
||
Pair<List<String>, List<DateTimeFormatter>> parsed = parseFormat(indexNameFormat); | ||
this.segments = parsed.getKey(); | ||
this.dateTimeFormatters = parsed.getRight(); | ||
} | ||
|
||
static Pair<List<String>, List<DateTimeFormatter>> parseFormat(String format) { | ||
List<String> segments = new ArrayList<>(); | ||
List<DateTimeFormatter> formatters = new ArrayList<>(); | ||
Matcher matcher = PATTERN_FIELD_REF.matcher(format); | ||
int pos = 0; | ||
while (matcher.find()) { | ||
segments.add(format.substring(pos, matcher.start())); | ||
formatters.add(DateTimeFormatter.ofPattern(matcher.group(1)) | ||
.withLocale(Locale.ENGLISH) | ||
.withZone(ZoneId.of("UTC"))); | ||
pos = matcher.end(); | ||
} | ||
segments.add(format.substring(pos)); | ||
return Pair.of(segments, formatters); | ||
} | ||
|
||
static void validate(String format) { | ||
Pair<List<String>, List<DateTimeFormatter>> parsed = parseFormat(format); | ||
for (String s : parsed.getLeft()) { | ||
if (!s.toLowerCase(Locale.ROOT).equals(s)) { | ||
throw new IllegalArgumentException("indexName should be lowercase only."); | ||
} | ||
} | ||
} | ||
|
||
public String indexName(Record<GenericObject> record) { | ||
if (this.dateTimeFormatters.isEmpty()) { | ||
return this.indexNameFormat; | ||
} | ||
Instant eventTime = Instant.ofEpochMilli(record.getEventTime() | ||
.orElseThrow(() -> new IllegalStateException("No event time in record"))); | ||
StringBuilder builder = new StringBuilder(this.segments.get(0)); | ||
|
||
for (int i = 0; i < dateTimeFormatters.size(); i++) { | ||
builder.append(dateTimeFormatters.get(i).format(eventTime)); | ||
builder.append(this.segments.get(i + 1)); | ||
} | ||
|
||
return builder.toString(); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...astic-search/src/test/java/org/apache/pulsar/io/elasticsearch/IndexNameFormatterTest.java
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,52 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.io.elasticsearch; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import java.util.Optional; | ||
import org.apache.pulsar.functions.api.Record; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
public class IndexNameFormatterTest { | ||
@DataProvider(name = "indexFormats") | ||
public Object[][] indexPatternTestData() { | ||
return new Object[][]{ | ||
new Object[] {"test", "test"}, | ||
new Object[] {"test-%{yyyy}", "test-%{yyyy}"}, | ||
new Object[] {"test-%{+yyyy}", "test-2022"}, | ||
new Object[] {"test-%{+yyyy-MM}", "test-2022-02"}, | ||
new Object[] {"test-%{+yyyy-MM-dd}", "test-2022-02-18"}, | ||
new Object[] {"test-%{+yyyy-MM-dd}-abc", "test-2022-02-18-abc"}, | ||
new Object[] {"%{+yyyy-MM-dd}-abc", "2022-02-18-abc"}, | ||
new Object[] {"%{+yyyy}/%{+MM-dd}", "2022/02-18"}, | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "indexFormats") | ||
public void testIndexFormats(String format, String result) { | ||
Record record = Mockito.mock(Record.class); | ||
when(record.getEventTime()).thenReturn(Optional.of(1645182000000L)); | ||
IndexNameFormatter formatter = new IndexNameFormatter(format); | ||
assertEquals(formatter.indexName(record), result); | ||
} | ||
} |
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