Skip to content

Commit

Permalink
Fixes the connection of Remote HiveServer2 through HiveServer2 JDBC D…
Browse files Browse the repository at this point in the history
…river (#31573)
  • Loading branch information
linghengqian authored Jun 4, 2024
1 parent 9ce019f commit 33f65cf
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ ShardingSphere 默认情况下仅包含核心 SPI 的实现,在 Git Source 存
- `org.apache.shardingsphere:shardingsphere-sql-parser-oracle`, SQL 解析的 Oracle 方言解析实现
- `org.apache.shardingsphere:shardingsphere-sql-parser-sqlserver`, SQL 解析的 SQL Server 方言实现
- `org.apache.shardingsphere:shardingsphere-sql-parser-doris`, SQL 解析的 Doris 方言实现
- `org.apache.shardingsphere:shardingsphere-sql-parser-hive`, SQL 解析的 Hive 方言实现
- `org.apache.shardingsphere:shardingsphere-sql-parser-presto`, SQL 解析的 Presto 方言实现
- `org.apache.shardingsphere:shardingsphere-sql-parser-sql92`, SQL 解析的 SQL 92 方言实现
- `org.apache.shardingsphere:shardingsphere-standalone-mode-core`,单机模式配置信息持久化定义核心
Expand All @@ -53,6 +52,7 @@ ShardingSphere 默认情况下仅包含核心 SPI 的实现,在 Git Source 存
- 数据库类型识别
- `org.apache.shardingsphere:shardingsphere-infra-database-testcontainers`, 对 `testcontainers-java``JDBC support` 的 jdbcURL 的识别适配
- SQL 解析
- `org.apache.shardingsphere:shardingsphere-sql-parser-clickhouse`, SQL 解析的 ClickHouse 方言实现
- `org.apache.shardingsphere:shardingsphere-parser-sql-clickhouse`, SQL 解析的 ClickHouse 方言实现
- `org.apache.shardingsphere:shardingsphere-parser-sql-hive`, SQL 解析的 Hive 方言实现

除了以上可选插件外,ShardingSphere 社区开发者还贡献了大量的插件实现,可以在 [ShardingSphere Plugin](https://github.com/apache/shardingsphere-plugin) 仓库中查看插件的使用说明,ShardingSphere Plugin 仓库中的插件会和 ShardingSphere 保持相同的发布节奏,可以在 https://central.sonatype.com/ 进行检索,并安装到 ShardingSphere 中。
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ All the built-in plugins for ShardingSphere-JDBC are listed below in the form of
- `org.apache.shardingsphere:shardingsphere-sql-parser-oracle`, Oracle dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-sql-parser-sqlserver`, SQL Server dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-sql-parser-doris`, Doris dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-sql-parser-hive`, Hive dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-sql-parser-presto`, Presto dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-sql-parser-sql92`,the SQL 92 dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-standalone-mode-core`, the persistence definition core of single-machine mode configuration information
Expand All @@ -53,6 +52,7 @@ All optional plugins are listed below in the form of `groupId:artifactId`.
- Database type identification
- `org.apache.shardingsphere:shardingsphere-infra-database-testcontainers`, Adaptation of jdbcURL for `JDBC support` of `testcontainers-java`
- SQL parsing
- `org.apache.shardingsphere:shardingsphere-sql-parser-clickhouse`, ClickHouse dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-parser-sql-clickhouse`, ClickHouse dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-parser-sql-hive`, Hive dialect implementation of SQL parsing

In addition to the above optional plugins, ShardingSphere community developers have contributed a number of plugin implementations. These plugins can be found in [ShardingSphere Plugins] (https://github.com/apache/shardingsphere-plugin) repository. Plugins in ShardingSphere Plugin repository would remain the same release plan with ShardingSphere, they can be retrieved at https://central.sonatype.com/, and install into ShardingSphere.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ client {
}
```

根据实际场景修改 Seata 的 `file.conf``registry.conf` 文件。
根据实际场景修改 Seata 的 `registry.conf` 文件。

## 使用限制

Expand Down Expand Up @@ -180,7 +180,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class ExampleApplication {

public static void main(String[] args) {
SpringApplication.run(ShardingsphereSeataSpringBootTestApplication.class, args);
SpringApplication.run(ExampleApplication.class, args);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ client {
}
```

Modify the `file.conf` and `registry.conf` files of Seata as required.
Modify the `registry.conf` file of Seata as required.

## Usage restrictions

Expand Down Expand Up @@ -184,7 +184,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class ExampleApplication {

public static void main(String[] args) {
SpringApplication.run(ShardingsphereSeataSpringBootTestApplication.class, args);
SpringApplication.run(ExampleApplication.class, args);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ ShardingSphere 默认情况下仅包含核心 SPI 的实现,在 Git Source 存
- `org.apache.shardingsphere:shardingsphere-infra-expr-espresso`,基于 GraalVM Truffle 的 Espresso 实现的使用 Groovy 语法的行表达式
- 数据库类型识别
- `org.apache.shardingsphere:shardingsphere-infra-database-testcontainers`, 对 `testcontainers-java``JDBC support` 的 jdbcURL 的识别适配
- SQL 解析
- `org.apache.shardingsphere:shardingsphere-parser-sql-clickhouse`, SQL 解析的 ClickHouse 方言实现
- `org.apache.shardingsphere:shardingsphere-parser-sql-hive`, SQL 解析的 Hive 方言实现

除了以上可选插件外,ShardingSphere 社区开发者还贡献了大量的插件实现,可以在 [ShardingSphere Plugin](https://github.com/apache/shardingsphere-plugin) 仓库中查看插件的使用说明,ShardingSphere Plugin 仓库中的插件会和 ShardingSphere 保持相同的发布节奏,可以在 https://central.sonatype.com/ 进行检索,并安装到 ShardingSphere 中。
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ All optional plugins are listed below in the form of `groupId:artifactId`.
- `org.apache.shardingsphere:shardingsphere-infra-expr-espresso`,Row Value Expressions that uses the Groovy syntax based on GraalVM Truffle's Espresso implementation
- Database type identification
- `org.apache.shardingsphere:shardingsphere-infra-database-testcontainers`, Adaptation of jdbcURL for `JDBC support` of `testcontainers-java`
- SQL parsing
- `org.apache.shardingsphere:shardingsphere-parser-sql-clickhouse`, ClickHouse dialect implementation of SQL parsing
- `org.apache.shardingsphere:shardingsphere-parser-sql-hive`, Hive dialect implementation of SQL parsing

In addition to the above optional plugins, ShardingSphere community developers have contributed a number of plugin implementations. These plugins can be found in [ShardingSphere Plugins](https://github.com/apache/shardingsphere-plugin) repository. Plugins in ShardingSphere Plugin repository would remain the same release plan with ShardingSphere, they can be retrieved at https://central.sonatype.com/, and install into ShardingSphere.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -47,10 +49,10 @@ public final class DatabaseTypeEngine {

/**
* Get protocol type.
*
* @param databaseName database name
*
* @param databaseName database name
* @param databaseConfig database configuration
* @param props configuration properties
* @param props configuration properties
* @return protocol type
*/
public static DatabaseType getProtocolType(final String databaseName, final DatabaseConfiguration databaseConfig, final ConfigurationProperties props) {
Expand All @@ -66,7 +68,7 @@ public static DatabaseType getProtocolType(final String databaseName, final Data
* Get protocol type.
*
* @param databaseConfigs database configurations
* @param props configuration properties
* @param props configuration properties
* @return protocol type
*/
public static DatabaseType getProtocolType(final Map<String, ? extends DatabaseConfiguration> databaseConfigs, final ConfigurationProperties props) {
Expand Down Expand Up @@ -94,7 +96,7 @@ private static Map<String, DataSource> getEnabledDataSources(final Map<String, ?
/**
* Get storage types.
*
* @param databaseName database name
* @param databaseName database name
* @param databaseConfig database configuration
* @return storage types
*/
Expand All @@ -109,6 +111,8 @@ public static Map<String, DatabaseType> getStorageTypes(final String databaseNam

/**
* Get storage type.
* Similar to apache/hive 4.0.0's `org.apache.hive.jdbc.HiveDatabaseMetaData`, it does not implement {@link java.sql.DatabaseMetaData#getURL()}.
* So use {@link CatalogSwitchableDataSource#getUrl()} to try fuzzy matching.
*
* @param dataSource data source
* @return storage type
Expand All @@ -117,6 +121,11 @@ public static Map<String, DatabaseType> getStorageTypes(final String databaseNam
public static DatabaseType getStorageType(final DataSource dataSource) {
try (Connection connection = dataSource.getConnection()) {
return DatabaseTypeFactory.get(connection.getMetaData().getURL());
} catch (final SQLFeatureNotSupportedException sqlFeatureNotSupportedException) {
if (dataSource instanceof CatalogSwitchableDataSource) {
return DatabaseTypeFactory.get(((CatalogSwitchableDataSource) dataSource).getUrl());
}
throw new SQLWrapperException(sqlFeatureNotSupportedException);
} catch (final SQLException ex) {
throw new SQLWrapperException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.shardingsphere.infra.database.clickhouse.connector;

import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser;
import org.apache.shardingsphere.infra.database.core.exception.UnrecognizedDatabaseURLException;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.util.Properties;
import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ClickHouseConnectionPropertiesTest {

private final ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, TypedSPILoader.getService(DatabaseType.class, "ClickHouse"));

@ParameterizedTest(name = "{0}")
@ArgumentsSource(NewConstructorTestCaseArgumentsProvider.class)
void assertNewConstructor(final String name, final String url, final String hostname, final int port, final String catalog, final String schema, final Properties queryProps) {
ConnectionProperties actual = parser.parse(url, null, null);
assertThat(actual.getHostname(), is(hostname));
assertThat(actual.getPort(), is(port));
assertThat(actual.getCatalog(), is(catalog));
assertThat(actual.getSchema(), is(schema));
assertThat(actual.getQueryProperties(), is(queryProps));
}

@Test
void assertNewConstructorFailure() {
assertThrows(UnrecognizedDatabaseURLException.class, () -> parser.parse("jdbc:ch:xxxxxxxx", null, null));
}

private static class NewConstructorTestCaseArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return Stream.of(
Arguments.of("simple", "jdbc:ch://127.0.0.1/foo_ds", "127.0.0.1", 8123, "foo_ds", null, new Properties()),
Arguments.of("complex", "jdbc:clickhouse:http://127.0.0.1:9999/foo_ds?continueBatchOnError=true", "127.0.0.1", 9999, "foo_ds", null,
PropertiesBuilder.build(new PropertiesBuilder.Property("continueBatchOnError", "true"))));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.shardingsphere.infra.database.clickhouse.metadata.database;

import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ClickHouseDatabaseMetaDataTest {

private final DialectDatabaseMetaData dialectDatabaseMetaData = DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class, TypedSPILoader.getService(DatabaseType.class, "ClickHouse"));

@Test
void assertGetQuoteCharacter() {
assertThat(dialectDatabaseMetaData.getQuoteCharacter(), is(QuoteCharacter.QUOTE));
}

@Test
void assertGetDefaultNullsOrderType() {
assertThat(dialectDatabaseMetaData.getDefaultNullsOrderType(), is(NullsOrderType.FIRST));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.shardingsphere.infra.database.hive.connector;

import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser;
import org.apache.shardingsphere.infra.database.core.connector.StandardConnectionProperties;
import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl;
import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser;

import java.util.Properties;

/**
* Connection properties parser of Hive.
*/
public final class HiveConnectionPropertiesParser implements ConnectionPropertiesParser {

private static final int DEFAULT_PORT = 10000;

private static final String DEFAULT_HOSTNAME = "localhost";

@Override
public ConnectionProperties parse(final String url, final String username, final String catalog) {
JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url);
return jdbcUrl.getHostname().isEmpty()
? new StandardConnectionProperties(DEFAULT_HOSTNAME, jdbcUrl.getPort(DEFAULT_PORT), jdbcUrl.getDatabase(), null, jdbcUrl.getQueryProperties(), new Properties())
: new StandardConnectionProperties(jdbcUrl.getHostname(), jdbcUrl.getPort(DEFAULT_PORT), jdbcUrl.getDatabase(), null, jdbcUrl.getQueryProperties(), new Properties());
}

@Override
public String getDatabaseType() {
return "Hive";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.shardingsphere.infra.database.hive.metadata.database;

import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;

/**
* Database metadata of Hive.
*/
public final class HiveDatabaseMetaData implements DialectDatabaseMetaData {

@Override
public QuoteCharacter getQuoteCharacter() {
return QuoteCharacter.QUOTE;
}

@Override
public NullsOrderType getDefaultNullsOrderType() {
return NullsOrderType.FIRST;
}

@Override
public String getDatabaseType() {
return "Hive";
}
}
Loading

0 comments on commit 33f65cf

Please sign in to comment.