# absaoss/abris architecture

> Architecture diagram of the GitHub repository absaoss/abris, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/absaoss/abris
- Repository: https://github.com/absaoss/abris

## Overview

##### Project type and purpose
- This is a Scala-based library (not an end-user application) that provides seamless integration between Apache Spark and Apache Avro, including native Avro, Confluent Avro, and Schema Registry support. It exposes two main Spark SQL expressions (`to_avro`, `from_avro`), a fluent configuration API (`AbrisConfig`), SPI extensibility points (custom `SchemaConverter` implementations), error-handling strategies, and utilities for schema conversion and management.

##### File structure
- Top-level build artifacts: `pom.xml`, `scalastyle-config.xml`, GitHub workflows for CI, code coverage, compatibility checks.
- `src/main/scala/org/apache/spark/sql/avro`: Core Spark UDF definitions (`AbrisAvroSerializer`, `AbrisAvroDeserializer`).
- `src/main/scala/za/co/absa/abris/avro`: Modular sub-packages:
- `errors` – exception handler implementations (FailFast, Permissive, SpecificRecord).
- `format` – conversions between Spark internal types and Avro bytes.
- `parsing/utils` – Avro schema utility functions.
- `read/confluent` – Confluent format constants and schema manager factory.
- `registry` – Schema Registry client abstractions and implementations (mock, Confluent).
- `sql` – SQL-level hooks for converting Avro ↔ Spark Row and default/custom schema converters.
- `src/main/avro` – example Avro schemas for testing/documentation.
- `src/main/resources/META-INF/services` – SPI registration for custom `SchemaConverter`.
- `src/main/scala/za/co/absa/abris/config` – centralized configuration mapping for to/from Avro use cases.
- `src/main/scala/za/co/absa/abris/examples` – executable examples (Kafka reader/writer, data generators).
- `src/test` – comprehensive unit tests mirroring all modules and entry points.

##### README for additional insights
- Confirms the library’s two main entry points (`to_avro`, `from_avro`), four config flows (`toSimpleAvro`, `toConfluentAvro`, `fromSimpleAvro`, `fromConfluentAvro`), the use of Confluent Schema Registry, authentication flexibility, and error handling strategies.
- Points to extended documentation in `documentation/*.md`.

a. Main components
- Data sources/sinks: Kafka topics, Parquet/HDFS files, raw Avro streams.
- Spark SQL engine & runtime (cluster).
- ABRiS library layers:
– SQL expressions (`to_avro`, `from_avro`)
– Fluent `AbrisConfig` builder (to/from, naming strategies, registry URLs, auth configs, exception handlers, converter selection)
– Core modules: Serializer, Deserializer, `AvroSchemaUtils`, `SparkAvroConversions`
– Registry client abstraction & implementations
– SchemaManager and SchemaManagerFactory
– SPI loader for custom `SchemaConverter`
– Exception handler strategies
- Confluent Schema Registry (REST endpoint)
- Optional custom extensions (user-provided `SchemaConverter`, custom ExceptionHandler)
- Build/packaging: Maven coordinates and dependencies (Spark, spark-avro, abris)

b. Relationships and interactions
1. User Spark job pulls binary Avro data (Kafka/HDFS) into DataFrame.
2. Job invokes `from_avro(column, AbrisConfig…)` → SQL expression layer.
3. Expression delegates to Deserializer module → requests Avro schema:
- If Confluent mode → uses SchemaManager → uses RegistryClient to call Schema Registry REST API.
- If simple mode → uses provided static schema or auto-generated via `AvroSchemaUtils`.
4. SchemaConverter (default or SPI custom) maps Avro `Schema` → Spark `StructType`.
5. Deserializer produces Spark Rows.
6. For `to_avro`, the reverse: Spark Row → Catalyst internal → Avro record → binary data, optional prepended Confluent schema id.
7. Error handlers intercept serialization/deserialization exceptions and apply configured strategy.

c. Architectural patterns / design principles
- Layered modular architecture separating concerns (SQL API, core conversion logic, registry access, error handling).
- Fluent builder pattern for configuration (`AbrisConfig`).
- SPI (Java ServiceLoader) for pluggable schema converters.
- Abstraction over Confluent registry (mockable for testing).
- Clear separation between “simple” Avro (static schema) and “Confluent” Avro (dynamic registry).

d. Key technologies / frameworks
- Apache Spark SQL (2.4+ / 3.x) and Spark’s DataFrame/Catalyst engine
- Apache Avro (binary serialization, schema definitions)
- Confluent Schema Registry (REST client)
- Scala (2.11/2.12/2.13), Maven, Java SPI, Log4j

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Data Sources
    subgraph "External Data Sources" 
        direction TB
        Kafka["Kafka Topics"]:::external
        HDFS["Parquet/HDFS Files"]:::external
        RawAvro["Raw Avro Streams"]:::external
    end

    %% Spark Engine
    SparkEngine["Spark SQL Engine & Runtime"]:::spark

    %% ABRiS SQL API Layer
    subgraph "ABRiS SQL API" 
        direction TB
        AbrisAvroDeserializer["from_avro Expression"]:::abris
        AbrisAvroSerializer["to_avro Expression"]:::abris
        click AbrisAvroDeserializer "https://github.com/absaoss/abris/blob/master/src/main/scala/org/apache/spark/sql/avro/AbrisAvroDeserializer.scala"
        click AbrisAvroSerializer "https://github.com/absaoss/abris/blob/master/src/main/scala/org/apache/spark/sql/avro/AbrisAvroSerializer.scala"
    end

    %% Configuration Builder
    AbrisConfig["AbrisConfig Fluent Builder"]:::config
    click AbrisConfig "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/config/Config.scala"
    click AbrisConfig "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/config/InternalToAvroConfig.scala"
    click AbrisConfig "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/config/InternalFromAvroConfig.scala"

    %% Conversion Core Modules
    subgraph "Conversion Core" 
        direction TB
        subgraph SerializerCore
            CatalystDataToAvro["CatalystDataToAvro"]:::core
            SparkAvroConversions["SparkAvroConversions"]:::core
            click CatalystDataToAvro "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/sql/CatalystDataToAvro.scala"
            click SparkAvroConversions "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/format/SparkAvroConversions.scala"
        end
        subgraph DeserializerCore
            AvroDataToCatalyst["AvroDataToCatalyst"]:::core
            AvroSchemaUtils["AvroSchemaUtils"]:::core
            click AvroDataToCatalyst "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/sql/AvroDataToCatalyst.scala"
            click AvroSchemaUtils "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/parsing/utils/AvroSchemaUtils.scala"
        end
        ExceptionHandlers["Exception Handling Module"]:::ext
        SchemaConverterSPI["Schema Converter SPI"]:::ext
        click SchemaConverterSPI "https://github.com/absaoss/abris/blob/master/src/main/resources/META-INF/services/za.co.absa.abris.avro.sql.SchemaConverter"
        click SchemaConverterSPI "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/sql/SchemaConverter.scala"
        click SchemaConverterSPI "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/sql/DefaultSchemaConverter.scala"
        click ExceptionHandlers "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/errors/FailFastExceptionHandler.scala"
        click ExceptionHandlers "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/errors/PermissiveRecordExceptionHandler.scala"
        click ExceptionHandlers "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/errors/SpecificRecordExceptionHandler.scala"
        click ExceptionHandlers "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/errors/DeserializationExceptionHandler.scala"
    end

    %% Registry Client Layer
    subgraph "Registry Client" 
        direction TB
        SchemaManagerFactory["SchemaManagerFactory"]:::core
        SchemaManager["SchemaManager"]:::core
        ConfluentConstants["ConfluentConstants"]:::core
        AbrisRegistryClient["AbrisRegistryClient"]:::core
        ConfluentRegistryClient["ConfluentRegistryClient"]:::core
        ConfluentMockRegistryClient["ConfluentMockRegistryClient"]:::core
        AbstractClient["AbstractConfluentRegistryClient"]:::core
        click SchemaManagerFactory "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/read/confluent/SchemaManagerFactory.scala"
        click SchemaManager "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/read/confluent/SchemaManager.scala"
        click ConfluentConstants "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/read/confluent/ConfluentConstants.scala"
        click AbrisRegistryClient "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/registry/AbrisRegistryClient.scala"
        click AbstractClient "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/registry/AbstractConfluentRegistryClient.scala"
        click ConfluentRegistryClient "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/registry/ConfluentRegistryClient.scala"
        click ConfluentMockRegistryClient "https://github.com/absaoss/abris/blob/master/src/main/scala/za/co/absa/abris/avro/registry/ConfluentMockRegistryClient.scala"
    end

    %% External Schema Registry
    SchemaRegistry["Confluent Schema Registry REST"]:::external

    %% Flow Connections
    Kafka -->|reads Avro bytes| SparkEngine
    HDFS -->|reads Avro bytes| SparkEngine
    RawAvro -->|reads Avro bytes| SparkEngine

    SparkEngine -->|invokes from_avro / to_avro| AbrisAvroDeserializer
    SparkEngine -->|invokes to_avro / from_avro| AbrisAvroSerializer

    AbrisAvroDeserializer --> AbrisConfig
    AbrisAvroSerializer --> AbrisConfig

    AbrisConfig -->|configures| CatalystDataToAvro
    AbrisConfig -->|configures| AvroDataToCatalyst

    CatalystDataToAvro --> SparkAvroConversions
    AvroDataToCatalyst --> AvroSchemaUtils

    AvroDataToCatalyst --> SchemaManagerFactory
    SchemaManagerFactory --> AbrisRegistryClient
    AbrisRegistryClient -->|REST API| SchemaRegistry

    SparkAvroConversions --> SchemaManagerFactory
    SchemaManagerFactory --> AbstractClient

    CatalystDataToAvro --> ExceptionHandlers
    AvroDataToCatalyst --> ExceptionHandlers

    AvroSchemaUtils --> SchemaConverterSPI
    SchemaConverterSPI --> AvroDataToCatalyst

    %% Return flow to sink
    CatalystDataToAvro -->|produce Avro bytes| SparkEngine
    SparkEngine -->|writes Avro bytes| Kafka
    SparkEngine -->|writes Avro bytes| HDFS

    %% Styles
    classDef external fill:#8FBC8F,stroke:#333,stroke-width:1px
    classDef spark fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef abris fill:#6495ED,stroke:#333,stroke-width:1px
    classDef config fill:#87CEEB,stroke:#333,stroke-width:1px
    classDef core fill:#1E90FF,stroke:#333,stroke-width:1px
    classDef ext fill:#D3D3D3,stroke:#333,stroke-width:1px
```
