Skip to content

Commit a21ca39

Browse files
authored
[Iceberg AddFiles] Add NameMapping when creating table (#39022)
* improvement * fix nullness * clean
1 parent 630a34c commit a21ca39

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

  • sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg

sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/AddFiles.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.charset.StandardCharsets;
3232
import java.util.ArrayList;
3333
import java.util.Collections;
34+
import java.util.HashMap;
3435
import java.util.Iterator;
3536
import java.util.LinkedList;
3637
import java.util.List;
@@ -96,7 +97,6 @@
9697
import org.apache.iceberg.Table;
9798
import org.apache.iceberg.TableProperties;
9899
import org.apache.iceberg.avro.Avro;
99-
import org.apache.iceberg.catalog.Catalog;
100100
import org.apache.iceberg.catalog.TableIdentifier;
101101
import org.apache.iceberg.exceptions.AlreadyExistsException;
102102
import org.apache.iceberg.exceptions.NoSuchTableException;
@@ -531,17 +531,23 @@ private Table getOrCreateTable(String filePath, FileFormat format) throws IOExce
531531
org.apache.iceberg.Schema schema = getSchema(filePath, format);
532532
PartitionSpec spec = PartitionUtils.toPartitionSpec(partitionFields, schema);
533533
SortOrder sortOrder = SortOrderUtils.toSortOrder(sortFields, schema);
534-
535-
Catalog.TableBuilder builder =
536-
catalogConfig
537-
.catalog()
538-
.buildTable(tableId, schema)
539-
.withPartitionSpec(spec)
540-
.withSortOrder(sortOrder);
541-
if (tableProps != null) {
542-
builder.withProperties(tableProps);
534+
Map<String, String> properties =
535+
tableProps != null ? new HashMap<>(tableProps) : new HashMap<>();
536+
if (properties.get(TableProperties.DEFAULT_NAME_MAPPING) == null) {
537+
// Forces Name based resolution instead of position based resolution
538+
NameMapping mapping = MappingUtil.create(schema);
539+
String mappingJson = NameMappingParser.toJson(mapping);
540+
properties.put(TableProperties.DEFAULT_NAME_MAPPING, mappingJson);
543541
}
544-
return builder.create();
542+
543+
return catalogConfig
544+
.catalog()
545+
.buildTable(tableId, schema)
546+
.withPartitionSpec(spec)
547+
.withSortOrder(sortOrder)
548+
.withProperties(properties)
549+
.create();
550+
545551
} catch (AlreadyExistsException e2) { // if table already exists, just load it
546552
return catalogConfig.catalog().loadTable(TableIdentifier.parse(identifier));
547553
}

0 commit comments

Comments
 (0)