Skip to content

Commit

Permalink
[Feature][Connector-V2][Jdbc] Add OceanBase catalog (apache#5439)
Browse files Browse the repository at this point in the history
* feat: add oceanbase jdbc catalog

* remove oceanbase-client and disable it cases

* update EXCLUDE_SCHEMAS and skip database check in OceanBase Oracle mode
  • Loading branch information
whhe authored Oct 16, 2023
1 parent a15b79f commit cd4b7ff
Show file tree
Hide file tree
Showing 11 changed files with 466 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public interface JdbcCatalogOptions {
.withDescription(
"for databases that support the schema parameter, give it priority.");

Option<String> COMPATIBLE_MODE =
Options.key("compatibleMode")
.stringType()
.noDefaultValue()
.withDescription(
"The compatible mode of database, required when the database supports multiple compatible modes. "
+ "For example, when using OceanBase database, you need to set it to 'mysql' or 'oracle'.");

OptionRule.Builder BASE_RULE =
OptionRule.builder().required(BASE_URL).required(USERNAME, PASSWORD).optional(SCHEMA);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.connectors.seatunnel.jdbc.catalog.oceanbase;

import org.apache.seatunnel.shade.com.google.common.base.Preconditions;

import org.apache.seatunnel.api.configuration.ReadonlyConfig;
import org.apache.seatunnel.api.configuration.util.OptionRule;
import org.apache.seatunnel.api.configuration.util.OptionValidationException;
import org.apache.seatunnel.api.table.catalog.Catalog;
import org.apache.seatunnel.api.table.factory.CatalogFactory;
import org.apache.seatunnel.api.table.factory.Factory;
import org.apache.seatunnel.common.utils.JdbcUrlUtil;
import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.JdbcCatalogOptions;

import org.apache.commons.lang3.StringUtils;

import com.google.auto.service.AutoService;

import java.util.Optional;

@AutoService(Factory.class)
public class OceanBaseCatalogFactory implements CatalogFactory {

public static final String IDENTIFIER = "OceanBase";

@Override
public String factoryIdentifier() {
return IDENTIFIER;
}

@Override
public Catalog createCatalog(String catalogName, ReadonlyConfig options) {
String urlWithDatabase = options.get(JdbcCatalogOptions.BASE_URL);
Preconditions.checkArgument(
StringUtils.isNoneBlank(urlWithDatabase),
"Miss config <base-url>! Please check your config.");
JdbcUrlUtil.UrlInfo urlInfo = JdbcUrlUtil.getUrlInfo(urlWithDatabase);
Optional<String> defaultDatabase = urlInfo.getDefaultDatabase();
if (!defaultDatabase.isPresent()) {
throw new OptionValidationException(JdbcCatalogOptions.BASE_URL);
}

String compatibleMode = options.get(JdbcCatalogOptions.COMPATIBLE_MODE);
Preconditions.checkArgument(
StringUtils.isNoneBlank(compatibleMode),
"Miss config <compatibleMode>! Please check your config.");

if ("oracle".equalsIgnoreCase(compatibleMode.trim())) {
return new OceanBaseOracleCatalog(
catalogName,
options.get(JdbcCatalogOptions.USERNAME),
options.get(JdbcCatalogOptions.PASSWORD),
urlInfo,
options.get(JdbcCatalogOptions.SCHEMA));
}
return new OceanBaseMySqlCatalog(
catalogName,
options.get(JdbcCatalogOptions.USERNAME),
options.get(JdbcCatalogOptions.PASSWORD),
urlInfo);
}

@Override
public OptionRule optionRule() {
return JdbcCatalogOptions.BASE_RULE.required(JdbcCatalogOptions.COMPATIBLE_MODE).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.connectors.seatunnel.jdbc.catalog.oceanbase;

import org.apache.seatunnel.common.utils.JdbcUrlUtil;
import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.mysql.MySqlCatalog;

public class OceanBaseMySqlCatalog extends MySqlCatalog {

static {
SYS_DATABASES.clear();
SYS_DATABASES.add("information_schema");
SYS_DATABASES.add("mysql");
SYS_DATABASES.add("oceanbase");
SYS_DATABASES.add("LBACSYS");
SYS_DATABASES.add("ORAAUDITOR");
SYS_DATABASES.add("SYS");
}

public OceanBaseMySqlCatalog(
String catalogName, String username, String pwd, JdbcUrlUtil.UrlInfo urlInfo) {
super(catalogName, username, pwd, urlInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.connectors.seatunnel.jdbc.catalog.oceanbase;

import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.catalog.TablePath;
import org.apache.seatunnel.api.table.catalog.exception.CatalogException;
import org.apache.seatunnel.api.table.catalog.exception.DatabaseNotExistException;
import org.apache.seatunnel.api.table.catalog.exception.TableAlreadyExistException;
import org.apache.seatunnel.common.utils.JdbcUrlUtil;
import org.apache.seatunnel.connectors.seatunnel.jdbc.catalog.oracle.OracleCatalog;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.apache.seatunnel.shade.com.google.common.base.Preconditions.checkNotNull;

public class OceanBaseOracleCatalog extends OracleCatalog {

static {
EXCLUDED_SCHEMAS =
Collections.unmodifiableList(
Arrays.asList("oceanbase", "LBACSYS", "ORAAUDITOR", "SYS"));
}

public OceanBaseOracleCatalog(
String catalogName,
String username,
String pwd,
JdbcUrlUtil.UrlInfo urlInfo,
String defaultSchema) {
super(catalogName, username, pwd, urlInfo, defaultSchema);
}

@Override
protected String getListDatabaseSql() {
throw new UnsupportedOperationException();
}

@Override
public List<String> listTables(String databaseName)
throws CatalogException, DatabaseNotExistException {
String dbUrl = getUrlFromDatabaseName(databaseName);
try {
return queryString(dbUrl, getListTableSql(databaseName), this::getTableName);
} catch (Exception e) {
throw new CatalogException(
String.format("Failed listing database in catalog %s", catalogName), e);
}
}

@Override
public boolean tableExists(TablePath tablePath) throws CatalogException {
try {
return listTables(tablePath.getDatabaseName()).contains(getTableName(tablePath));
} catch (DatabaseNotExistException e) {
return false;
}
}

@Override
public void createTable(TablePath tablePath, CatalogTable table, boolean ignoreIfExists)
throws TableAlreadyExistException, DatabaseNotExistException, CatalogException {
checkNotNull(tablePath, "Table path cannot be null");

if (defaultSchema.isPresent()) {
tablePath =
new TablePath(
tablePath.getDatabaseName(),
defaultSchema.get(),
tablePath.getTableName());
}

if (tableExists(tablePath)) {
if (ignoreIfExists) {
return;
}
throw new TableAlreadyExistException(catalogName, tablePath);
}

createTableInternal(tablePath, table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class OracleCatalog extends AbstractJdbcCatalog {
private static final OracleDataTypeConvertor DATA_TYPE_CONVERTOR =
new OracleDataTypeConvertor();

private static final List<String> EXCLUDED_SCHEMAS =
protected static List<String> EXCLUDED_SCHEMAS =
Collections.unmodifiableList(
Arrays.asList(
"APPQOSSYS",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,11 @@ public String buildTableInfoWithSchema(String schema, String table) {
@Override
public void startUp() {
dbServer = initContainer().withImagePullPolicy(PullPolicy.alwaysPull());
jdbcCase = getJdbcCase();

Startables.deepStart(Stream.of(dbServer)).join();

jdbcCase = getJdbcCase();

given().ignoreExceptions()
.await()
.atMost(360, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,86 +17,40 @@

package org.apache.seatunnel.connectors.seatunnel.jdbc;

import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;

import org.apache.commons.lang3.tuple.Pair;

import org.junit.jupiter.api.Assertions;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public abstract class JdbcOceanBaseITBase extends AbstractJdbcIT {

private static final String OCEANBASE_DATABASE = "seatunnel";
private static final String OCEANBASE_SOURCE = "source";
private static final String OCEANBASE_SINK = "sink";

private static final String OCEANBASE_JDBC_TEMPLATE = "jdbc:oceanbase://" + HOST + ":%s";
private static final String OCEANBASE_DRIVER_CLASS = "com.oceanbase.jdbc.Driver";

abstract String imageName();
protected static final String OCEANBASE_SOURCE = "source";
protected static final String OCEANBASE_SINK = "sink";

abstract String host();
protected static final String OCEANBASE_CATALOG_TABLE = "catalog_table";

abstract int port();

abstract String username();

abstract String password();
protected static final String OCEANBASE_JDBC_TEMPLATE = "jdbc:oceanbase://" + HOST + ":%s/%s";
protected static final String OCEANBASE_DRIVER_CLASS = "com.oceanbase.jdbc.Driver";

abstract List<String> configFile();

abstract String createSqlTemplate();

abstract String[] getFieldNames();

@Override
JdbcCase getJdbcCase() {
Map<String, String> containerEnv = new HashMap<>();
String jdbcUrl = String.format(OCEANBASE_JDBC_TEMPLATE, port());
Pair<String[], List<SeaTunnelRow>> testDataSet = initTestData();
String[] fieldNames = testDataSet.getKey();

String insertSql = insertTable(OCEANBASE_DATABASE, OCEANBASE_SOURCE, fieldNames);

return JdbcCase.builder()
.dockerImage(imageName())
.networkAliases(host())
.containerEnv(containerEnv)
.driverClass(OCEANBASE_DRIVER_CLASS)
.host(HOST)
.port(port())
.localPort(port())
.jdbcTemplate(OCEANBASE_JDBC_TEMPLATE)
.jdbcUrl(jdbcUrl)
.userName(username())
.password(password())
.database(OCEANBASE_DATABASE)
.sourceTable(OCEANBASE_SOURCE)
.sinkTable(OCEANBASE_SINK)
.createSql(createSqlTemplate())
.configFile(configFile())
.insertSql(insertSql)
.testData(testDataSet)
.build();
}
abstract String getFullTableName(String tableName);

@Override
void compareResult() {
String sourceSql =
String.format(
"select * from %s.%s order by 1", OCEANBASE_DATABASE, OCEANBASE_SOURCE);
String.format("select * from %s order by 1", getFullTableName(OCEANBASE_SOURCE));
String sinkSql =
String.format("select * from %s.%s order by 1", OCEANBASE_DATABASE, OCEANBASE_SINK);
String.format("select * from %s order by 1", getFullTableName(OCEANBASE_SINK));
try {
Statement sourceStatement = connection.createStatement();
Statement sinkStatement = connection.createStatement();
Expand Down Expand Up @@ -133,15 +87,4 @@ void compareResult() {
String driverUrl() {
return "https://repo1.maven.org/maven2/com/oceanbase/oceanbase-client/2.4.3/oceanbase-client-2.4.3.jar";
}

@Override
protected void createSchemaIfNeeded() {
String sql = "CREATE DATABASE IF NOT EXISTS " + OCEANBASE_DATABASE;
try {
connection.prepareStatement(sql).executeUpdate();
} catch (Exception e) {
throw new SeaTunnelRuntimeException(
JdbcITErrorCode.CREATE_TABLE_FAILED, "Fail to execute sql " + sql, e);
}
}
}
Loading

0 comments on commit cd4b7ff

Please sign in to comment.