|
| 1 | +/* |
| 2 | + * Copyright 2024 Qiheng He |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.github.linghengqian.hive.server2.jdbc.driver.thin; |
| 18 | + |
| 19 | +import com.zaxxer.hikari.HikariConfig; |
| 20 | +import com.zaxxer.hikari.HikariDataSource; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.testcontainers.containers.GenericContainer; |
| 24 | +import org.testcontainers.containers.Network; |
| 25 | +import org.testcontainers.junit.jupiter.Container; |
| 26 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 27 | +import org.testcontainers.utility.DockerImageName; |
| 28 | + |
| 29 | +import java.sql.Connection; |
| 30 | +import java.sql.ResultSet; |
| 31 | +import java.sql.SQLException; |
| 32 | +import java.sql.Statement; |
| 33 | +import java.time.Duration; |
| 34 | +import java.time.temporal.ChronoUnit; |
| 35 | + |
| 36 | +import static org.awaitility.Awaitility.await; |
| 37 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 38 | +import static org.hamcrest.Matchers.is; |
| 39 | + |
| 40 | +@SuppressWarnings({"SqlNoDataSourceInspection", "resource"}) |
| 41 | +@Testcontainers |
| 42 | +public class StandaloneMetastoreTest { |
| 43 | + |
| 44 | + private static final Network NETWORK = Network.newNetwork(); |
| 45 | + |
| 46 | + @Container |
| 47 | + public static final GenericContainer<?> HMS_CONTAINER = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.1")) |
| 48 | + .withEnv("SERVICE_NAME", "metastore") |
| 49 | + .withNetwork(NETWORK) |
| 50 | + .withNetworkAliases("metastore") |
| 51 | + .withExposedPorts(9083); |
| 52 | + |
| 53 | + @Container |
| 54 | + public static final GenericContainer<?> HS2_CONTAINER = new GenericContainer<>(DockerImageName.parse("apache/hive:4.0.1")) |
| 55 | + .withEnv("SERVICE_NAME", "hiveserver2") |
| 56 | + .withEnv("SERVICE_OPTS", "-Dhive.metastore.uris=thrift://metastore:9083") |
| 57 | + .withNetwork(NETWORK) |
| 58 | + .withExposedPorts(10000) |
| 59 | + .dependsOn(HMS_CONTAINER); |
| 60 | + |
| 61 | + @AfterAll |
| 62 | + static void afterAll() { |
| 63 | + NETWORK.close(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void test() throws SQLException { |
| 68 | + HikariConfig config = new HikariConfig(); |
| 69 | + String jdbcUrlPrefix = "jdbc:hive2://" + HS2_CONTAINER.getHost() + ":" + HS2_CONTAINER.getMappedPort(10000); |
| 70 | + config.setJdbcUrl(jdbcUrlPrefix + "/"); |
| 71 | + config.setDriverClassName("org.apache.hive.jdbc.HiveDriver"); |
| 72 | + await().atMost(Duration.of(30L, ChronoUnit.SECONDS)).until(() -> { |
| 73 | + try (HikariDataSource hikariDataSource = new HikariDataSource(config)) { |
| 74 | + hikariDataSource.getConnection().close(); |
| 75 | + } |
| 76 | + return true; |
| 77 | + }); |
| 78 | + try (HikariDataSource hikariDataSource = new HikariDataSource(config); |
| 79 | + Connection connection = hikariDataSource.getConnection(); |
| 80 | + Statement statement = connection.createStatement()) { |
| 81 | + statement.execute("CREATE DATABASE demo_ds_0"); |
| 82 | + ResultSet firstResultSet = statement.executeQuery("show tables"); |
| 83 | + assertThat(firstResultSet.next(), is(false)); |
| 84 | + statement.execute("create table hive_example(a string, b int) partitioned by(c int)"); |
| 85 | + statement.execute("alter table hive_example add partition(c=1)"); |
| 86 | + statement.execute("insert into hive_example partition(c=1) values('a', 1), ('a', 2),('b',3)"); |
| 87 | + ResultSet secondResultSet = statement.executeQuery("select count(distinct a) from hive_example"); |
| 88 | + assertThat(secondResultSet.next(), is(true)); |
| 89 | + assertThat(secondResultSet.getInt("_c0"), is(2)); |
| 90 | + ResultSet thirdResultSet = statement.executeQuery("select sum(b) from hive_example"); |
| 91 | + assertThat(thirdResultSet.next(), is(true)); |
| 92 | + assertThat(thirdResultSet.getInt("_c0"), is(6)); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments