Class FileSchema.Builder

java.lang.Object
dev.hardwood.schema.FileSchema.Builder
Enclosing class:
FileSchema

public static final class FileSchema.Builder extends Object

Builder for constructing a FileSchema programmatically.

Fields are added as top-level primitive leaves (addColumn(String, PhysicalType, RepetitionType)), nested struct groups (struct(String, RepetitionType, Consumer)), LIST groups (list(String, RepetitionType, Consumer)), or MAP groups (map(String, RepetitionType, PhysicalType, Consumer)). The LIST and MAP groups emit the canonical 3-level and 2-level physical layouts.

  • Method Details

    • addColumn

      public FileSchema.Builder addColumn(String columnName, PhysicalType type, RepetitionType repetition)
      Append a primitive column.
      Parameters:
      columnName - the column name
      type - the physical type
      repetition - REQUIRED or OPTIONAL
      Throws:
      IllegalArgumentException - if repetition is REPEATED
    • addColumn

      public FileSchema.Builder addColumn(String columnName, PhysicalType type, RepetitionType repetition, int typeLength)
      Append a primitive column, giving the fixed byte length of a FIXED_LEN_BYTE_ARRAY column.
      Parameters:
      columnName - the column name
      type - the physical type
      repetition - REQUIRED or OPTIONAL
      typeLength - the fixed byte length (required and positive for FIXED_LEN_BYTE_ARRAY, rejected for any other type)
      Throws:
      IllegalArgumentException - if repetition is REPEATED or typeLength does not match the type
    • addColumn

      public FileSchema.Builder addColumn(String columnName, PhysicalType type, RepetitionType repetition, LogicalType logicalType)
      Append a primitive column carrying a logical type annotation.
      Parameters:
      columnName - the column name
      type - the physical type
      repetition - REQUIRED or OPTIONAL
      logicalType - the annotation, which must be legal for type
      Throws:
      IllegalArgumentException - if repetition is REPEATED or the annotation does not apply to the physical type
    • addColumn

      public FileSchema.Builder addColumn(String columnName, PhysicalType type, RepetitionType repetition, int typeLength, LogicalType logicalType)
      Append a primitive column carrying a logical type annotation, giving the fixed byte length of a FIXED_LEN_BYTE_ARRAY column.
      Parameters:
      columnName - the column name
      type - the physical type
      repetition - REQUIRED or OPTIONAL
      typeLength - the fixed byte length (required and positive for FIXED_LEN_BYTE_ARRAY, rejected for any other type)
      logicalType - the annotation, which must be legal for type and typeLength
      Throws:
      IllegalArgumentException - if repetition is REPEATED, typeLength does not match the type, or the annotation does not apply to the physical type
    • struct

      public FileSchema.Builder struct(String structName, RepetitionType repetition, Consumer<FileSchema.StructBuilder> filler)
      Append a struct group whose fields are declared by filler.
      Parameters:
      structName - the group name
      repetition - REQUIRED or OPTIONAL
      filler - declares the group's fields
      Throws:
      IllegalArgumentException - if repetition is REPEATED or the group has no fields
    • list

      public FileSchema.Builder list(String listName, RepetitionType repetition, Consumer<FileSchema.ElementBuilder> element)
      Append a LIST group whose element is declared by element.
      Parameters:
      listName - the list group name
      repetition - REQUIRED or OPTIONAL (whether the list itself may be null)
      element - declares the list's element
      Throws:
      IllegalArgumentException - if repetition is REPEATED or no element is declared
    • map

      public FileSchema.Builder map(String mapName, RepetitionType repetition, PhysicalType keyType, Consumer<FileSchema.ElementBuilder> value)
      Append a MAP group whose value is declared by value. The key is a required primitive of keyType per the canonical layout.
      Parameters:
      mapName - the map group name
      repetition - REQUIRED or OPTIONAL (whether the map itself may be null)
      keyType - the physical type of the required key
      value - declares the map's value, like a list element
      Throws:
      IllegalArgumentException - if repetition is REPEATED or no value is declared
    • map

      public FileSchema.Builder map(String mapName, RepetitionType repetition, PhysicalType keyType, LogicalType keyLogicalType, Consumer<FileSchema.ElementBuilder> value)
      Append a MAP group whose key carries a logical type annotation — a STRING key being the common case.
      Parameters:
      mapName - the map group name
      repetition - REQUIRED or OPTIONAL (whether the map itself may be null)
      keyType - the physical type of the required key
      keyLogicalType - the key's annotation, which must be legal for keyType
      value - declares the map's value, like a list element
      Throws:
      IllegalArgumentException - if repetition is REPEATED, no value is declared, or the annotation does not apply to the key's physical type
    • map

      public FileSchema.Builder map(String mapName, RepetitionType repetition, PhysicalType keyType, int keyTypeLength, LogicalType keyLogicalType, Consumer<FileSchema.ElementBuilder> value)
      Append a MAP group whose key carries a logical type annotation, giving the fixed byte length of a FIXED_LEN_BYTE_ARRAY key.
      Parameters:
      mapName - the map group name
      repetition - REQUIRED or OPTIONAL (whether the map itself may be null)
      keyType - the physical type of the required key
      keyTypeLength - the key's fixed byte length (required and positive for FIXED_LEN_BYTE_ARRAY, rejected for any other type)
      keyLogicalType - the key's annotation, which must be legal for keyType and keyTypeLength
      value - declares the map's value, like a list element
      Throws:
      IllegalArgumentException - if repetition is REPEATED, no value is declared, keyTypeLength does not match the key's type, or the annotation does not apply to it
    • build

      public FileSchema build()
      Build the schema.
      Throws:
      IllegalArgumentException - if no fields were added