Coder Social home page Coder Social logo

avro-clj's Introduction

A Clojure Wrapper for Apache avro serialization/deserialization

Note: At the moment, avro-clj doesn't support Nested maps, which will be added in next release.

Example

(ns sample
  (:require [avroclj.avro :as avro]))

;The avro schema to use for serializing/desrializing should be attached to the
;clojure map using the meta key :avro-type

;in the below example, avroclj will look for the avro schema file:"avroclj/model/person/Person.avsc" in
;the class path to serialize/deserialize


(defn person [fields]
  (with-meta fields {:avro-type :avroclj.model.person.Person})
  )

;Serializes the map into a byte array
(def byteArray (avro/serialize (person {:first-name "John" :middle-name "Pradeep" :last-name "Vincent"})))


;Deserializes the bytes into the map passed as argument
(avro/deserialize byteArray (person {}))

avroclj/model/person/Person.avsc

{
  "namespace": "avroclj.model.person",
  "type" : "record",
  "name" : "Person",
  "doc" : "Represents a Person",
  "fields" : [
    {"name" : "firstName", "type" : "string"},
    {"name" : "middleName", "type" : "string"},
    {"name" : "lastName", "type" : "string"}
  ]
}

If you don't have a schema (avsc) file in class path, you can bind a schema dynamically during run time as shown.

(ns sample
  (:require [avroclj.avro :as avro]))

;Add a schema definition to avro clj which will be used when you call serialize/deserialize on map/byte array 
;with appropriate :avro-type meta data as shown in the below example.
(avro/add-schema {:namespace :avroclj.model.location
                      :name :Location
                      :type "record"
                      :doc "Represnts a Location"
                      :fields [
                                {:name "id"
                                 :type "string"
                                 }
                                {:name "name"
                                 :type "string"
                                 }
                                ]
                      })
                      
(defn location [fields]
  (with-meta fields {:avro-type :avroclj.model.location.Location})
  )

(def byteArray (avro/serialize (location {:name "India" :id "IND"})))

(avro/deserialize byteArray (location {}))

Artifact

avro-clj is published in clojars

Installation

With Leiningen:

[yehohanan7/avro-clj "0.3"]

With Maven:

<dependency>
  <groupId>yehohanan7</groupId>
  <artifactId>avro-clj</artifactId>
  <version>0.3</version>
</dependency>

avro-clj's People

Contributors

yehohanan7 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.