Skip to content

Commit

Permalink
Hive: Introduce HiveMetastoreExtension for Hive tests (#9282)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduard Tudenhoefner <etudenhoefner@gmail.com>
  • Loading branch information
nk1506 and nastra authored Dec 12, 2023
1 parent 0c5b87a commit 09b44bb
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.apache.iceberg.AppendFiles;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.CatalogUtil;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DataFiles;
import org.apache.iceberg.PartitionSpec;
Expand All @@ -41,14 +44,17 @@
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.types.Types;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class HiveCreateReplaceTableTest extends HiveMetastoreTest {
public class HiveCreateReplaceTableTest {

private static final String DB_NAME = "hivedb";
private static final String TABLE_NAME = "tbl";
private static final TableIdentifier TABLE_IDENTIFIER = TableIdentifier.of(DB_NAME, TABLE_NAME);
private static final Schema SCHEMA =
Expand All @@ -60,8 +66,27 @@ public class HiveCreateReplaceTableTest extends HiveMetastoreTest {

private String tableLocation;

@RegisterExtension
private static final HiveMetastoreExtension HIVE_METASTORE_EXTENSION =
new HiveMetastoreExtension(DB_NAME, Collections.emptyMap());

private static HiveCatalog catalog;

@BeforeAll
public static void initCatalog() {
catalog =
(HiveCatalog)
CatalogUtil.loadCatalog(
HiveCatalog.class.getName(),
CatalogUtil.ICEBERG_CATALOG_TYPE_HIVE,
ImmutableMap.of(
CatalogProperties.CLIENT_POOL_CACHE_EVICTION_INTERVAL_MS,
String.valueOf(TimeUnit.SECONDS.toMillis(10))),
HIVE_METASTORE_EXTENSION.hiveConf());
}

@BeforeEach
public void createTableLocation() throws IOException {
public void createTableLocation() {
tableLocation = temp.resolve("hive-").toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.iceberg.hive;

import java.util.Map;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class HiveMetastoreExtension implements BeforeAllCallback, AfterAllCallback {
private HiveMetaStoreClient metastoreClient;
private TestHiveMetastore metastore;
private final Map<String, String> hiveConfOverride;
private final String databaseName;

public HiveMetastoreExtension(String databaseName, Map<String, String> hiveConfOverride) {
this.databaseName = databaseName;
this.hiveConfOverride = hiveConfOverride;
}

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
metastore = new TestHiveMetastore();
HiveConf hiveConfWithOverrides = new HiveConf(TestHiveMetastore.class);
if (hiveConfOverride != null) {
for (Map.Entry<String, String> kv : hiveConfOverride.entrySet()) {
hiveConfWithOverrides.set(kv.getKey(), kv.getValue());
}
}

metastore.start(hiveConfWithOverrides);
metastoreClient = new HiveMetaStoreClient(hiveConfWithOverrides);
String dbPath = metastore.getDatabasePath(databaseName);
Database db = new Database(databaseName, "description", dbPath, Maps.newHashMap());
metastoreClient.createDatabase(db);
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
if (null != metastoreClient) {
metastoreClient.close();
}

if (null != metastore) {
metastore.stop();
}

metastoreClient = null;
metastore = null;
}

public HiveMetaStoreClient metastoreClient() {
return metastoreClient;
}

public HiveConf hiveConf() {
return metastore.hiveConf();
}

public TestHiveMetastore metastore() {
return metastore;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,36 @@
import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.hadoop.fs.Path;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.CatalogUtil;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.TableMetadataParser;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.types.Types;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;

public class HiveTableBaseTest extends HiveMetastoreTest {
public class HiveTableBaseTest {

static final String TABLE_NAME = "tbl";
static final String DB_NAME = "hivedb";
static final TableIdentifier TABLE_IDENTIFIER = TableIdentifier.of(DB_NAME, TABLE_NAME);

@RegisterExtension
protected static final HiveMetastoreExtension HIVE_METASTORE_EXTENSION =
new HiveMetastoreExtension(DB_NAME, Collections.emptyMap());

protected static HiveCatalog catalog;

static final Schema schema =
new Schema(Types.StructType.of(required(1, "id", Types.LongType.get())).fields());

Expand All @@ -56,6 +70,19 @@ public class HiveTableBaseTest extends HiveMetastoreTest {

private Path tableLocation;

@BeforeAll
public static void initCatalog() {
catalog =
(HiveCatalog)
CatalogUtil.loadCatalog(
HiveCatalog.class.getName(),
CatalogUtil.ICEBERG_CATALOG_TYPE_HIVE,
ImmutableMap.of(
CatalogProperties.CLIENT_POOL_CACHE_EVICTION_INTERVAL_MS,
String.valueOf(TimeUnit.SECONDS.toMillis(10))),
HIVE_METASTORE_EXTENSION.hiveConf());
}

@BeforeEach
public void createTestTable() {
this.tableLocation =
Expand All @@ -65,12 +92,12 @@ public void createTestTable() {
@AfterEach
public void dropTestTable() throws Exception {
// drop the table data
tableLocation.getFileSystem(hiveConf).delete(tableLocation, true);
tableLocation.getFileSystem(HIVE_METASTORE_EXTENSION.hiveConf()).delete(tableLocation, true);
catalog.dropTable(TABLE_IDENTIFIER, false /* metadata only, location was already deleted */);
}

private static String getTableBasePath(String tableName) {
String databasePath = metastore.getDatabasePath(DB_NAME);
String databasePath = HIVE_METASTORE_EXTENSION.metastore().getDatabasePath(DB_NAME);
return Paths.get(databasePath, tableName).toAbsolutePath().toString();
}

Expand Down
Loading

0 comments on commit 09b44bb

Please sign in to comment.