Skip to content

kfyodor/avro-repo

Repository files navigation

Avro-repository for Clojure

CircleCI Clojars Project

A little library for managing Avro schemas in your Clojure projects.

This is a part of thdr/kfk project: clients and helpers for working with Apache Kafka in Clojure.

Install

Boot / Leiningen

[io.thdr/avro-repo "0.1.0"]

Maven

<dependency>
  <groupId>io.thdr</groupId>
  <artifactId>avro-repo</artifactId>
  <version>0.1.0</version>
</dependency>

Usage

Loading schemas.

Make sure you schemas directory is accessible somewhere on the classpath.

Use (load-schemas! schemas-path) to load Avro schemas into your app. load-schemas! returns a map where the key by default is a kebab-cased and keywordized schema's filename and the value is an org.apache.avro.Schema instance.

You can provide :key-fn option to customize keys in a resulting map:

(load-schemas! schemas-path 
               {:key-fn (fn [basename schema] (.getName schema))})

Sharing custom types and schemas

If types or includes directories are present on provided schemas-path, they will be loaded first. Avro-repo loads schemas in the following order:

:schemas-path:/types/**/*.avsc
:schemas-path:/includes/**/*.avsc
everithing else on :schemas-path:

Using schemas

A map with schemas can be accessed directly though it's better to create a record with proto/SchemaRepo protocol implemented and use it with something like Component library.

Note that values are raw Schema instances and JSON-serialization is quite expensive. If you need each Schema to be serialized to JSON (for example, when working with Confluent's schema-registry), be sure to implement some kind of caching mechanism. In future versions I'm thinking of adding this feature to this library.

(ns schema-repo
  (:require [com.stuartsierra.component :as component]
            [thdr.avro-repo
			  [core :refer [load-schemas!]]
			  [proto :as proto]]))
		     
(defrecord Schemas [schemas]
  component/Lifecycle
  
  (start [this]
	(assoc this :schemas (load-schemas!)))

  (stop [this]
    (assoc this :schemas {}))
	
  proto/SchemaRepo
  
  (get-schema [this k]
    (get schemas k)))

Contributing

Feel free to open an issue or PR.

License

Copyright © 2016 Theodore Konukhov me@thdr.io

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

About

Avro Schemas repository for Clojure

Resources

License

Stars

Watchers

Forks

Packages

No packages published