use of org.apache.iceberg.UpdateProperties in project drill by apache.
the class IcebergMetastore method updateTableProperties.
/**
* Checks config table properties against current table properties.
* Adds properties that are absent, updates existing and removes absent.
* If properties are the same, does nothing.
*
* @param table Iceberg table instance
* @param tableProperties table properties from the config
*/
private void updateTableProperties(Table table, Map<String, String> tableProperties) {
Map<String, String> currentProperties = table.properties();
MapDifference<String, String> difference = Maps.difference(tableProperties, currentProperties);
if (difference.areEqual()) {
return;
}
UpdateProperties updateProperties = table.updateProperties();
// collect properties that are different
Map<String, String> propertiesToUpdate = difference.entriesDiffering().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().leftValue(), (o, n) -> n));
// add new properties
propertiesToUpdate.putAll(difference.entriesOnlyOnLeft());
logger.debug("Updating Iceberg table [{}] properties: {}", table.location(), updateProperties);
propertiesToUpdate.forEach(updateProperties::set);
logger.debug("Removing Iceberg table [{}] properties: {}", table.location(), difference.entriesOnlyOnRight());
difference.entriesOnlyOnRight().keySet().forEach(updateProperties::remove);
updateProperties.commit();
}
use of org.apache.iceberg.UpdateProperties in project incubator-gobblin by apache.
the class IcebergMetadataWriter method flush.
/**
* For flush of each table, we do the following logic:
* 1. Commit the appendFiles if it exist
* 2. Update the new table property: high watermark of GMCE, data offset range, schema versions
* 3. Update the schema
* 4. Commit the transaction
* 5. reset tableMetadata
* @param dbName
* @param tableName
*/
@Override
public void flush(String dbName, String tableName) throws IOException {
Lock writeLock = readWriteLock.writeLock();
writeLock.lock();
try {
TableIdentifier tid = TableIdentifier.of(dbName, tableName);
TableMetadata tableMetadata = tableMetadataMap.getOrDefault(tid, new TableMetadata());
if (tableMetadata.transaction.isPresent()) {
Transaction transaction = tableMetadata.transaction.get();
Map<String, String> props = tableMetadata.newProperties.or(Maps.newHashMap(tableMetadata.lastProperties.or(getIcebergTable(tid).properties())));
if (tableMetadata.appendFiles.isPresent()) {
tableMetadata.appendFiles.get().commit();
if (tableMetadata.completenessEnabled) {
String topicName = props.get(TOPIC_NAME_KEY);
if (topicName == null) {
log.error(String.format("Not performing audit check. %s is null. Please set as table property of %s.%s", TOPIC_NAME_KEY, dbName, tableName));
} else {
long newCompletenessWatermark = computeCompletenessWatermark(topicName, tableMetadata.datePartitions, tableMetadata.prevCompletenessWatermark);
if (newCompletenessWatermark > tableMetadata.prevCompletenessWatermark) {
log.info(String.format("Updating %s for %s.%s to %s", COMPLETION_WATERMARK_KEY, dbName, tableName, newCompletenessWatermark));
props.put(COMPLETION_WATERMARK_KEY, String.valueOf(newCompletenessWatermark));
props.put(COMPLETION_WATERMARK_TIMEZONE_KEY, this.timeZone);
tableMetadata.newCompletenessWatermark = newCompletenessWatermark;
}
}
}
}
if (tableMetadata.deleteFiles.isPresent()) {
tableMetadata.deleteFiles.get().commit();
}
// Set high waterMark
Long highWatermark = tableCurrentWatermarkMap.get(tid);
props.put(String.format(GMCE_HIGH_WATERMARK_KEY, tableTopicPartitionMap.get(tid)), highWatermark.toString());
// Set low waterMark
props.put(String.format(GMCE_LOW_WATERMARK_KEY, tableTopicPartitionMap.get(tid)), tableMetadata.lowWatermark.get().toString());
// Set whether to delete metadata files after commit
props.put(TableProperties.METADATA_DELETE_AFTER_COMMIT_ENABLED, Boolean.toString(conf.getBoolean(TableProperties.METADATA_DELETE_AFTER_COMMIT_ENABLED, TableProperties.METADATA_DELETE_AFTER_COMMIT_ENABLED_DEFAULT)));
props.put(TableProperties.METADATA_PREVIOUS_VERSIONS_MAX, Integer.toString(conf.getInt(TableProperties.METADATA_PREVIOUS_VERSIONS_MAX, TableProperties.METADATA_PREVIOUS_VERSIONS_MAX_DEFAULT)));
// Set data offset range
boolean containOffsetRange = setDatasetOffsetRange(tableMetadata, props);
String topicName = tableName;
if (containOffsetRange) {
String topicPartitionString = tableMetadata.dataOffsetRange.get().keySet().iterator().next();
// In case the topic name is not the table name or the topic name contains '-'
topicName = topicPartitionString.substring(0, topicPartitionString.lastIndexOf('-'));
}
// Update schema(commit)
updateSchema(tableMetadata, props, topicName);
// Update properties
UpdateProperties updateProperties = transaction.updateProperties();
props.forEach(updateProperties::set);
updateProperties.commit();
try (AutoCloseableHiveLock lock = this.locks.getTableLock(dbName, tableName)) {
transaction.commitTransaction();
}
// Emit GTE for snapshot commits
Snapshot snapshot = tableMetadata.table.get().currentSnapshot();
Map<String, String> currentProps = tableMetadata.table.get().properties();
submitSnapshotCommitEvent(snapshot, tableMetadata, dbName, tableName, currentProps, highWatermark);
// Reset the table metadata for next accumulation period
tableMetadata.reset(currentProps, highWatermark, tableMetadata.newCompletenessWatermark);
log.info(String.format("Finish commit of new snapshot %s for table %s", snapshot.snapshotId(), tid.toString()));
} else {
log.info("There's no transaction initiated for the table {}", tid.toString());
}
} catch (RuntimeException e) {
throw new RuntimeException(String.format("Fail to flush table %s %s", dbName, tableName), e);
} catch (Exception e) {
throw new IOException(String.format("Fail to flush table %s %s", dbName, tableName), e);
} finally {
writeLock.unlock();
}
}
use of org.apache.iceberg.UpdateProperties in project hive by apache.
the class HiveIcebergMetaHook method alterTableProperties.
private void alterTableProperties(org.apache.hadoop.hive.metastore.api.Table hmsTable, Map<String, String> contextProperties) {
Map<String, String> hmsTableParameters = hmsTable.getParameters();
Splitter splitter = Splitter.on(PROPERTIES_SEPARATOR);
UpdateProperties icebergUpdateProperties = icebergTable.updateProperties();
if (contextProperties.containsKey(SET_PROPERTIES)) {
splitter.splitToList(contextProperties.get(SET_PROPERTIES)).forEach(k -> icebergUpdateProperties.set(k, hmsTableParameters.get(k)));
} else if (contextProperties.containsKey(UNSET_PROPERTIES)) {
splitter.splitToList(contextProperties.get(UNSET_PROPERTIES)).forEach(icebergUpdateProperties::remove);
}
icebergUpdateProperties.commit();
}
Aggregations