Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Connector-V2][Teradata] Add Teradata Source And Sink Connector #3362

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/en/connector-v2/sink/Jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ there are some reference value for params above.
| GBase8a | com.gbase.jdbc.Driver | jdbc:gbase://e2e_gbase8aDb:5258/test | / | https://www.gbase8.cn/wp-content/uploads/2020/10/gbase-connector-java-8.3.81.53-build55.5.7-bin_min_mix.jar |
| StarRocks | com.mysql.cj.jdbc.Driver | jdbc:mysql://localhost:3306/test | / | https://mvnrepository.com/artifact/mysql/mysql-connector-java |
| db2 | com.ibm.db2.jcc.DB2Driver | jdbc:db2://localhost:50000/testdb | com.ibm.db2.jcc.DB2XADataSource | https://mvnrepository.com/artifact/com.ibm.db2.jcc/db2jcc/db2jcc4 |
| teradata | com.teradata.jdbc.TeraDriver | jdbc:teradata://localhost/DBS_PORT=1025,DATABASE=test | / | https://mvnrepository.com/artifact/com.teradata.jdbc/terajdbc |

## Example

Expand Down Expand Up @@ -200,5 +201,7 @@ sink {

### next version

- [Feature] Support CDC write DELETE/UPDATE/INSERT events ([3378](https://github.com/apache/incubator-seatunnel/issues/3378))
- [Feature] Support Teradata JDBC Sink ([3362](https://github.com/apache/incubator-seatunnel/pull/3362))
- [Feature] Support Sqlite JDBC Sink ([3089](https://github.com/apache/incubator-seatunnel/pull/3089))
- [Feature] Support CDC write DELETE/UPDATE/INSERT events ([3378](https://github.com/apache/incubator-seatunnel/issues/3378))
- [Feature] Support CDC write DELETE/UPDATE/INSERT events ([3378](https://github.com/apache/incubator-seatunnel/issues/3378))
2 changes: 2 additions & 0 deletions docs/en/connector-v2/source/Jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ there are some reference value for params above.
| starrocks | com.mysql.cj.jdbc.Driver | jdbc:mysql://localhost:3306/test | https://mvnrepository.com/artifact/mysql/mysql-connector-java |
| db2 | com.ibm.db2.jcc.DB2Driver | jdbc:db2://localhost:50000/testdb | https://mvnrepository.com/artifact/com.ibm.db2.jcc/db2jcc/db2jcc4 |
| tablestore | com.alicloud.openservices.tablestore.jdbc.OTSDriver | "jdbc:ots:http s://myinstance.cn-hangzhou.ots.aliyuncs.com/myinstance" | https://mvnrepository.com/artifact/com.aliyun.openservices/tablestore-jdbc |
| teradata | com.teradata.jdbc.TeraDriver | jdbc:teradata://localhost/DBS_PORT=1025,DATABASE=test | https://mvnrepository.com/artifact/com.teradata.jdbc/terajdbc |

## Example

Expand Down Expand Up @@ -159,4 +160,5 @@ parallel:
- [BugFix] Fix jdbc split bug ([3220](https://github.com/apache/incubator-seatunnel/pull/3220))
- [Feature] Support Sqlite JDBC Source ([3089](https://github.com/apache/incubator-seatunnel/pull/3089))
- [Feature] Support Tablestore Source ([3309](https://github.com/apache/incubator-seatunnel/pull/3309))
- [Feature] Support Teradata JDBC Source ([3362](https://github.com/apache/incubator-seatunnel/pull/3362))
- [Feature] Support JDBC Fetch Size Config ([3478](https://github.com/apache/incubator-seatunnel/pull/3478))
13 changes: 13 additions & 0 deletions seatunnel-connectors-v2/connector-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<db2.version>db2jcc4</db2.version>
<sqlite.version>3.39.3.0</sqlite.version>
<tablestore.version>5.13.9</tablestore.version>
<teradata.version>17.20.00.12</teradata.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -98,6 +99,14 @@
<version>${tablestore.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.teradata.jdbc</groupId>
<artifactId>terajdbc4</artifactId>
<version>${teradata.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -147,6 +156,10 @@
<groupId>com.aliyun.openservices</groupId>
<artifactId>tablestore-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.teradata.jdbc</groupId>
<artifactId>terajdbc4</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.internal.dialect.teradata;

import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.JdbcRowConverter;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectTypeMapper;

import java.util.Optional;

public class TeradataDialect implements JdbcDialect {

@Override
public String dialectName() {
return "Teradata";
}

@Override
public JdbcRowConverter getRowConverter() {
return new TeradataJdbcRowConverter();
}

@Override
public JdbcDialectTypeMapper getJdbcDialectTypeMapper() {
return new TeradataTypeMapper();
}

@Override
public Optional<String> getUpsertStatement(String tableName, String[] fieldNames, String[] uniqueKeyFields) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.internal.dialect.teradata;

import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialect;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectFactory;

import com.google.auto.service.AutoService;

@AutoService(JdbcDialectFactory.class)
public class TeradataDialectFactory implements JdbcDialectFactory {

@Override
public boolean acceptsURL(String url) {
return url.startsWith("jdbc:teradata:");
}

@Override
public JdbcDialect create() {
return new TeradataDialect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.internal.dialect.teradata;

import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.converter.AbstractJdbcRowConverter;

public class TeradataJdbcRowConverter extends AbstractJdbcRowConverter {

@Override
public String converterName() {
return "Teradata";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.internal.dialect.teradata;

import org.apache.seatunnel.api.table.type.BasicType;
import org.apache.seatunnel.api.table.type.DecimalType;
import org.apache.seatunnel.api.table.type.LocalTimeType;
import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.JdbcDialectTypeMapper;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class TeradataTypeMapper implements JdbcDialectTypeMapper {

// ============================data types=====================

// -------------------------number----------------------------
private static final String TERADATA_BYTEINT = "BYTEINT";
private static final String TERADATA_SMALLINT = "SMALLINT";
private static final String TERADATA_INTEGER = "INTEGER";
private static final String TERADATA_BIGINT = "BIGINT";
private static final String TERADATA_FLOAT = "FLOAT";
private static final String TERADATA_DECIMAL = "DECIMAL";

// -------------------------string----------------------------
private static final String TERADATA_CHAR = "CHAR";
private static final String TERADATA_VARCHAR = "VARCHAR";
private static final String TERADATA_CLOB = "CLOB";


// ---------------------------binary---------------------------
private static final String TERADATA_BYTE = "BYTE";
private static final String TERADATA_VARBYTE = "VARBYTE";

// ------------------------------time-------------------------
private static final String TERADATA_DATE = "DATE";
private static final String TERADATA_TIME = "TIME";
private static final String TERADATA_TIMESTAMP = "TIMESTAMP";

// ------------------------------blob-------------------------
private static final String TERADATA_BLOB = "BLOB";

@Override
public SeaTunnelDataType<?> mapping(ResultSetMetaData metadata, int colIndex) throws SQLException {
String teradataType = metadata.getColumnTypeName(colIndex).toUpperCase();
switch (teradataType) {
case TERADATA_BYTEINT:
return BasicType.BYTE_TYPE;
case TERADATA_SMALLINT:
return BasicType.SHORT_TYPE;
case TERADATA_INTEGER:
return BasicType.INT_TYPE;
case TERADATA_BIGINT:
return BasicType.LONG_TYPE;
case TERADATA_FLOAT:
return BasicType.FLOAT_TYPE;
case TERADATA_DECIMAL:
return new DecimalType(metadata.getPrecision(colIndex), metadata.getScale(colIndex));
case TERADATA_CHAR:
case TERADATA_VARCHAR:
case TERADATA_CLOB:
return BasicType.STRING_TYPE;
case TERADATA_BYTE:
case TERADATA_VARBYTE:
case TERADATA_BLOB:
return PrimitiveByteArrayType.INSTANCE;
case TERADATA_DATE:
return LocalTimeType.LOCAL_DATE_TYPE;
case TERADATA_TIME:
return LocalTimeType.LOCAL_TIME_TYPE;
case TERADATA_TIMESTAMP:
return LocalTimeType.LOCAL_DATE_TIME_TYPE;
default:
final String jdbcColumnName = metadata.getColumnName(colIndex);
throw new UnsupportedOperationException(
String.format(
"Doesn't support TERADATA type '%s' on column '%s' yet.",
teradataType, jdbcColumnName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.teradata.jdbc</groupId>
<artifactId>terajdbc4</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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;

import org.apache.seatunnel.e2e.common.TestResource;
import org.apache.seatunnel.e2e.common.TestSuiteBase;
import org.apache.seatunnel.e2e.common.container.ContainerExtendedFactory;
import org.apache.seatunnel.e2e.common.container.TestContainer;

import com.teradata.jdbc.TeraDataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.TestTemplate;
import org.testcontainers.containers.Container;

import java.sql.Connection;
import java.sql.Statement;

@Disabled("Disabled because it needs user's personal teradata account to run this test!")
public class JdbcTeradataIT extends TestSuiteBase implements TestResource {
private static final String HOST = "1.2.3.4";
private static final String PORT = "1025";
private static final String USERNAME = "dbc";
private static final String PASSWORD = "dbc";
private static final String DATABASE = "test";
private static final String SINK_TABLE = "sink_table";
private static final String TERADATA_DRIVER_JAR = "https://repo1.maven.org/maven2/com/teradata/jdbc/terajdbc4/17.20.00.12/terajdbc4-17.20.00.12.jar";
private final ContainerExtendedFactory extendedFactory = container -> {
container.execInContainer("bash", "-c", "mkdir -p /tmp/seatunnel/plugins/Jdbc/lib && cd /tmp/seatunnel/plugins/Jdbc/lib && curl -O " + TERADATA_DRIVER_JAR);
};

private Connection connection;

@TestTemplate
public void testTeradata(TestContainer container) throws Exception {
container.executeExtraCommands(extendedFactory);
Container.ExecResult execResult = container.executeJob("/jdbc_teradata_source_and_sink.conf");
Assertions.assertEquals(0, execResult.getExitCode());
clearSinkTable();
Comment on lines +52 to +56
Copy link
Member

@hailin0 hailin0 Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Teradata does not have a Docker image, and it seems that it cannot be made by itself.

}

private void clearSinkTable() {
try (Statement statement = connection.createStatement()) {
statement.execute(String.format("delete from %s", SINK_TABLE));
} catch (Exception e) {
throw new RuntimeException("Test teradata server failed!", e);
}
}

@BeforeAll
@Override
public void startUp() throws Exception {
TeraDataSource teraDataSource = new TeraDataSource();
teraDataSource.setDSName(HOST);
teraDataSource.setDbsPort(PORT);
teraDataSource.setUser(USERNAME);
teraDataSource.setPassword(PASSWORD);
teraDataSource.setDATABASE(DATABASE);
this.connection = teraDataSource.getConnection();
}

@AfterAll
@Override
public void tearDown() throws Exception {
if (connection != null) {
this.connection.close();
}
}
}
Loading