Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
package org.apache.hudi.aws.sync;

import org.apache.hudi.common.fs.FSUtils;
import org.apache.hudi.common.table.TableSchemaResolver;
import org.apache.hudi.common.util.CollectionUtils;
import org.apache.hudi.common.util.Option;
import org.apache.hudi.config.GlueCatalogSyncClientConfig;
import org.apache.hudi.hive.HiveSyncConfig;
import org.apache.hudi.sync.common.HoodieSyncClient;
import org.apache.hudi.sync.common.model.FieldSchema;
import org.apache.hudi.sync.common.model.Partition;

import com.amazonaws.services.glue.AWSGlue;
Expand Down Expand Up @@ -237,6 +239,74 @@ public boolean updateTableProperties(String tableName, Map<String, String> table
}
}

private void setComments(List<Column> columns, Map<String, Option<String>> commentsMap) {
columns.forEach(column -> {
String comment = commentsMap.getOrDefault(column.getName(), Option.empty()).orElse(null);
column.setComment(comment);
});
}

private String getTableDoc() {
try {
return new TableSchemaResolver(metaClient).getTableAvroSchema(true).getDoc();
} catch (Exception e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the table avro schema be reused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the avro schema is already reused by running method getTableAvroSchema ?

throw new HoodieGlueSyncException("Failed to get schema's doc from storage : ", e);
}
}

@Override
public List<FieldSchema> getStorageFieldSchemas() {
try {
return new TableSchemaResolver(metaClient).getTableAvroSchema(true)
.getFields()
.stream()
.map(f -> new FieldSchema(f.name(), f.schema().getType().getName(), f.doc()))
.collect(Collectors.toList());
} catch (Exception e) {
throw new HoodieGlueSyncException("Failed to get field schemas from storage : ", e);
}
}

@Override
public boolean updateTableComments(String tableName, List<FieldSchema> fromMetastore, List<FieldSchema> fromStorage) {
Table table = getTable(awsGlue, databaseName, tableName);

Map<String, Option<String>> commentsMap = fromStorage.stream().collect(Collectors.toMap(FieldSchema::getName, FieldSchema::getComment));

StorageDescriptor storageDescriptor = table.getStorageDescriptor();
List<Column> columns = storageDescriptor.getColumns();
setComments(columns, commentsMap);

List<Column> partitionKeys = table.getPartitionKeys();
setComments(partitionKeys, commentsMap);

String tableDescription = getTableDoc();

if (getTable(awsGlue, databaseName, tableName).getStorageDescriptor().equals(storageDescriptor)
&& getTable(awsGlue, databaseName, tableName).getPartitionKeys().equals(partitionKeys)) {
// no comments have been modified / added
return false;
} else {
final Date now = new Date();
TableInput updatedTableInput = new TableInput()
.withName(tableName)
.withDescription(tableDescription)
.withTableType(table.getTableType())
.withParameters(table.getParameters())
.withPartitionKeys(partitionKeys)
.withStorageDescriptor(storageDescriptor)
.withLastAccessTime(now)
.withLastAnalyzedTime(now);

UpdateTableRequest request = new UpdateTableRequest()
.withDatabaseName(databaseName)
.withTableInput(updatedTableInput);

awsGlue.updateTable(request);
return true;
}
}

@Override
public void updateTableSchema(String tableName, MessageType newSchema) {
// ToDo Cascade is set in Hive meta sync, but need to investigate how to configure it for Glue meta
Expand Down