|
@@ -14,27 +14,18 @@
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
-package org.apache.dolphinscheduler.server.worker.task.datax;
|
|
|
|
|
|
+package org.apache.dolphinscheduler.server.worker.task.datax;
|
|
|
|
|
|
-import com.alibaba.druid.sql.ast.SQLStatement;
|
|
|
-import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
|
|
-import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
|
|
-import com.alibaba.druid.sql.ast.statement.*;
|
|
|
-import com.alibaba.druid.sql.parser.SQLStatementParser;
|
|
|
-import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
-import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.dolphinscheduler.common.Constants;
|
|
|
import org.apache.dolphinscheduler.common.enums.CommandType;
|
|
|
import org.apache.dolphinscheduler.common.enums.DbType;
|
|
|
import org.apache.dolphinscheduler.common.enums.Flag;
|
|
|
-import org.apache.dolphinscheduler.common.enums.Flag;
|
|
|
import org.apache.dolphinscheduler.common.process.Property;
|
|
|
import org.apache.dolphinscheduler.common.task.AbstractParameters;
|
|
|
import org.apache.dolphinscheduler.common.task.datax.DataxParameters;
|
|
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
|
|
|
-import org.apache.dolphinscheduler.common.utils.*;
|
|
|
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
|
|
|
import org.apache.dolphinscheduler.common.utils.OSUtils;
|
|
|
import org.apache.dolphinscheduler.common.utils.ParameterUtils;
|
|
|
import org.apache.dolphinscheduler.dao.datasource.BaseDataSource;
|
|
@@ -46,7 +37,8 @@ import org.apache.dolphinscheduler.server.utils.ParamUtils;
|
|
|
import org.apache.dolphinscheduler.server.worker.task.AbstractTask;
|
|
|
import org.apache.dolphinscheduler.server.worker.task.CommandExecuteResult;
|
|
|
import org.apache.dolphinscheduler.server.worker.task.ShellCommandExecutor;
|
|
|
-import org.slf4j.Logger;
|
|
|
+
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -56,25 +48,48 @@ import java.nio.file.StandardOpenOption;
|
|
|
import java.nio.file.attribute.FileAttribute;
|
|
|
import java.nio.file.attribute.PosixFilePermission;
|
|
|
import java.nio.file.attribute.PosixFilePermissions;
|
|
|
-import java.sql.*;
|
|
|
-import java.util.*;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.ResultSetMetaData;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
+
|
|
|
+import com.alibaba.druid.sql.ast.SQLStatement;
|
|
|
+import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
|
|
+import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
|
|
+import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
|
|
+import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
|
|
+import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
|
|
+import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
|
|
+import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
|
|
+import com.alibaba.druid.sql.parser.SQLStatementParser;
|
|
|
+import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
|
|
/**
|
|
|
* DataX task
|
|
|
*/
|
|
|
public class DataxTask extends AbstractTask {
|
|
|
|
|
|
+ /**
|
|
|
+ * jvm parameters
|
|
|
+ */
|
|
|
+ public static final String JVM_EVN = " --jvm=\"-Xms%sG -Xmx%sG\" ";
|
|
|
/**
|
|
|
* python process(datax only supports version 2.7 by default)
|
|
|
*/
|
|
|
private static final String DATAX_PYTHON = "python2.7";
|
|
|
-
|
|
|
/**
|
|
|
* datax home path
|
|
|
*/
|
|
|
private static final String DATAX_HOME_EVN = "${DATAX_HOME}";
|
|
|
-
|
|
|
/**
|
|
|
* datax channel count
|
|
|
*/
|
|
@@ -97,6 +112,7 @@ public class DataxTask extends AbstractTask {
|
|
|
|
|
|
/**
|
|
|
* constructor
|
|
|
+ *
|
|
|
* @param taskExecutionContext taskExecutionContext
|
|
|
* @param logger logger
|
|
|
*/
|
|
@@ -104,9 +120,8 @@ public class DataxTask extends AbstractTask {
|
|
|
super(taskExecutionContext, logger);
|
|
|
this.taskExecutionContext = taskExecutionContext;
|
|
|
|
|
|
-
|
|
|
this.shellCommandExecutor = new ShellCommandExecutor(this::logHandle,
|
|
|
- taskExecutionContext,logger);
|
|
|
+ taskExecutionContext, logger);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -149,9 +164,7 @@ public class DataxTask extends AbstractTask {
|
|
|
setExitStatusCode(commandExecuteResult.getExitStatusCode());
|
|
|
setAppIds(commandExecuteResult.getAppIds());
|
|
|
setProcessId(commandExecuteResult.getProcessId());
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- logger.error("datax task failure", e);
|
|
|
+ } catch (Exception e) {
|
|
|
setExitStatusCode(Constants.EXIT_CODE_FAILURE);
|
|
|
throw e;
|
|
|
}
|
|
@@ -189,9 +202,9 @@ public class DataxTask extends AbstractTask {
|
|
|
return fileName;
|
|
|
}
|
|
|
|
|
|
- if (dataXParameters.getCustomConfig() == Flag.YES.ordinal()){
|
|
|
+ if (dataXParameters.getCustomConfig() == Flag.YES.ordinal()) {
|
|
|
json = dataXParameters.getJson().replaceAll("\\r\\n", "\n");
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
ObjectNode job = JSONUtils.createObjectNode();
|
|
|
job.putArray("content").addAll(buildDataxJobContentJson());
|
|
|
job.set("setting", buildDataxJobSettingJson());
|
|
@@ -248,7 +261,6 @@ public class DataxTask extends AbstractTask {
|
|
|
readerParam.put("password", dataSourceCfg.getPassword());
|
|
|
readerParam.putArray("connection").addAll(readerConnArr);
|
|
|
|
|
|
-
|
|
|
ObjectNode reader = JSONUtils.createObjectNode();
|
|
|
reader.put("name", DataxUtils.getReaderPluginName(DbType.of(dataxTaskExecutionContext.getSourcetype())));
|
|
|
reader.set("parameter", readerParam);
|
|
@@ -277,7 +289,6 @@ public class DataxTask extends AbstractTask {
|
|
|
}
|
|
|
writerParam.putArray("connection").addAll(writerConnArr);
|
|
|
|
|
|
-
|
|
|
if (CollectionUtils.isNotEmpty(dataXParameters.getPreStatements())) {
|
|
|
ArrayNode preSqlArr = writerParam.putArray("preSql");
|
|
|
for (String preSql : dataXParameters.getPreStatements()) {
|
|
@@ -368,7 +379,7 @@ public class DataxTask extends AbstractTask {
|
|
|
* @throws Exception if error throws Exception
|
|
|
*/
|
|
|
private String buildShellCommandFile(String jobConfigFilePath, Map<String, Property> paramsMap)
|
|
|
- throws Exception {
|
|
|
+ throws Exception {
|
|
|
// generate scripts
|
|
|
String fileName = String.format("%s/%s_node.%s",
|
|
|
taskExecutionContext.getExecutePath(),
|
|
@@ -387,6 +398,7 @@ public class DataxTask extends AbstractTask {
|
|
|
sbr.append(" ");
|
|
|
sbr.append(DATAX_HOME_EVN);
|
|
|
sbr.append(" ");
|
|
|
+ sbr.append(loadJvmEnv(dataXParameters));
|
|
|
sbr.append(jobConfigFilePath);
|
|
|
|
|
|
// replace placeholder
|
|
@@ -409,17 +421,19 @@ public class DataxTask extends AbstractTask {
|
|
|
return fileName;
|
|
|
}
|
|
|
|
|
|
+ public String loadJvmEnv(DataxParameters dataXParameters) {
|
|
|
+ int xms = dataXParameters.getXms() < 1 ? 1 : dataXParameters.getXms();
|
|
|
+ int xmx = dataXParameters.getXmx() < 1 ? 1 : dataXParameters.getXmx();
|
|
|
+ return String.format(JVM_EVN, xms, xmx);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* parsing synchronized column names in SQL statements
|
|
|
*
|
|
|
- * @param dsType
|
|
|
- * the database type of the data source
|
|
|
- * @param dtType
|
|
|
- * the database type of the data target
|
|
|
- * @param dataSourceCfg
|
|
|
- * the database connection parameters of the data source
|
|
|
- * @param sql
|
|
|
- * sql for data synchronization
|
|
|
+ * @param dsType the database type of the data source
|
|
|
+ * @param dtType the database type of the data target
|
|
|
+ * @param dataSourceCfg the database connection parameters of the data source
|
|
|
+ * @param sql sql for data synchronization
|
|
|
* @return Keyword converted column names
|
|
|
*/
|
|
|
private String[] parsingSqlColumnNames(DbType dsType, DbType dtType, BaseDataSource dataSourceCfg, String sql) {
|
|
@@ -438,10 +452,8 @@ public class DataxTask extends AbstractTask {
|
|
|
/**
|
|
|
* try grammatical parsing column
|
|
|
*
|
|
|
- * @param dbType
|
|
|
- * database type
|
|
|
- * @param sql
|
|
|
- * sql for data synchronization
|
|
|
+ * @param dbType database type
|
|
|
+ * @param sql sql for data synchronization
|
|
|
* @return column name array
|
|
|
* @throws RuntimeException if error throws RuntimeException
|
|
|
*/
|
|
@@ -453,16 +465,16 @@ public class DataxTask extends AbstractTask {
|
|
|
notNull(parser, String.format("database driver [%s] is not support", dbType.toString()));
|
|
|
|
|
|
SQLStatement sqlStatement = parser.parseStatement();
|
|
|
- SQLSelectStatement sqlSelectStatement = (SQLSelectStatement)sqlStatement;
|
|
|
+ SQLSelectStatement sqlSelectStatement = (SQLSelectStatement) sqlStatement;
|
|
|
SQLSelect sqlSelect = sqlSelectStatement.getSelect();
|
|
|
|
|
|
List<SQLSelectItem> selectItemList = null;
|
|
|
if (sqlSelect.getQuery() instanceof SQLSelectQueryBlock) {
|
|
|
- SQLSelectQueryBlock block = (SQLSelectQueryBlock)sqlSelect.getQuery();
|
|
|
+ SQLSelectQueryBlock block = (SQLSelectQueryBlock) sqlSelect.getQuery();
|
|
|
selectItemList = block.getSelectList();
|
|
|
} else if (sqlSelect.getQuery() instanceof SQLUnionQuery) {
|
|
|
- SQLUnionQuery unionQuery = (SQLUnionQuery)sqlSelect.getQuery();
|
|
|
- SQLSelectQueryBlock block = (SQLSelectQueryBlock)unionQuery.getRight();
|
|
|
+ SQLUnionQuery unionQuery = (SQLUnionQuery) sqlSelect.getQuery();
|
|
|
+ SQLSelectQueryBlock block = (SQLSelectQueryBlock) unionQuery.getRight();
|
|
|
selectItemList = block.getSelectList();
|
|
|
}
|
|
|
|
|
@@ -470,7 +482,7 @@ public class DataxTask extends AbstractTask {
|
|
|
String.format("select query type [%s] is not support", sqlSelect.getQuery().toString()));
|
|
|
|
|
|
columnNames = new String[selectItemList.size()];
|
|
|
- for (int i = 0; i < selectItemList.size(); i++ ) {
|
|
|
+ for (int i = 0; i < selectItemList.size(); i++) {
|
|
|
SQLSelectItem item = selectItemList.get(i);
|
|
|
|
|
|
String columnName = null;
|
|
@@ -479,10 +491,10 @@ public class DataxTask extends AbstractTask {
|
|
|
columnName = item.getAlias();
|
|
|
} else if (item.getExpr() != null) {
|
|
|
if (item.getExpr() instanceof SQLPropertyExpr) {
|
|
|
- SQLPropertyExpr expr = (SQLPropertyExpr)item.getExpr();
|
|
|
+ SQLPropertyExpr expr = (SQLPropertyExpr) item.getExpr();
|
|
|
columnName = expr.getName();
|
|
|
} else if (item.getExpr() instanceof SQLIdentifierExpr) {
|
|
|
- SQLIdentifierExpr expr = (SQLIdentifierExpr)item.getExpr();
|
|
|
+ SQLIdentifierExpr expr = (SQLIdentifierExpr) item.getExpr();
|
|
|
columnName = expr.getName();
|
|
|
}
|
|
|
} else {
|
|
@@ -497,8 +509,7 @@ public class DataxTask extends AbstractTask {
|
|
|
|
|
|
columnNames[i] = columnName;
|
|
|
}
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
+ } catch (Exception e) {
|
|
|
logger.warn(e.getMessage(), e);
|
|
|
return null;
|
|
|
}
|
|
@@ -509,10 +520,8 @@ public class DataxTask extends AbstractTask {
|
|
|
/**
|
|
|
* try to execute sql to resolve column names
|
|
|
*
|
|
|
- * @param baseDataSource
|
|
|
- * the database connection parameters
|
|
|
- * @param sql
|
|
|
- * sql for data synchronization
|
|
|
+ * @param baseDataSource the database connection parameters
|
|
|
+ * @param sql sql for data synchronization
|
|
|
* @return column name array
|
|
|
*/
|
|
|
public String[] tryExecuteSqlResolveColumnNames(BaseDataSource baseDataSource, String sql) {
|
|
@@ -529,11 +538,10 @@ public class DataxTask extends AbstractTask {
|
|
|
ResultSetMetaData md = resultSet.getMetaData();
|
|
|
int num = md.getColumnCount();
|
|
|
columnNames = new String[num];
|
|
|
- for (int i = 1; i <= num; i++ ) {
|
|
|
+ for (int i = 1; i <= num; i++) {
|
|
|
columnNames[i - 1] = md.getColumnName(i);
|
|
|
}
|
|
|
- }
|
|
|
- catch (SQLException e) {
|
|
|
+ } catch (SQLException e) {
|
|
|
logger.warn(e.getMessage(), e);
|
|
|
return null;
|
|
|
}
|