|
| 1 | +/* |
| 2 | + * Copyright 2024 OceanBase. |
| 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 com.oceanbase.spark |
| 18 | + |
| 19 | +import com.oceanbase.spark.OceanBaseCatalogE2eITCase.{MYSQL_CONNECTOR_JAVA, SINK_CONNECTOR_NAME} |
| 20 | +import com.oceanbase.spark.OceanBaseTestBase.assertEqualsInAnyOrder |
| 21 | +import com.oceanbase.spark.utils.SparkContainerTestEnvironment |
| 22 | +import com.oceanbase.spark.utils.SparkContainerTestEnvironment.getResource |
| 23 | + |
| 24 | +import org.junit.jupiter.api._ |
| 25 | +import org.junit.jupiter.api.condition.DisabledIfSystemProperty |
| 26 | +import org.junit.jupiter.api.function.ThrowingSupplier |
| 27 | +import org.slf4j.LoggerFactory |
| 28 | +import org.testcontainers.containers.output.Slf4jLogConsumer |
| 29 | + |
| 30 | +import java.util |
| 31 | + |
| 32 | +class OceanBaseCatalogE2eITCase extends SparkContainerTestEnvironment { |
| 33 | + @BeforeEach |
| 34 | + @throws[Exception] |
| 35 | + override def before(): Unit = { |
| 36 | + super.before() |
| 37 | + initialize("sql/mysql/products.sql") |
| 38 | + } |
| 39 | + |
| 40 | + @AfterEach |
| 41 | + @throws[Exception] |
| 42 | + override def after(): Unit = { |
| 43 | + super.after() |
| 44 | + dropTables("products") |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisabledIfSystemProperty( |
| 49 | + named = "spark_version", |
| 50 | + matches = "^2\\.4\\.[0-9]$", |
| 51 | + disabledReason = "Catalog is only supported starting from Spark3." |
| 52 | + ) |
| 53 | + def testInsertValues(): Unit = { |
| 54 | + val sqlLines: util.List[String] = new util.ArrayList[String] |
| 55 | + sqlLines.add(s""" |
| 56 | + |set spark.sql.catalog.ob=com.oceanbase.spark.catalog.OceanBaseCatalog; |
| 57 | + |set spark.sql.catalog.ob.url=$getJdbcUrlInContainer; |
| 58 | + |set spark.sql.catalog.ob.username=$getUsername; |
| 59 | + |set spark.sql.catalog.ob.password=$getPassword; |
| 60 | + |set `spark.sql.catalog.ob.schema-name`=$getSchemaName; |
| 61 | + |set spark.sql.defaultCatalog=ob; |
| 62 | + |""".stripMargin) |
| 63 | + sqlLines.add( |
| 64 | + s""" |
| 65 | + |INSERT INTO $getSchemaName.products VALUES |
| 66 | + |(101, 'scooter', 'Small 2-wheel scooter', 3.14), |
| 67 | + |(102, 'car battery', '12V car battery', 8.1), |
| 68 | + |(103, '12-pack drill bits', '12-pack of drill bits with sizes ranging from #40 to #3', 0.8), |
| 69 | + |(104, 'hammer', '12oz carpenter\\'s hammer', 0.75), |
| 70 | + |(105, 'hammer', '14oz carpenter\\'s hammer', 0.875), |
| 71 | + |(106, 'hammer', '16oz carpenter\\'s hammer', 1.0), |
| 72 | + |(107, 'rocks', 'box of assorted rocks', 5.3), |
| 73 | + |(108, 'jacket', 'water resistent black wind breaker', 0.1), |
| 74 | + |(109, 'spare tire', '24 inch spare tire', 22.2); |
| 75 | + |""".stripMargin) |
| 76 | + |
| 77 | + sqlLines.add("select * from products limit 10;") |
| 78 | + |
| 79 | + submitSQLJob(sqlLines, getResource(SINK_CONNECTOR_NAME), getResource(MYSQL_CONNECTOR_JAVA)) |
| 80 | + |
| 81 | + val expected: util.List[String] = util.Arrays.asList( |
| 82 | + "101,scooter,Small 2-wheel scooter,3.1400000000", |
| 83 | + "102,car battery,12V car battery,8.1000000000", |
| 84 | + "103,12-pack drill bits,12-pack of drill bits with sizes ranging from #40 to #3,0.8000000000", |
| 85 | + "104,hammer,12oz carpenter's hammer,0.7500000000", |
| 86 | + "105,hammer,14oz carpenter's hammer,0.8750000000", |
| 87 | + "106,hammer,16oz carpenter's hammer,1.0000000000", |
| 88 | + "107,rocks,box of assorted rocks,5.3000000000", |
| 89 | + "108,jacket,water resistent black wind breaker,0.1000000000", |
| 90 | + "109,spare tire,24 inch spare tire,22.2000000000" |
| 91 | + ) |
| 92 | + val actual: util.List[String] = queryTable("products") |
| 93 | + assertEqualsInAnyOrder(expected, actual) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @DisabledIfSystemProperty( |
| 98 | + named = "spark_version", |
| 99 | + matches = "^2\\.4\\.[0-9]$", |
| 100 | + disabledReason = "Catalog is only supported starting from Spark3." |
| 101 | + ) |
| 102 | + def testCatalogOp(): Unit = { |
| 103 | + val sqlLines: util.List[String] = new util.ArrayList[String] |
| 104 | + sqlLines.add(s""" |
| 105 | + |set spark.sql.catalog.ob=com.oceanbase.spark.catalog.OceanBaseCatalog; |
| 106 | + |set spark.sql.catalog.ob.url=$getJdbcUrlInContainer; |
| 107 | + |set spark.sql.catalog.ob.username=$getUsername; |
| 108 | + |set spark.sql.catalog.ob.password=$getPassword; |
| 109 | + |set `spark.sql.catalog.ob.schema-name`=$getSchemaName; |
| 110 | + |set spark.sql.defaultCatalog=ob; |
| 111 | + |""".stripMargin) |
| 112 | + |
| 113 | + sqlLines.add(s""" |
| 114 | + |show databases; |
| 115 | + |show tables; |
| 116 | + |use test; |
| 117 | + |select * from products limit 10; |
| 118 | + |""".stripMargin) |
| 119 | + |
| 120 | + Assertions.assertDoesNotThrow(new ThrowingSupplier[Unit] { |
| 121 | + override def get(): Unit = { |
| 122 | + submitSQLJob(sqlLines, getResource(SINK_CONNECTOR_NAME), getResource(MYSQL_CONNECTOR_JAVA)) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +object OceanBaseCatalogE2eITCase extends SparkContainerTestEnvironment { |
| 129 | + private val LOG = LoggerFactory.getLogger(classOf[OceanBaseE2eITCase]) |
| 130 | + private val SINK_CONNECTOR_NAME = |
| 131 | + "^.*spark-connector-oceanbase-\\d+\\.\\d+_\\d+\\.\\d+-[\\d\\.]+(?:-SNAPSHOT)?\\.jar$" |
| 132 | + private val MYSQL_CONNECTOR_JAVA = "mysql-connector-java.jar" |
| 133 | + |
| 134 | + @BeforeAll def setup(): Unit = { |
| 135 | + OceanBaseMySQLTestBase.CONTAINER.withLogConsumer(new Slf4jLogConsumer(LOG)).start() |
| 136 | + } |
| 137 | + |
| 138 | + @AfterAll def tearDown(): Unit = { |
| 139 | + OceanBaseMySQLTestBase.CONTAINER.stop() |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments