kezhenxu94 commented on code in PR #10198:
URL: https://github.com/apache/dolphinscheduler/pull/10198#discussion_r880046049


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-openmldb/src/main/java/org/apache/dolphinscheduler/plugin/task/openmldb/OpenmldbTask.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.dolphinscheduler.plugin.task.openmldb;
+
+import org.apache.dolphinscheduler.plugin.task.api.AbstractTaskExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.ShellCommandExecutor;
+import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
+import org.apache.dolphinscheduler.plugin.task.api.TaskException;
+import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
+import org.apache.dolphinscheduler.plugin.task.api.model.Property;
+import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
+import 
org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
+import org.apache.dolphinscheduler.plugin.task.api.parser.ParamUtils;
+import org.apache.dolphinscheduler.plugin.task.api.parser.ParameterUtils;
+import org.apache.dolphinscheduler.plugin.task.api.utils.MapUtils;
+import org.apache.dolphinscheduler.spi.utils.JSONUtils;
+
+import org.apache.commons.io.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import com.google.common.base.Preconditions;
+import org.apache.dolphinscheduler.spi.utils.StringUtils;
+
+/**
+ * openmldb task
+ */
+public class OpenmldbTask extends AbstractTaskExecutor {
+
+    /**
+     * openmldb parameters
+     */
+    private OpenmldbParameters openmldbParameters;
+
+    /**
+     * shell command executor
+     */
+    private final ShellCommandExecutor shellCommandExecutor;
+
+    private final TaskExecutionContext taskRequest;
+
+    private static final String PYTHON_HOME = "PYTHON_HOME";
+
+    /**
+     * python process(openmldb only supports version 3 by default)
+     */
+    private static final String OPENMLDB_PYTHON = "python3";
+    private static final Pattern PYTHON_PATH_PATTERN = 
Pattern.compile("/bin/python[\\d.]*$");
+
+    /**
+     * constructor
+     *
+     * @param taskRequest taskRequest
+     */
+    public OpenmldbTask(TaskExecutionContext taskRequest) {
+        super(taskRequest);
+        this.taskRequest = taskRequest;
+
+        this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle,
+                taskRequest,
+                logger);
+    }
+
+    @Override
+    public void init() {
+        logger.info("openmldb task params {}", taskRequest.getTaskParams());
+
+        openmldbParameters = 
JSONUtils.parseObject(taskRequest.getTaskParams(), OpenmldbParameters.class);
+
+        assert openmldbParameters != null;
+        if (!openmldbParameters.checkParameters()) {
+            throw new TaskException("openmldb task params is not valid");
+        }
+    }
+
+    @Override
+    public void handle() throws Exception {
+        try {
+            // generate the content of this python script
+            String pythonScriptContent = buildPythonScriptContent();
+            // generate the file path of this python script
+            String pythonScriptFile = buildPythonCommandFilePath();
+
+            // create this file
+            createPythonCommandFileIfNotExists(pythonScriptContent, 
pythonScriptFile);
+            String command = buildPythonExecuteCommand(pythonScriptFile);
+
+            TaskResponse taskResponse = shellCommandExecutor.run(command);
+            setExitStatusCode(taskResponse.getExitStatusCode());
+            setAppIds(taskResponse.getAppIds());
+            setProcessId(taskResponse.getProcessId());
+            setVarPool(shellCommandExecutor.getVarPool());
+        } catch (Exception e) {
+            logger.error("openmldb task failure", e);
+            setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE);
+            throw new TaskException("run openmldb task error", e);
+        }
+    }
+
+    @Override
+    public void cancelApplication(boolean cancelApplication) throws Exception {
+        // cancel process
+        shellCommandExecutor.cancelApplication();
+    }
+
+    @Override
+    public AbstractParameters getParameters() {
+        return openmldbParameters;
+    }
+
+    /**
+     * create python command file if not exists
+     *
+     * @param pythonScript     exec python script
+     * @param pythonScriptFile python script file
+     * @throws IOException io exception
+     */
+    protected void createPythonCommandFileIfNotExists(String pythonScript, 
String pythonScriptFile) throws IOException {
+        logger.info("tenantCode :{}, task dir:{}", 
taskRequest.getTenantCode(), taskRequest.getExecutePath());
+
+        if (!Files.exists(Paths.get(pythonScriptFile))) {
+            logger.info("generate python script file:{}", pythonScriptFile);
+
+            StringBuilder sb = new StringBuilder();
+            sb.append("#-*- encoding=utf8 -*-\n");
+
+            sb.append("\n\n");
+            sb.append(pythonScript);
+            logger.info(sb.toString());
+
+            // write data to file
+            FileUtils.writeStringToFile(new File(pythonScriptFile),
+                    sb.toString(),
+                    StandardCharsets.UTF_8);
+        }
+    }
+
+    /**
+     * build python command file path
+     *
+     * @return python command file path
+     */
+    protected String buildPythonCommandFilePath() {
+        return String.format("%s/openmldb_%s.py", 
taskRequest.getExecutePath(), taskRequest.getTaskAppId());
+    }
+
+    /**
+     * build python script content
+     *
+     * @return raw python script
+     */
+    private String buildPythonScriptContent() {
+        // sqls doesn't need \n, use ; to split
+        String rawSqlScript = 
openmldbParameters.getSql().replaceAll("[\\r]?\\n", " ");

Review Comment:
   Hi, I don't think you can simply replace all line breaks with spaces, if 
there are comments in the sql scripts, you will totally change the logic of it, 
for example, if the original sql is 
   
   ```sql
   select * from users
   -- some comment
   inner join order on users.order_id = order.id;
   ```
   
   will become 
   
   ```sql
   select * from users -- some comment inner join order on users.order_id = 
order.id;
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to