Skip to main content

Schema Class

The Schema class represents the structure of a data table in Apache Iceberg. It defines the columns, their types, and identifier fields. Package: org.apache.iceberg

Overview

The Schema class provides:
  • Column definitions with field IDs and types
  • Nested field support through struct types
  • Schema evolution tracking with schema IDs
  • Identifier field management (similar to primary keys)
  • Field lookup by name or ID
  • Schema projection and selection
Schema IDs are only populated when reading from/writing to table metadata. Otherwise, the schema ID defaults to 0.

Constructors

Schema(List<NestedField> columns)

Creates a new schema with the given columns.
List<NestedField>
required
List of top-level columns

Schema(List<NestedField> columns, Set<Integer> identifierFieldIds)

Creates a new schema with columns and identifier fields.
List<NestedField>
required
List of top-level columns
Set<Integer>
required
Set of field IDs that form the identifier

Schema(int schemaId, List<NestedField> columns, Set<Integer> identifierFieldIds)

Creates a new schema with a specific schema ID.
int
required
The schema ID
List<NestedField>
required
List of top-level columns
Set<Integer>
required
Set of field IDs that form the identifier

Basic Methods

schemaId()

Returns the schema ID for this schema. Returns: The schema ID (defaults to 0 if not set)

columns()

Returns a list of the columns (top-level fields) in this schema. Returns: List of NestedField objects

asStruct()

Returns the underlying struct type for this schema. Returns: The StructType representation of this schema

highestFieldId()

Returns the highest field ID in this schema, including nested fields. Returns: The highest field ID

Field Lookup

findField(int id)

Returns the field identified by the given field ID.
int
required
The field ID to look up
Returns: The field with the given ID, or null if not found Example:

findField(String name)

Returns a field by name. The name may be a top-level or nested field.
String
required
The field name (e.g., “id” or “user.email”)
Returns: The field with the given name, or null if not found Example:

caseInsensitiveFindField(String name)

Returns a field by name using case-insensitive matching.
String
required
The field name (case-insensitive)
Returns: The field with the given name, or null if not found

findType(String name)

Returns the type of a field identified by name.
String
required
The field name
Returns: The field’s Type, or null if not found

findType(int id)

Returns the type of a field identified by field ID.
int
required
The field ID
Returns: The field’s Type, or null if not found

findColumnName(int id)

Returns the full column name for the given field ID.
int
required
The field ID
Returns: The full column name (e.g., “user.email”), or null if not found

idToName()

Returns a map of field IDs to qualified field names. Returns: Map of field ID to qualified field name

Identifier Fields

Identifier fields in Iceberg are similar to primary keys in relational databases. They consist of a unique set of primitive fields that should uniquely identify a row. However, Iceberg does not enforce uniqueness.

identifierFieldIds()

Returns the set of identifier field IDs. Returns: Set of field IDs that form the identifier Identifier Field Rules:
  • Must be primitive types (not structs, lists, or maps)
  • Must be required fields (not optional)
  • Cannot be float or double types
  • Must be at root level or nested in a chain of required structs
  • Can include nested fields (e.g., “user.last_name”)

identifierFieldNames()

Returns the set of identifier field names. Returns: Set of qualified field names that form the identifier Example:

Schema Projection

select(String… names)

Creates a projection schema for a subset of columns.
String...
required
Column names to select
Returns: A projection schema containing only the selected columns Example:

select(Collection<String> names)

Creates a projection schema for a subset of columns.
Collection<String>
required
Column names to select
Returns: A projection schema containing only the selected columns

caseInsensitiveSelect(String… names)

Creates a projection schema using case-insensitive column matching.
String...
required
Column names to select (case-insensitive)
Returns: A projection schema containing only the selected columns

caseInsensitiveSelect(Collection<String> names)

Creates a projection schema using case-insensitive column matching.
Collection<String>
required
Column names to select (case-insensitive)
Returns: A projection schema containing only the selected columns

Schema Comparison

sameSchema(Schema anotherSchema)

Checks whether this schema is equivalent to another schema while ignoring the schema ID.
Schema
required
The schema to compare with
Returns: True if the schemas are equivalent (same structure and identifier fields)

Aliases

getAliases()

Returns the alias map for this schema, if set. Alias maps are created when translating external schemas (like Avro) to Iceberg format. Returns: Map of column aliases to field IDs, or null if no aliases

aliasToId(String alias)

Returns the column ID for the given column alias.
String
required
A full column name from the unconverted schema
Returns: The column ID, or null if the alias doesn’t exist

idToAlias(Integer fieldId)

Returns the full column name in the unconverted schema for the given column ID.
Integer
required
A column ID in this schema
Returns: The column alias, or null if not found

Data Access

accessorForField(int id)

Returns an accessor for retrieving data from StructLike rows.
int
required
The field ID
Returns: An Accessor to retrieve values from a StructLike row
Accessors do not retrieve data contained in lists or maps.

Static Methods

checkCompatibility(Schema schema, int formatVersion)

Checks the compatibility of the schema with a format version. This validates that the schema does not contain types released in later format versions.
Schema
required
The schema to check
int
required
The table format version
Throws: IllegalStateException if the schema is incompatible

indexFields(Collection<Schema> schemas)

Indexes all fields from multiple schemas. This method favors field definitions from higher schema IDs to handle type promotions.
Collection<Schema>
required
The collection of schemas to index
Returns: Map of field IDs to fields

Usage Examples

Creating a Schema

Creating a Schema with Identifier Fields

Looking Up Fields

Schema Projection

Working with Identifier Fields

Comparing Schemas

Source Code Reference

Source: org/apache/iceberg/Schema.java:56