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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
