Schema Class
TheSchema 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
TheSchema 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)
List<NestedField>
required
List of top-level columns
Schema(List<NestedField> columns, Set<Integer> identifierFieldIds)
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)
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()
columns()
asStruct()
highestFieldId()
Field Lookup
findField(int id)
int
required
The field ID to look up
findField(String name)
String
required
The field name (e.g., “id” or “user.email”)
caseInsensitiveFindField(String name)
String
required
The field name (case-insensitive)
findType(String name)
String
required
The field name
findType(int id)
int
required
The field ID
findColumnName(int id)
int
required
The field ID
idToName()
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()
- 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()
Schema Projection
select(String… names)
String...
required
Column names to select
select(Collection<String> names)
Collection<String>
required
Column names to select
caseInsensitiveSelect(String… names)
String...
required
Column names to select (case-insensitive)
caseInsensitiveSelect(Collection<String> names)
Collection<String>
required
Column names to select (case-insensitive)
Schema Comparison
sameSchema(Schema anotherSchema)
Schema
required
The schema to compare with
Aliases
getAliases()
aliasToId(String alias)
String
required
A full column name from the unconverted schema
idToAlias(Integer fieldId)
Integer
required
A column ID in this schema
Data Access
accessorForField(int id)
StructLike rows.
int
required
The field ID
Accessors do not retrieve data contained in lists or maps.
Static Methods
checkCompatibility(Schema schema, int formatVersion)
Schema
required
The schema to check
int
required
The table format version
IllegalStateException if the schema is incompatible
indexFields(Collection<Schema> schemas)
Collection<Schema>
required
The collection of schemas to index
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