You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/10/24 14:09:30 UTC

[GitHub] [incubator-seatunnel] TaoZex commented on a diff in pull request #3145: [Feature][st-engine] Support for transform-v2 API

TaoZex commented on code in PR #3145:
URL: https://github.com/apache/incubator-seatunnel/pull/3145#discussion_r1002882999


##########
seatunnel-dist/src/main/assembly/assembly-bin.xml:
##########
@@ -155,6 +155,19 @@
                 <include>org.apache.seatunnel:seatunnel-starter:jar</include>
             </includes>
             <outputFileNameMapping>${artifact.file.name}</outputFileNameMapping>
+            <outputDirectory>/starter</outputDirectory>
+            <scope>provided</scope>
+        </dependencySet>
+
+        <!-- ============ SeaTunnel Transforms Jars ============  -->

Review Comment:
   How about use “SeaTunnel Transforms V2 Jars”?



##########
seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/ReplaceTransform.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.seatunnel.transform;
+
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.api.transform.SeaTunnelTransform;
+import org.apache.seatunnel.transform.common.SeaTunnelRowAccessor;
+import org.apache.seatunnel.transform.common.SingleFieldOutputTransform;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.auto.service.AutoService;
+
+@AutoService(SeaTunnelTransform.class)
+public class ReplaceTransform extends SingleFieldOutputTransform {
+
+    private static final String KEY_REPLACE_FIELD = "replace_field";
+    private static final String KEY_PATTERN = "pattern";
+    private static final String KEY_REPLACEMENT = "replacement";
+    private static final String KEY_IS_REGEX = "is_regex";
+    private static final String KEY_REPLACE_FIRST = "replace_first";
+
+    private int inputFieldIndex;
+    private String replaceField;
+    private String pattern;
+    private String replacement;
+    private boolean isRegex;
+    private boolean replaceFirst;
+
+    @Override
+    public String getPluginName() {
+        return "Replace";
+    }
+
+    @Override
+    protected void setConfig(Config pluginConfig) {
+        if (!pluginConfig.hasPath(KEY_REPLACE_FIELD)) {
+            throw new IllegalArgumentException("The configuration missing key: " + KEY_REPLACE_FIELD);
+        }
+        replaceField = pluginConfig.getString(KEY_REPLACE_FIELD);
+
+        if (!pluginConfig.hasPath(KEY_PATTERN)) {
+            throw new IllegalArgumentException("The configuration missing key: " + KEY_PATTERN);
+        }
+        pattern = pluginConfig.getString(KEY_PATTERN);
+
+        if (!pluginConfig.hasPath(KEY_REPLACEMENT)) {
+            throw new IllegalArgumentException("The configuration missing key: " + KEY_REPLACEMENT);
+        }
+        replacement = pluginConfig.getString(KEY_REPLACEMENT);
+
+        if (pluginConfig.hasPath(KEY_IS_REGEX)) {
+            isRegex = pluginConfig.getBoolean(KEY_IS_REGEX);
+        }
+        if (pluginConfig.hasPath(KEY_REPLACE_FIRST)) {
+            replaceFirst = pluginConfig.getBoolean(KEY_REPLACE_FIRST);
+        }
+    }
+
+    @Override
+    protected void setInputRowType(SeaTunnelRowType rowType) {
+        inputFieldIndex = rowType.indexOf(replaceField);
+    }
+
+    @Override
+    protected String getOutputFieldName() {
+        return replaceField;
+    }
+
+    @Override
+    protected SeaTunnelDataType getOutputFieldDataType() {
+        return BasicType.STRING_TYPE;
+    }
+
+    @Override
+    protected Object getOutputFieldValue(SeaTunnelRowAccessor inputRow) {
+        Object inputFieldValue = inputRow.getField(inputFieldIndex);
+        if (inputFieldValue == null) {
+            return null;
+        }
+
+        if (isRegex) {
+            if (replaceFirst) {
+                return inputFieldValue.toString().replaceFirst(pattern, replacement);
+            }
+            return inputFieldValue.toString().replaceAll(pattern, replacement);
+        }
+        return inputFieldValue.toString().replace(pattern, replacement);

Review Comment:
   If there are multiple field names containing pattern, do we need to implement a method to replace the first pattern?



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org