libenchao commented on a change in pull request #11466:  
[FLINK-15400][connectors / elasticsearch] elasticsearch table sink support 
dynamic index.
URL: https://github.com/apache/flink/pull/11466#discussion_r396050636
 
 

 ##########
 File path: 
flink-connectors/flink-connector-elasticsearch-base/src/main/java/org/apache/flink/streaming/connectors/elasticsearch/IndexFormatter.java
 ##########
 @@ -0,0 +1,235 @@
+/*
+ * 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.flink.streaming.connectors.elasticsearch;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+import java.sql.Date;
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+/**
+ * User can use {@link IndexFormatter} to format index value from index 
pattern.
+ *
+ * <p>If the index is a dynamic index, the index pattern include general 
pattern like
+ * 'connector.index'='my-index-{item}' and time extract pattern like 
'connector.index'='my-index-{log_ts|yyyy-MM-dd}'.
+ *
+ * <p>For general pattern:
+ * 'item' the index field comes from any type column.
+ *
+ * <p>For time extract pattern:
+ * 'log_ts' is the index field comes from a varchar/timestamp column.
+ * 'yyyy-MM-dd' is the date format follows the {@link SimpleDateFormat} syntax.
+ * '{log_ts|yyyy-MM-dd}' is the time extract pattern for index in a dynamic 
index pattern.
+ */
+@Internal
+public class IndexFormatter implements Serializable {
+
+       private static final Pattern dynamicIndexPattern = 
Pattern.compile(".*\\{.+\\}.*");
+       private static final Pattern dynamicIndexTimeExtractPattern = 
Pattern.compile(".*\\{.+\\|.*\\}.*");
+       private static final String DEFAULT_FORMAT = "yyyy-MM-dd";
+
+       private String index;
+       private boolean dynamicIndexEnabled;
+       private boolean dynamicIndexTimeExtractEnabled;
+       private String dynamicIndexPatternStr;
+
+       private int indexFieldPos;
+       private TypeInformation indexFieldType;
+       private SimpleDateFormat dateFormat;
+
+       IndexFormatter(String index, String[] fieldNames, TypeInformation<?>[] 
fieldTypes) {
+               this.index = index;
+               this.dynamicIndexEnabled = checkDynamicIndexEnabled();
+               this.dynamicIndexPatternStr = extractDynamicIndexPatternStr();
+               this.dynamicIndexTimeExtractEnabled = 
checkDynamicIndexTimeExtractEnabled();
+               this.indexFieldPos = extractIndexFieldPos(fieldNames);
+               this.indexFieldType = extractIndexFieldType(fieldTypes);
+               this.dateFormat = extractDateFormat();
+       }
+
+       /**
+        * Builder for {@link IndexFormatter}.
+        */
+       public static class Builder {
 
 Review comment:
   Could we also add a `private` constructor for `Builder`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to