Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#584] improvement(hive): Support detailed Hive schema property metadata (#595)

mchades3a6b51e41e23
diff --git a/api/src/main/java/com/datastrato/gravitino/rel/SupportsSchemas.java b/api/src/main/java/com/datastrato/gravitino/rel/SupportsSchemas.java
index 7d1e1cbfdd..372a6e26e6 100644
--- a/api/src/main/java/com/datastrato/gravitino/rel/SupportsSchemas.java
+++ b/api/src/main/java/com/datastrato/gravitino/rel/SupportsSchemas.java
@@ -70,12 +70,12 @@ public interface SupportsSchemas {
*
* @param ident The name identifier of the schema.
* @param comment The comment of the schema.
- * @param metadata The metadata of the schema.
+ * @param properties The properties of the schema.
* @return The created schema.
* @throws NoSuchCatalogException If the catalog does not exist.
* @throws SchemaAlreadyExistsException If the schema already exists.
*/
- Schema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)
+ Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException;
/**
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
index 94355370db..366c166cd9 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveCatalogOperations.java
@@ -184,20 +184,21 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
*
* @param ident The identifier of the schema to create.
* @param comment The comment for the schema.
- * @param metadata The metadata properties for the schema.
+ * @param properties The metadata properties for the schema.
* @return The created {@link HiveSchema}.
* @throws NoSuchCatalogException If the provided namespace is invalid or does not exist.
* @throws SchemaAlreadyExistsException If a schema with the same name already exists.
*/
@Override
- public HiveSchema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)
+ public HiveSchema createSchema(
+ NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
try {
HiveSchema hiveSchema =
new HiveSchema.Builder()
.withName(ident.name())
.withComment(comment)
- .withProperties(metadata)
+ .withProperties(properties)
.withConf(hiveConf)
.withAuditInfo(
new AuditInfo.Builder()
@@ -275,19 +276,19 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
try {
// load the database parameters
Database database = clientPool.run(client -> client.getDatabase(ident.name()));
- Map<String, String> metadata = HiveSchema.convertToMetadata(database);
+ Map<String, String> properties = HiveSchema.buildSchemaProperties(database);
LOG.debug(
- "Loaded metadata for Hive schema (database) {} found {}",
+ "Loaded properties for Hive schema (database) {} found {}",
ident.name(),
- metadata.keySet());
+ properties.keySet());
for (SchemaChange change : changes) {
if (change instanceof SchemaChange.SetProperty) {
- metadata.put(
+ properties.put(
((SchemaChange.SetProperty) change).getProperty(),
((SchemaChange.SetProperty) change).getValue());
} else if (change instanceof SchemaChange.RemoveProperty) {
- metadata.remove(((SchemaChange.RemoveProperty) change).getProperty());
+ properties.remove(((SchemaChange.RemoveProperty) change).getProperty());
} else {
throw new IllegalArgumentException(
"Unsupported schema change type: " + change.getClass().getSimpleName());
@@ -296,7 +297,7 @@ public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas
// alter the hive database parameters
Database alteredDatabase = database.deepCopy();
- alteredDatabase.setParameters(metadata);
+ alteredDatabase.setParameters(properties);
clientPool.run(
client -> {
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchema.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchema.java
index 820908b7e5..c8d8f65250 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchema.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchema.java
@@ -4,25 +4,21 @@
*/
package com.datastrato.gravitino.catalog.hive;
+import static com.datastrato.gravitino.catalog.hive.HiveSchemaPropertiesMetadata.LOCATION;
+
import com.datastrato.gravitino.catalog.rel.BaseSchema;
import com.datastrato.gravitino.meta.AuditInfo;
-import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
-import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import lombok.ToString;
import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.PrincipalType;
/** Represents a Hive Schema (Database) entity in the Hive Metastore catalog. */
@ToString
public class HiveSchema extends BaseSchema {
- private static final String HMS_DB_OWNER = "hive.metastore.database.owner";
- private static final String HMS_DB_OWNER_TYPE = "hive.metastore.database.owner-type";
-
private Configuration conf;
private HiveSchema() {}
@@ -35,7 +31,7 @@ public class HiveSchema extends BaseSchema {
* @return A new HiveSchema instance.
*/
public static HiveSchema fromHiveDB(Database db, Configuration hiveConf) {
- Map<String, String> properties = convertToMetadata(db);
+ Map<String, String> properties = buildSchemaProperties(db);
// Get audit info from Hive's Database object. Because Hive's database doesn't store create
// time, last modifier and last modified time, we only get creator from Hive's database.
@@ -52,27 +48,15 @@ public class HiveSchema extends BaseSchema {
}
/**
- * Converts a Database to metadata represented as a map of key-value pairs.
+ * Build schema properties from a Hive Database object.
*
- * @param database The Database to be converted to metadata.
- * @return A map containing the metadata key-value pairs.
+ * @param database The Hive Database object.
+ * @return A map of schema properties.
*/
- public static Map<String, String> convertToMetadata(Database database) {
- Map<String, String> meta = Maps.newHashMap();
-
- meta.putAll(database.getParameters());
- meta.put("location", database.getLocationUri());
- if (database.getDescription() != null) {
- meta.put("comment", database.getDescription());
- }
- if (database.getOwnerName() != null) {
- meta.put(HMS_DB_OWNER, database.getOwnerName());
- if (database.getOwnerType() != null) {
- meta.put(HMS_DB_OWNER_TYPE, database.getOwnerType().name());
- }
- }
-
- return meta;
+ public static Map<String, String> buildSchemaProperties(Database database) {
+ Map<String, String> properties = Maps.newHashMap(database.getParameters());
+ properties.put(LOCATION, database.getLocationUri());
+ return properties;
}
/**
@@ -81,59 +65,21 @@ public class HiveSchema extends BaseSchema {
* @return The converted Database object.
*/
public Database toHiveDB() {
- Database innerDb = new Database();
-
- Map<String, String> parameter = Maps.newHashMap();
- innerDb.setName(name());
- innerDb.setLocationUri(databaseLocation(name()));
- if (comment() != null) {
- innerDb.setDescription(comment());
- }
+ Database hiveDb = new Database();
- Optional.ofNullable(properties)
- .orElse(Collections.emptyMap())
- .forEach(
- (key, value) -> {
- if (key.equals("location")) {
- innerDb.setLocationUri(value);
- } else if (key.equals(HMS_DB_OWNER)) {
- innerDb.setOwnerName(value);
- } else if (key.equals(HMS_DB_OWNER_TYPE) && value != null) {
- innerDb.setOwnerType(PrincipalType.valueOf(value));
- } else {
- if (value != null) {
- parameter.put(key, value);
- }
- }
- });
-
- if (innerDb.getOwnerName() == null) {
- innerDb.setOwnerName(auditInfo().creator());
- innerDb.setOwnerType(PrincipalType.USER);
- }
+ hiveDb.setName(name());
+ Optional.ofNullable(properties().get(LOCATION)).ifPresent(hiveDb::setLocationUri);
+ Optional.ofNullable(comment()).ifPresent(hiveDb::setDescription);
- innerDb.setParameters(parameter);
+ // TODO: Add more privilege info to Hive's Database object after Gravitino supports privilege.
+ hiveDb.setOwnerName(auditInfo().creator());
+ hiveDb.setOwnerType(PrincipalType.USER);
- return innerDb;
- }
+ Map<String, String> parameters = Maps.newHashMap(properties());
+ parameters.remove(LOCATION);
+ hiveDb.setParameters(parameters);
- private String databaseLocation(String databaseName) {
- String warehouseLocation = conf.get(HiveConf.ConfVars.METASTOREWAREHOUSE.varname);
- Preconditions.checkNotNull(
- warehouseLocation, "Warehouse location is not set: hive.metastore.warehouse.dir=null");
- warehouseLocation = stripTrailingSlash(warehouseLocation);
- return String.format("%s/%s.db", warehouseLocation, databaseName);
- }
-
- private static String stripTrailingSlash(String path) {
- Preconditions.checkArgument(
- path != null && path.length() > 0, "path must not be null or empty");
-
- String result = path;
- while (result.endsWith("/")) {
- result = result.substring(0, result.length() - 1);
- }
- return result;
+ return hiveDb;
}
/** A builder class for constructing HiveSchema instances. */
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchemaPropertiesMetadata.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchemaPropertiesMetadata.java
index 346e153198..8a924ffc7f 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchemaPropertiesMetadata.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveSchemaPropertiesMetadata.java
@@ -4,17 +4,30 @@
*/
package com.datastrato.gravitino.catalog.hive;
+import static com.datastrato.gravitino.catalog.PropertyEntry.stringOptionalPropertyEntry;
+
import com.datastrato.gravitino.catalog.BasePropertiesMetadata;
import com.datastrato.gravitino.catalog.PropertyEntry;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
+import java.util.List;
import java.util.Map;
public class HiveSchemaPropertiesMetadata extends BasePropertiesMetadata {
- private static final Map<String, PropertyEntry<?>> propertiesMetadata = Maps.newHashMap();
+ public static final String LOCATION = "location";
+ private static final Map<String, PropertyEntry<?>> propertiesMetadata;
+
+ static {
+ List<PropertyEntry<?>> propertyEntries =
+ ImmutableList.of(
+ stringOptionalPropertyEntry(
+ LOCATION, "The location for Hive database storage", false, null, false));
+
+ propertiesMetadata = Maps.uniqueIndex(propertyEntries, PropertyEntry::getName);
+ }
@Override
protected Map<String, PropertyEntry<?>> specificPropertyEntries() {
- // TODO(minghuang): support Hive schema properties metadata
return propertiesMetadata;
}
}
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
index 791e20380a..081186a36c 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/com/datastrato/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
@@ -141,14 +141,14 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
*
* @param ident The identifier of the schema to create.
* @param comment The comment for the schema.
- * @param metadata The metadata properties for the schema.
+ * @param properties The properties for the schema.
* @return The created {@link IcebergSchema}.
* @throws NoSuchCatalogException If the provided namespace is invalid or does not exist.
* @throws SchemaAlreadyExistsException If a schema with the same name already exists.
*/
@Override
public IcebergSchema createSchema(
- NameIdentifier ident, String comment, Map<String, String> metadata)
+ NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
try {
String currentUser = currentUser();
@@ -156,7 +156,7 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
new IcebergSchema.Builder()
.withName(ident.name())
.withComment(comment)
- .withProperties(metadata)
+ .withProperties(properties)
.withAuditInfo(
new AuditInfo.Builder()
.withCreator(currentUser)
@@ -170,7 +170,7 @@ public class IcebergCatalogOperations implements CatalogOperations, SupportsSche
ident.name(),
currentUser,
comment,
- metadata);
+ properties);
return createdSchema;
} catch (org.apache.iceberg.exceptions.AlreadyExistsException e) {
throw new SchemaAlreadyExistsException(
diff --git a/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java b/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java
index 5ab8350131..bd14f0aab5 100644
--- a/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java
+++ b/clients/client-java/src/main/java/com/datastrato/gravitino/client/RelationalCatalog.java
@@ -265,17 +265,17 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*
* @param ident The name identifier of the schema.
* @param comment The comment of the schema.
- * @param metadata The metadata of the schema.
+ * @param properties The properties of the schema.
* @return The created {@link Schema}.
* @throws NoSuchCatalogException if the catalog with specified namespace does not exist.
* @throws SchemaAlreadyExistsException if the schema with specified identifier already exists.
*/
@Override
- public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)
+ public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
NameIdentifier.checkSchema(ident);
- SchemaCreateRequest req = new SchemaCreateRequest(ident.name(), comment, metadata);
+ SchemaCreateRequest req = new SchemaCreateRequest(ident.name(), comment, properties);
req.validate();
SchemaResponse resp =
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java
index 9eff8c9c33..b79a879857 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogOperationDispatcher.java
@@ -101,14 +101,14 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
*
* @param ident The identifier for the schema to be created.
* @param comment The comment for the new schema.
- * @param metadata Additional metadata for the new schema.
+ * @param properties Additional properties for the new schema.
* @return The created Schema object.
* @throws NoSuchCatalogException If the catalog corresponding to the provided identifier does not
* exist.
* @throws SchemaAlreadyExistsException If a schema with the same identifier already exists.
*/
@Override
- public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)
+ public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
NameIdentifier catalogIdent = getCatalogIdentifier(ident);
doWithCatalog(
@@ -116,7 +116,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
c ->
c.doWithPropertiesMeta(
p -> {
- p.schemaPropertiesMetadata().validatePropertyForCreate(metadata);
+ p.schemaPropertiesMetadata().validatePropertyForCreate(properties);
return null;
}),
IllegalArgumentException.class);
@@ -125,7 +125,7 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
// StringIdentifier to make sure only when the operation is successful, the related
// SchemaEntity will be visible.
StringIdentifier stringId = StringIdentifier.fromId(uid);
- Map<String, String> updatedProperties = StringIdentifier.addToProperties(stringId, metadata);
+ Map<String, String> updatedProperties = StringIdentifier.addToProperties(stringId, properties);
doWithCatalog(
catalogIdent,
diff --git a/core/src/test/java/com/datastrato/gravitino/TestCatalogOperations.java b/core/src/test/java/com/datastrato/gravitino/TestCatalogOperations.java
index 72aed3d5b1..141fa08568 100644
--- a/core/src/test/java/com/datastrato/gravitino/TestCatalogOperations.java
+++ b/core/src/test/java/com/datastrato/gravitino/TestCatalogOperations.java
@@ -186,7 +186,7 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
}
@Override
- public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)
+ public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
AuditInfo auditInfo =
new AuditInfo.Builder().withCreator("test").withCreateTime(Instant.now()).build();
@@ -195,7 +195,7 @@ public class TestCatalogOperations implements CatalogOperations, TableCatalog, S
new TestSchema.Builder()
.withName(ident.name())
.withComment(comment)
- .withProperties(metadata)
+ .withProperties(properties)
.withAuditInfo(auditInfo)
.build();
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
index dc207e97e0..fa52203904 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/hive/CatalogHiveIT.java
@@ -31,6 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import com.datastrato.gravitino.Catalog;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.catalog.hive.HiveClientPool;
+import com.datastrato.gravitino.catalog.hive.HiveSchemaPropertiesMetadata;
import com.datastrato.gravitino.catalog.hive.HiveTablePropertiesMetadata;
import com.datastrato.gravitino.client.GravitinoMetaLake;
import com.datastrato.gravitino.dto.rel.ColumnDTO;
@@ -79,10 +80,12 @@ import org.junit.jupiter.api.TestMethodOrder;
public class CatalogHiveIT extends AbstractIT {
public static String metalakeName = GravitinoITUtils.genRandomName("CatalogHiveIT_metalake");
public static String catalogName = GravitinoITUtils.genRandomName("CatalogHiveIT_catalog");
- public static String schemaName = GravitinoITUtils.genRandomName("CatalogHiveIT_schema");
- public static String tableName = GravitinoITUtils.genRandomName("CatalogHiveIT_table");
- public static String alertTableName = "alert_table_name";
- public static String table_comment = "table_comment";
+ public static String SCHEMA_PREFIX = "CatalogHiveIT_schema";
+ public static String schemaName = GravitinoITUtils.genRandomName(SCHEMA_PREFIX);
+ public static String TABLE_PREFIX = "CatalogHiveIT_table";
+ public static String tableName = GravitinoITUtils.genRandomName(TABLE_PREFIX);
+ public static String ALTER_TABLE_NAME = "alert_table_name";
+ public static String TABLE_COMMENT = "table_comment";
public static String HIVE_COL_NAME1 = "hive_col_name1";
public static String HIVE_COL_NAME2 = "hive_col_name2";
public static String HIVE_COL_NAME3 = "hive_col_name3";
@@ -183,14 +186,18 @@ public class CatalogHiveIT extends AbstractIT {
private static void createSchema() throws TException, InterruptedException {
NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName, schemaName);
- Map<String, String> properties1 = Maps.newHashMap();
- properties1.put("key1", "val1");
- properties1.put("key2", "val2");
+ Map<String, String> properties = Maps.newHashMap();
+ properties.put("key1", "val1");
+ properties.put("key2", "val2");
String comment = "comment";
- Schema createdSchema = catalog.asSchemas().createSchema(ident, comment, properties1);
+ catalog.asSchemas().createSchema(ident, comment, properties);
Schema loadSchema = catalog.asSchemas().loadSchema(ident);
- Assertions.assertEquals(createdSchema.name().toLowerCase(), loadSchema.name());
+ Assertions.assertEquals(schemaName.toLowerCase(), loadSchema.name());
+ Assertions.assertEquals(comment, loadSchema.comment());
+ Assertions.assertEquals("val1", loadSchema.properties().get("key1"));
+ Assertions.assertEquals("val2", loadSchema.properties().get("key2"));
+ Assertions.assertNotNull(loadSchema.properties().get(HiveSchemaPropertiesMetadata.LOCATION));
// Directly get database from hive metastore to verify the schema creation
Database database = hiveClientPool.run(client -> client.getDatabase(schemaName));
@@ -284,7 +291,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
nameIdentifier,
columns,
- table_comment,
+ TABLE_COMMENT,
properties,
new Transform[0],
distribution,
@@ -305,7 +312,7 @@ public class CatalogHiveIT extends AbstractIT {
Table createdTable1 =
catalog
.asTableCatalog()
- .createTable(nameIdentifier, columns, table_comment, properties, (Transform[]) null);
+ .createTable(nameIdentifier, columns, TABLE_COMMENT, properties, (Transform[]) null);
// Directly get table from hive metastore to check if the table is created successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTable1 =
@@ -335,7 +342,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
nameIdentifier,
columns,
- table_comment,
+ TABLE_COMMENT,
properties,
new Transform[0],
badDistribution,
@@ -359,7 +366,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
nameIdentifier,
columns,
- table_comment,
+ TABLE_COMMENT,
properties,
new Transform[0],
distribution,
@@ -378,7 +385,7 @@ public class CatalogHiveIT extends AbstractIT {
Table createdTable =
catalog
.asTableCatalog()
- .createTable(nameIdentifier, columns, table_comment, properties, new Transform[0]);
+ .createTable(nameIdentifier, columns, TABLE_COMMENT, properties, new Transform[0]);
// Directly get table from hive metastore to check if the table is created successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTab =
@@ -395,7 +402,7 @@ public class CatalogHiveIT extends AbstractIT {
Table createdTable1 =
catalog
.asTableCatalog()
- .createTable(nameIdentifier, columns, table_comment, properties, (Transform[]) null);
+ .createTable(nameIdentifier, columns, TABLE_COMMENT, properties, (Transform[]) null);
// Directly get table from hive metastore to check if the table is created successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTable1 =
@@ -419,7 +426,7 @@ public class CatalogHiveIT extends AbstractIT {
catalog
.asTableCatalog()
.createTable(
- nameIdentifier, columns, table_comment, ImmutableMap.of(), new Transform[0]);
+ nameIdentifier, columns, TABLE_COMMENT, ImmutableMap.of(), new Transform[0]);
HiveTablePropertiesMetadata tablePropertiesMetadata = new HiveTablePropertiesMetadata();
org.apache.hadoop.hive.metastore.api.Table actualTable =
hiveClientPool.run(client -> client.getTable(schemaName, tableName));
@@ -427,14 +434,14 @@ public class CatalogHiveIT extends AbstractIT {
checkTableReadWrite(actualTable);
// test set properties
- String table2 = GravitinoITUtils.genRandomName("CatalogHiveIT_table");
+ String table2 = GravitinoITUtils.genRandomName(TABLE_PREFIX);
Table createdTable2 =
catalog
.asTableCatalog()
.createTable(
NameIdentifier.of(metalakeName, catalogName, schemaName, table2),
columns,
- table_comment,
+ TABLE_COMMENT,
ImmutableMap.of(
TABLE_TYPE,
"external_table",
@@ -454,7 +461,7 @@ public class CatalogHiveIT extends AbstractIT {
Assertions.assertEquals(IGNORE_KEY_OUTPUT_FORMAT_CLASS, actualTable2.getSd().getOutputFormat());
Assertions.assertEquals(EXTERNAL_TABLE.name(), actualTable2.getTableType());
Assertions.assertEquals(table2, actualTable2.getSd().getSerdeInfo().getName());
- Assertions.assertEquals(table_comment, actualTable2.getParameters().get(COMMENT));
+ Assertions.assertEquals(TABLE_COMMENT, actualTable2.getParameters().get(COMMENT));
Assertions.assertEquals(
((Boolean) tablePropertiesMetadata.getDefaultValue(EXTERNAL)).toString().toUpperCase(),
actualTable.getParameters().get(EXTERNAL));
@@ -478,6 +485,39 @@ public class CatalogHiveIT extends AbstractIT {
Assertions.assertTrue(exception.getMessage().contains("cannot be set"));
}
+ @Test
+ public void testHiveSchemaProperties() throws TException, InterruptedException {
+ // test LOCATION property
+ NameIdentifier schemaIdent =
+ NameIdentifier.of(metalakeName, catalogName, GravitinoITUtils.genRandomName(SCHEMA_PREFIX));
+ Map<String, String> properties = Maps.newHashMap();
+ String expectedSchemaLocation = "/tmp";
+ properties.put(HiveSchemaPropertiesMetadata.LOCATION, expectedSchemaLocation);
+ catalog.asSchemas().createSchema(schemaIdent, "comment", properties);
+
+ Database actualSchema = hiveClientPool.run(client -> client.getDatabase(schemaIdent.name()));
+ String actualSchemaLocation = actualSchema.getLocationUri();
+ Assertions.assertTrue(actualSchemaLocation.endsWith(expectedSchemaLocation));
+
+ NameIdentifier tableIdent =
+ NameIdentifier.of(
+ metalakeName,
+ catalogName,
+ schemaIdent.name(),
+ GravitinoITUtils.genRandomName(TABLE_PREFIX));
+ catalog
+ .asTableCatalog()
+ .createTable(
+ tableIdent, createColumns(), TABLE_COMMENT, ImmutableMap.of(), new Transform[0]);
+ org.apache.hadoop.hive.metastore.api.Table actualTable =
+ hiveClientPool.run(client -> client.getTable(schemaIdent.name(), tableIdent.name()));
+ String actualTableLocation = actualTable.getSd().getLocation();
+ // use `tableIdent.name().toLowerCase()` because HMS will convert table name to lower
+ String expectedTableLocation = expectedSchemaLocation + "/" + tableIdent.name().toLowerCase();
+ Assertions.assertTrue(actualTableLocation.endsWith(expectedTableLocation));
+ checkTableReadWrite(actualTable);
+ }
+
@Test
public void testCreatePartitionedHiveTable() throws TException, InterruptedException {
// Create table from Gravitino API
@@ -492,7 +532,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
nameIdentifier,
columns,
- table_comment,
+ TABLE_COMMENT,
properties,
new Transform[] {identity(columns[1]), identity(columns[2])});
@@ -516,7 +556,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
nameIdentifier,
columns,
- table_comment,
+ TABLE_COMMENT,
properties,
new Transform[] {identity(columns[0]), identity(columns[1])});
});
@@ -537,7 +577,7 @@ public class CatalogHiveIT extends AbstractIT {
Assertions.assertEquals(schemaName.toLowerCase(), hiveTab.getDbName());
Assertions.assertEquals(tableName.toLowerCase(), hiveTab.getTableName());
Assertions.assertEquals("MANAGED_TABLE", hiveTab.getTableType());
- Assertions.assertEquals(table_comment, hiveTab.getParameters().get("comment"));
+ Assertions.assertEquals(TABLE_COMMENT, hiveTab.getParameters().get("comment"));
Assertions.assertEquals(HIVE_COL_NAME1, actualColumns.get(0).getName());
Assertions.assertEquals("tinyint", actualColumns.get(0).getType());
@@ -599,7 +639,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
columns,
- table_comment,
+ TABLE_COMMENT,
createProperties(),
new Transform[] {identity(columns[2])});
Table alteredTable =
@@ -607,8 +647,8 @@ public class CatalogHiveIT extends AbstractIT {
.asTableCatalog()
.alterTable(
NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
- TableChange.rename(alertTableName),
- TableChange.updateComment(table_comment + "_new"),
+ TableChange.rename(ALTER_TABLE_NAME),
+ TableChange.updateComment(TABLE_COMMENT + "_new"),
TableChange.removeProperty("key1"),
TableChange.setProperty("key2", "val2_new"),
TableChange.addColumn(
@@ -623,9 +663,9 @@ public class CatalogHiveIT extends AbstractIT {
// Direct get table from hive metastore to check if the table is altered successfully.
org.apache.hadoop.hive.metastore.api.Table hiveTab =
- hiveClientPool.run(client -> client.getTable(schemaName, alertTableName));
+ hiveClientPool.run(client -> client.getTable(schemaName, ALTER_TABLE_NAME));
Assertions.assertEquals(schemaName.toLowerCase(), hiveTab.getDbName());
- Assertions.assertEquals(alertTableName, hiveTab.getTableName());
+ Assertions.assertEquals(ALTER_TABLE_NAME, hiveTab.getTableName());
Assertions.assertEquals("val2_new", hiveTab.getParameters().get("key2"));
Assertions.assertEquals(HIVE_COL_NAME1, hiveTab.getSd().getCols().get(0).getName());
@@ -653,7 +693,7 @@ public class CatalogHiveIT extends AbstractIT {
catalog
.asTableCatalog()
.alterTable(
- NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName),
+ NameIdentifier.of(metalakeName, catalogName, schemaName, ALTER_TABLE_NAME),
TableChange.updateColumnType(
new String[] {HIVE_COL_NAME3}, TypeCreator.NULLABLE.I32));
});
@@ -690,7 +730,7 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
tableIdentifier,
newColumns,
- table_comment,
+ TABLE_COMMENT,
ImmutableMap.of(),
new Transform[0],
Distribution.NONE,
@@ -744,17 +784,17 @@ public class CatalogHiveIT extends AbstractIT {
.createTable(
NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
createColumns(),
- table_comment,
+ TABLE_COMMENT,
createProperties(),
new Transform[0]);
catalog
.asTableCatalog()
- .dropTable(NameIdentifier.of(metalakeName, catalogName, schemaName, alertTableName));
+ .dropTable(NameIdentifier.of(metalakeName, catalogName, schemaName, ALTER_TABLE_NAME));
// Directly get table from hive metastore to check if the table is dropped successfully.
assertThrows(
NoSuchObjectException.class,
- () -> hiveClientPool.run(client -> client.getTable(schemaName, alertTableName)));
+ () -> hiveClientPool.run(client -> client.getTable(schemaName, ALTER_TABLE_NAME)));
}
@Test