You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by GitBox <gi...@apache.org> on 2022/07/22 16:08:52 UTC

[GitHub] [inlong] haibo-duan opened a new pull request, #5187: [INLONG-5085][Manager] Support create Oracle databases and tables

haibo-duan opened a new pull request, #5187:
URL: https://github.com/apache/inlong/pull/5187

   ### Prepare a Pull Request
   *(Change the title refer to the following example)*
   
   - Title Example: [INLONG-XYZ][Component] Title of the pull request
   
   *(The following *XYZ* should be replaced by the actual [GitHub Issue](https://github.com/apache/inlong/issues) number)*
   
   - About #5086 
   
   ### Motivation
   
   *Explain here the context, and why you're making that change. What is the problem you're trying to solve?*
   
   ### Modifications
   
   *Describe the modifications you've done.*
   
   ### Verifying this change
   
   *(Please pick either of the following options)*
   
   - [ ] This change is a trivial rework/code cleanup without any test coverage.
   
   - [ ] This change is already covered by existing tests, such as:
     *(please describe tests)*
   
   - [ ] This change added tests and can be verified as follows:
   
     *(example:)*
     - *Added integration tests for end-to-end deployment with large payloads (10MB)*
     - *Extended integration test for recovery after broker failure*
   
   ### Documentation
   
     - Does this pull request introduce a new feature? (yes / no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
     - If a feature is not applicable for documentation, explain why?
     - If a feature is not documented yet in this PR, please create a follow-up issue for adding the documentation
   


-- 
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@inlong.apache.org

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


[GitHub] [inlong] healchow commented on a diff in pull request #5187: [INLONG-5086][Manager] Support create Oracle databases and tables

Posted by GitBox <gi...@apache.org>.
healchow commented on code in PR #5187:
URL: https://github.com/apache/inlong/pull/5187#discussion_r928186474


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/oracle/OracleJdbcUtils.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.inlong.manager.service.resource.oracle;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.inlong.manager.common.pojo.sink.oracle.OracleColumnInfo;
+import org.apache.inlong.manager.common.pojo.sink.oracle.OracleTableInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Utils for Oracle JDBC.
+ */
+public class OracleJdbcUtils {
+
+    private static final String ORACLE_JDBC_PREFIX = "jdbc:oracle";
+
+    private static final String ORACLE_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
+
+    private static final Logger LOG = LoggerFactory.getLogger(OracleJdbcUtils.class);
+
+    /**
+     * Get Oracle connection from the url and user.
+     *
+     * @param url jdbc url,such as jdbc:oracle:thin@host:port:sid or jdbc:oracle:thin@host:port/service_name
+     * @param user Username for JDBC URL
+     * @param password User password
+     * @return {@link Connection}
+     * @throws Exception on get connection error
+     */
+    public static Connection getConnection(String url, String user, String password)
+            throws Exception {
+        if (StringUtils.isBlank(url) || !url.startsWith(ORACLE_JDBC_PREFIX)) {
+            throw new Exception("Oracle server URL was invalid, it should start with jdbc:oracle");
+        }
+        Connection conn;
+        try {
+            Class.forName(ORACLE_DRIVER_CLASS);
+            conn = DriverManager.getConnection(url, user, password);
+        } catch (Exception e) {
+            String errorMsg = "get Oracle connection error, please check Oracle JDBC url, username or password!";
+            LOG.error(errorMsg, e);
+            throw new Exception(errorMsg + " other error msg: " + e.getMessage());
+        }
+        if (conn == null) {
+            throw new Exception("get Oracle connection failed, please contact administrator.");
+        }
+        LOG.info("get Oracle connection success, url={}", url);
+        return conn;
+    }
+
+    /**
+     * Execute SQL command on Oracle.
+     *
+     * @param conn JDBC Connection  {@link Connection}
+     * @param sql SQL string to be executed
+     * @throws Exception on execute SQL error
+     */
+    public static void executeSql(Connection conn, String sql) throws Exception {
+        Statement stmt = conn.createStatement();
+        LOG.info("execute sql [{}] success !", sql);

Review Comment:
   This successful log should be written after the execute method.



##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/oracle/OracleResourceOperator.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.inlong.manager.service.resource.oracle;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.inlong.manager.common.consts.InlongConstants;
+import org.apache.inlong.manager.common.enums.SinkStatus;
+import org.apache.inlong.manager.common.enums.SinkType;
+import org.apache.inlong.manager.common.exceptions.WorkflowException;
+import org.apache.inlong.manager.common.pojo.sink.SinkInfo;
+import org.apache.inlong.manager.common.pojo.sink.oracle.OracleColumnInfo;
+import org.apache.inlong.manager.common.pojo.sink.oracle.OracleSinkDTO;
+import org.apache.inlong.manager.common.pojo.sink.oracle.OracleTableInfo;
+import org.apache.inlong.manager.dao.entity.StreamSinkFieldEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSinkFieldEntityMapper;
+import org.apache.inlong.manager.service.resource.SinkResourceOperator;
+import org.apache.inlong.manager.service.sink.StreamSinkService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.List;
+
+public class OracleResourceOperator implements SinkResourceOperator {
+
+
+    private static final Logger LOG = LoggerFactory.getLogger(OracleResourceOperator.class);
+
+    @Autowired
+    private StreamSinkService sinkService;
+
+    @Autowired
+    private StreamSinkFieldEntityMapper fieldEntityMapper;
+
+    @Override
+    public Boolean accept(SinkType sinkType) {
+        return SinkType.ORACLE == sinkType;
+    }
+
+    @Override
+    public void createSinkResource(SinkInfo sinkInfo) {
+        LOG.info("begin to create Oracle resources sinkId={}", sinkInfo.getId());
+        if (SinkStatus.CONFIG_SUCCESSFUL.getCode().equals(sinkInfo.getStatus())) {
+            LOG.warn("Oracle resource [" + sinkInfo.getId() + "] already success, skip to create");
+            return;
+        } else if (InlongConstants.DISABLE_CREATE_RESOURCE.equals(sinkInfo.getEnableCreateResource())) {
+            LOG.warn("create resource was disabled, skip to create for [" + sinkInfo.getId() + "]");
+            return;
+        }
+        this.createTable(sinkInfo);
+    }
+
+    /**
+     * Create Oracle table by SinkInfo.
+     *
+     * @param sinkInfo {@link SinkInfo}
+     */
+    private void createTable(SinkInfo sinkInfo) {
+        LOG.info("begin to create Oracle table for sinkId={}", sinkInfo.getId());
+        List<StreamSinkFieldEntity> fieldList = fieldEntityMapper.selectBySinkId(sinkInfo.getId());
+        if (CollectionUtils.isEmpty(fieldList)) {
+            LOG.warn("no Oracle fields found, skip to create table for sinkId={}", sinkInfo.getId());
+        }
+        // set columns
+        List<OracleColumnInfo> columnList = new ArrayList<>();
+        for (StreamSinkFieldEntity field : fieldList) {
+            OracleColumnInfo columnInfo = new OracleColumnInfo();
+            columnInfo.setName(field.getFieldName());
+            columnInfo.setType(field.getFieldType());
+            columnInfo.setComment(field.getFieldComment());
+            columnList.add(columnInfo);
+        }
+
+        OracleSinkDTO oracleSink = OracleSinkDTO.getFromJson(sinkInfo.getExtParams());
+        OracleTableInfo tableInfo = OracleSinkDTO.getTableInfo(oracleSink, columnList);
+        String url = oracleSink.getJdbcUrl();
+        String user = oracleSink.getUsername();
+        String password = oracleSink.getPassword();
+        String tableName = tableInfo.getTableName();
+        Connection conn = null;
+        try {
+            conn = OracleJdbcUtils.getConnection(url, user, password);
+
+            //In Oracle, there is no need to consider whether the database exists

Review Comment:
   Please add one blank after `//`, thanks.



-- 
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@inlong.apache.org

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


[GitHub] [inlong] healchow merged pull request #5187: [INLONG-5086][Manager] Support create Oracle databases and tables

Posted by GitBox <gi...@apache.org>.
healchow merged PR #5187:
URL: https://github.com/apache/inlong/pull/5187


-- 
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@inlong.apache.org

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


[GitHub] [inlong] haibo-duan commented on pull request #5187: [INLONG-5085][Manager] Support create Oracle databases and tables

Posted by GitBox <gi...@apache.org>.
haibo-duan commented on PR #5187:
URL: https://github.com/apache/inlong/pull/5187#issuecomment-1192730957

   @healchow @dockerzhang  pls review it.


-- 
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@inlong.apache.org

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