use of org.apache.iceberg.TableProperties in project hive by apache.
the class TestHiveIcebergStorageHandlerNoScan method testDeleteBackingTable.
@Test
public void testDeleteBackingTable() throws TException, IOException, InterruptedException {
TableIdentifier identifier = TableIdentifier.of("default", "customers");
shell.executeStatement("CREATE EXTERNAL TABLE customers " + "STORED BY ICEBERG " + testTables.locationForCreateTableSQL(identifier) + "TBLPROPERTIES ('" + InputFormatConfig.TABLE_SCHEMA + "'='" + SchemaParser.toJson(HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA) + "', " + "'" + InputFormatConfig.EXTERNAL_TABLE_PURGE + "'='FALSE', " + "'" + InputFormatConfig.CATALOG_NAME + "'='" + testTables.catalogName() + "')");
org.apache.hadoop.hive.metastore.api.Table hmsTable = shell.metastore().getTable("default", "customers");
Properties tableProperties = new Properties();
hmsTable.getParameters().entrySet().stream().filter(e -> !IGNORED_PARAMS.contains(e.getKey())).forEach(e -> tableProperties.put(e.getKey(), e.getValue()));
if (!Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
shell.executeStatement("DROP TABLE customers");
// Check if the table remains
testTables.loadTable(identifier);
} else {
// Check the HMS table parameters
Path hmsTableLocation = new Path(hmsTable.getSd().getLocation());
// Drop the table
shell.executeStatement("DROP TABLE customers");
// Check if we drop an exception when trying to drop the table
AssertHelpers.assertThrows("should throw exception", NoSuchTableException.class, "Table does not exist", () -> {
testTables.loadTable(identifier);
});
// Check if the files are kept
FileSystem fs = Util.getFs(hmsTableLocation, shell.getHiveConf());
Assert.assertEquals(1, fs.listStatus(hmsTableLocation).length);
Assert.assertEquals(1, fs.listStatus(new Path(hmsTableLocation, "metadata")).length);
}
}
use of org.apache.iceberg.TableProperties in project hive by apache.
the class TestHiveIcebergStorageHandlerNoScan method testIcebergAndHmsTableProperties.
@Test
public void testIcebergAndHmsTableProperties() throws Exception {
TableIdentifier identifier = TableIdentifier.of("default", "customers");
shell.executeStatement(String.format("CREATE EXTERNAL TABLE default.customers " + "STORED BY ICEBERG %s" + "TBLPROPERTIES ('%s'='%s', '%s'='%s', '%s'='%s', '%s'='%s')", // we need the location for HadoopTable based tests only
testTables.locationForCreateTableSQL(identifier), InputFormatConfig.TABLE_SCHEMA, SchemaParser.toJson(HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA), InputFormatConfig.PARTITION_SPEC, PartitionSpecParser.toJson(SPEC), "custom_property", "initial_val", InputFormatConfig.CATALOG_NAME, testTables.catalogName()));
// Check the Iceberg table parameters
org.apache.iceberg.Table icebergTable = testTables.loadTable(identifier);
Map<String, String> expectedIcebergProperties = new HashMap<>();
expectedIcebergProperties.put("custom_property", "initial_val");
expectedIcebergProperties.put("EXTERNAL", "TRUE");
expectedIcebergProperties.put("storage_handler", HiveIcebergStorageHandler.class.getName());
expectedIcebergProperties.put(serdeConstants.SERIALIZATION_FORMAT, "1");
// Check the HMS table parameters
org.apache.hadoop.hive.metastore.api.Table hmsTable = shell.metastore().getTable("default", "customers");
Map<String, String> hmsParams = hmsTable.getParameters().entrySet().stream().filter(e -> !IGNORED_PARAMS.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Properties tableProperties = new Properties();
tableProperties.putAll(hmsParams);
if (Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
expectedIcebergProperties.put(TableProperties.ENGINE_HIVE_ENABLED, "true");
}
if (MetastoreUtil.hive3PresentOnClasspath()) {
expectedIcebergProperties.put("bucketing_version", "2");
}
Assert.assertEquals(expectedIcebergProperties, icebergTable.properties());
if (Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
Assert.assertEquals(10, hmsParams.size());
Assert.assertEquals("initial_val", hmsParams.get("custom_property"));
Assert.assertEquals("TRUE", hmsParams.get(InputFormatConfig.EXTERNAL_TABLE_PURGE));
Assert.assertEquals("TRUE", hmsParams.get("EXTERNAL"));
Assert.assertEquals("true", hmsParams.get(TableProperties.ENGINE_HIVE_ENABLED));
Assert.assertEquals(HiveIcebergStorageHandler.class.getName(), hmsParams.get(hive_metastoreConstants.META_TABLE_STORAGE));
Assert.assertEquals(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE.toUpperCase(), hmsParams.get(BaseMetastoreTableOperations.TABLE_TYPE_PROP));
Assert.assertEquals(hmsParams.get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP), getCurrentSnapshotForHiveCatalogTable(icebergTable));
Assert.assertNull(hmsParams.get(BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP));
Assert.assertNotNull(hmsParams.get(hive_metastoreConstants.DDL_TIME));
Assert.assertNotNull(hmsParams.get(serdeConstants.SERIALIZATION_FORMAT));
} else {
Assert.assertEquals(7, hmsParams.size());
Assert.assertNull(hmsParams.get(TableProperties.ENGINE_HIVE_ENABLED));
}
// Check HMS inputformat/outputformat/serde
Assert.assertEquals(HiveIcebergInputFormat.class.getName(), hmsTable.getSd().getInputFormat());
Assert.assertEquals(HiveIcebergOutputFormat.class.getName(), hmsTable.getSd().getOutputFormat());
Assert.assertEquals(HiveIcebergSerDe.class.getName(), hmsTable.getSd().getSerdeInfo().getSerializationLib());
// Add two new properties to the Iceberg table and update an existing one
icebergTable.updateProperties().set("new_prop_1", "true").set("new_prop_2", "false").set("custom_property", "new_val").commit();
// Refresh the HMS table to see if new Iceberg properties got synced into HMS
hmsParams = shell.metastore().getTable("default", "customers").getParameters().entrySet().stream().filter(e -> !IGNORED_PARAMS.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
// 2 newly-added properties + previous_metadata_location prop
Assert.assertEquals(13, hmsParams.size());
Assert.assertEquals("true", hmsParams.get("new_prop_1"));
Assert.assertEquals("false", hmsParams.get("new_prop_2"));
Assert.assertEquals("new_val", hmsParams.get("custom_property"));
String prevSnapshot = getCurrentSnapshotForHiveCatalogTable(icebergTable);
icebergTable.refresh();
String newSnapshot = getCurrentSnapshotForHiveCatalogTable(icebergTable);
Assert.assertEquals(hmsParams.get(BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP), prevSnapshot);
Assert.assertEquals(hmsParams.get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP), newSnapshot);
} else {
Assert.assertEquals(7, hmsParams.size());
}
// Remove some Iceberg props and see if they're removed from HMS table props as well
if (Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
icebergTable.updateProperties().remove("custom_property").remove("new_prop_1").commit();
hmsParams = shell.metastore().getTable("default", "customers").getParameters();
Assert.assertFalse(hmsParams.containsKey("custom_property"));
Assert.assertFalse(hmsParams.containsKey("new_prop_1"));
Assert.assertTrue(hmsParams.containsKey("new_prop_2"));
}
// append some data and check whether HMS stats are aligned with snapshot summary
if (Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
List<Record> records = HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS;
testTables.appendIcebergTable(shell.getHiveConf(), icebergTable, FileFormat.PARQUET, null, records);
hmsParams = shell.metastore().getTable("default", "customers").getParameters();
Map<String, String> summary = icebergTable.currentSnapshot().summary();
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP), hmsParams.get(StatsSetupConst.NUM_FILES));
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_RECORDS_PROP), hmsParams.get(StatsSetupConst.ROW_COUNT));
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP), hmsParams.get(StatsSetupConst.TOTAL_SIZE));
}
}
use of org.apache.iceberg.TableProperties in project hive by apache.
the class TestHiveIcebergStorageHandlerNoScan method testCreateDropTable.
@Test
public void testCreateDropTable() throws TException, IOException, InterruptedException {
TableIdentifier identifier = TableIdentifier.of("default", "customers");
shell.executeStatement("CREATE EXTERNAL TABLE customers " + "STORED BY ICEBERG " + testTables.locationForCreateTableSQL(identifier) + "TBLPROPERTIES ('" + InputFormatConfig.TABLE_SCHEMA + "'='" + SchemaParser.toJson(HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA) + "', " + "'" + InputFormatConfig.PARTITION_SPEC + "'='" + PartitionSpecParser.toJson(PartitionSpec.unpartitioned()) + "', " + "'dummy'='test', " + "'" + InputFormatConfig.CATALOG_NAME + "'='" + testTables.catalogName() + "')");
// Check the Iceberg table data
org.apache.iceberg.Table icebergTable = testTables.loadTable(identifier);
Assert.assertEquals(HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA.asStruct(), icebergTable.schema().asStruct());
Assert.assertEquals(PartitionSpec.unpartitioned(), icebergTable.spec());
org.apache.hadoop.hive.metastore.api.Table hmsTable = shell.metastore().getTable("default", "customers");
Properties tableProperties = new Properties();
hmsTable.getParameters().entrySet().stream().filter(e -> !IGNORED_PARAMS.contains(e.getKey())).forEach(e -> tableProperties.put(e.getKey(), e.getValue()));
if (!Catalogs.hiveCatalog(shell.getHiveConf(), tableProperties)) {
shell.executeStatement("DROP TABLE customers");
// Check if the table was really dropped even from the Catalog
AssertHelpers.assertThrows("should throw exception", NoSuchTableException.class, "Table does not exist", () -> {
testTables.loadTable(identifier);
});
} else {
Path hmsTableLocation = new Path(hmsTable.getSd().getLocation());
// Drop the table
shell.executeStatement("DROP TABLE customers");
// Check if we drop an exception when trying to load the table
AssertHelpers.assertThrows("should throw exception", NoSuchTableException.class, "Table does not exist", () -> {
testTables.loadTable(identifier);
});
// Check if the files are removed
FileSystem fs = Util.getFs(hmsTableLocation, shell.getHiveConf());
if (fs.exists(hmsTableLocation)) {
// if table directory has been deleted, we're good. This is the expected behavior in Hive4.
// if table directory exists, its contents should have been cleaned up, save for an empty metadata dir (Hive3).
Assert.assertEquals(1, fs.listStatus(hmsTableLocation).length);
Assert.assertEquals(0, fs.listStatus(new Path(hmsTableLocation, "metadata")).length);
}
}
}
Aggregations