Skip to content

Commit

Permalink
[Feature][Connector-V2] Jdbc connector support SAP HANA. (apache#3017)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlechazoW authored and lhyundeadsoul committed Jan 3, 2023
1 parent 0e7638d commit 7ecc19e
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/en/connector-v2/sink/Jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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 |
| saphana | com.sap.db.jdbc.Driver | jdbc:sap://localhost:39015 | / | https://mvnrepository.com/artifact/com.sap.cloud.db.jdbc/ngdbc |
| Doris | com.mysql.cj.jdbc.Driver | jdbc:mysql://localhost:3306/test | / | https://mvnrepository.com/artifact/mysql/mysql-connector-java |
| teradata | com.teradata.jdbc.TeraDriver | jdbc:teradata://localhost/DBS_PORT=1025,DATABASE=test | / | https://mvnrepository.com/artifact/com.teradata.jdbc/terajdbc |
| Redshift | com.amazon.redshift.jdbc42.Driver | jdbc:redshift://localhost:5439/testdb | com.amazon.redshift.xa.RedshiftXADataSource | https://mvnrepository.com/artifact/com.amazon.redshift/redshift-jdbc42 |
Expand Down
1 change: 1 addition & 0 deletions docs/en/connector-v2/source/Jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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 |
| saphana | com.sap.db.jdbc.Driver | jdbc:sap://localhost:39015 | https://mvnrepository.com/artifact/com.sap.cloud.db.jdbc/ngdbc |
| doris | com.mysql.cj.jdbc.Driver | jdbc:mysql://localhost:3306/test | https://mvnrepository.com/artifact/mysql/mysql-connector-java |
| teradata | com.teradata.jdbc.TeraDriver | jdbc:teradata://localhost/DBS_PORT=1025,DATABASE=test | https://mvnrepository.com/artifact/com.teradata.jdbc/terajdbc |
| Redshift | com.amazon.redshift.jdbc42.Driver | jdbc:redshift://localhost:5439/testdb | https://mvnrepository.com/artifact/com.amazon.redshift/redshift-jdbc42 |
Expand Down
16 changes: 14 additions & 2 deletions seatunnel-connectors-v2/connector-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<tablestore.version>5.13.9</tablestore.version>
<teradata.version>17.20.00.12</teradata.version>
<redshift.version>2.1.0.9</redshift.version>
<saphana.version>2.14.7</saphana.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -114,6 +115,13 @@
<version>${redshift.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sap.cloud.db.jdbc/ngdbc -->
<dependency>
<groupId>com.sap.cloud.db.jdbc</groupId>
<artifactId>ngdbc</artifactId>
<version>${saphana.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -171,6 +179,10 @@
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc42</artifactId>
</dependency>
</dependencies>

</project>
<dependency>
<groupId>com.sap.cloud.db.jdbc</groupId>
<artifactId>ngdbc</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.saphana;

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 SapHanaDialect implements JdbcDialect {
@Override
public String dialectName() {
return "SapHana";
}

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

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

@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,41 @@
/*
* 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.saphana;

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;

/**
* Dialect Factory of {@link SapHanaDialect}
*/

@AutoService(JdbcDialectFactory.class)
public class SapHanaDialectFactory implements JdbcDialectFactory {
@Override
public boolean acceptsURL(String url) {
return url.startsWith("jdbc:sap://");
}

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

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

public class SapHanaJdbcRowConverter extends AbstractJdbcRowConverter {
@Override
public String converterName() {
return "SapHana";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.saphana;

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;
import java.util.Locale;

public class SapHanaTypeMapper implements JdbcDialectTypeMapper {

// refer to https://help.sap.com/docs/SAP_BUSINESSOBJECTS_BUSINESS_INTELLIGENCE_PLATFORM/aa4cb9ab429349e49678e146f05d7341/ec3313286fdb101497906a7cb0e91070.html?locale=zh-CN
private static final String SAP_HANA_BLOB = "blob";
private static final String SAP_HANA_VARBINARY = "varbinary";
private static final String SAP_HANA_DATE = "date";
private static final String SAP_HANA_TIME = "time";
private static final String SAP_HANA_LONGDATE = "longtime";
private static final String SAP_HANA_SECONDDATE = "seconddate";
private static final String SAP_HANA_TIMESTAMP = "timestamp";
private static final String SAP_HANA_DECIMAL = "decimal";
private static final String SAP_HANA_REAL = "real";
private static final String SAP_HANA_SMALLDECIMAL = "smalldecimal";
private static final String SAP_HANA_BIGINT = "bigint";
private static final String SAP_HANA_INTEGER = "integer";
private static final String SAP_HANA_SMALLINT = "smallint";
private static final String SAP_HANA_TINYINT = "tinyint";
private static final String SAP_HANA_DOUBLE = "double";
private static final String SAP_HANA_CLOB = "clob";
private static final String SAP_HANA_NCLOB = "nclob";
private static final String SAP_HANA_TEXT = "text";
private static final String SAP_HANA_ALPHANUM = "alphanum";
private static final String SAP_HANA_NVARCHAR = "nvarchar";
private static final String SAP_HANA_SHORTTEXT = "shorttext";
private static final String SAP_HANA_VARCHAR = "varchar";
private static final String SAP_HANA_BINARY = "binary";

@Override
public SeaTunnelDataType<?> mapping(ResultSetMetaData metadata, int colIndex) throws SQLException {
String typeName = metadata.getColumnTypeName(colIndex).toLowerCase(Locale.ROOT);

int precision = metadata.getPrecision(colIndex);
int scale = metadata.getScale(colIndex);

switch (typeName) {
case SAP_HANA_BLOB:
case SAP_HANA_VARBINARY:
case SAP_HANA_BINARY:
return PrimitiveByteArrayType.INSTANCE;
case SAP_HANA_DATE:
return LocalTimeType.LOCAL_DATE_TYPE;
case SAP_HANA_TIME:
return LocalTimeType.LOCAL_TIME_TYPE;
case SAP_HANA_TIMESTAMP:
case SAP_HANA_LONGDATE:
case SAP_HANA_SECONDDATE:
return LocalTimeType.LOCAL_DATE_TIME_TYPE;
case SAP_HANA_DECIMAL:
return new DecimalType(precision, scale);
case SAP_HANA_REAL:
case SAP_HANA_SMALLDECIMAL:
return BasicType.FLOAT_TYPE;
case SAP_HANA_BIGINT:
return BasicType.LONG_TYPE;
case SAP_HANA_INTEGER:
return BasicType.INT_TYPE;
case SAP_HANA_SMALLINT:
return BasicType.SHORT_TYPE;
case SAP_HANA_TINYINT:
return BasicType.BYTE_TYPE;
case SAP_HANA_DOUBLE:
return BasicType.DOUBLE_TYPE;
case SAP_HANA_CLOB:
case SAP_HANA_NCLOB:
case SAP_HANA_TEXT:
case SAP_HANA_ALPHANUM:
case SAP_HANA_NVARCHAR:
case SAP_HANA_SHORTTEXT:
case SAP_HANA_VARCHAR:
return BasicType.STRING_TYPE;
default:
final String jdbcColumnName = metadata.getColumnName(colIndex);
throw new UnsupportedOperationException(
String.format(
"Doesn't support SapHana type '%s' on column '%s' yet.",
typeName, jdbcColumnName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<artifactId>terajdbc4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sap.cloud.db.jdbc</groupId>
<artifactId>ngdbc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.lifecycle.Startables;
import org.testcontainers.utility.DockerLoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -83,7 +84,7 @@ private void getContainer() throws SQLException, MalformedURLException, ClassNot
.withNetwork(NETWORK)
.withNetworkAliases(jdbcCase.getNetworkAliases())
.withEnv(jdbcCase.getContainerEnv())
.withLogConsumer(new Slf4jLogConsumer(log));
.withLogConsumer(new Slf4jLogConsumer(DockerLoggerFactory.getLogger(jdbcCase.getDockerImage())));
dbServer.setPortBindings(Lists.newArrayList(
String.format("%s:%s", jdbcCase.getLocalPort(), jdbcCase.getPort())));
Startables.deepStart(Stream.of(dbServer)).join();
Expand Down
Loading

0 comments on commit 7ecc19e

Please sign in to comment.