GithubHelp home page GithubHelp logo

isabella232 / scavro Goto Github PK

View Code? Open in Web Editor NEW

This project forked from braintree/scavro

0.0 0.0 0.0 115 KB

An SBT plugin for automatically calling Avro code generation and a thin scala wrapper for reading and writing Avro files

License: Apache License 2.0

Scala 98.06% Java 1.94%

scavro's Introduction

Scavro

Scavro is an SBT plugin for automatically calling Avro code generation and a thin scala wrapper for reading and writing Avro files.

The two components can work fully independently, so one can use the SBT plugin to automate Avro's Java code generation and use the default SpecificDatumWriter API supplied by Avro.

Scavro Plugin

The Scavro Plugin is an SBT plugin that automates calling Avro's code generation. To use, you must import the scavro library into your project's SBT settings by adding addSbtPlugin("com.oysterbooks" % "scavro_2.10" % "0.9.0") to your plugins.sbt file. Note that, whatever version of Scala you use in your project, SBT runs on 2.10. Avro schema and protocol files can then be added to your Build.scala or build.sbt file.

avroSchemaFiles := Seq(file("SchemaFile.avsc"))
avroProtocolFiles := Seq(file("ProtocolFile.avpr"))
avroIDLFiles := Seq(file("AvroIdlFile.avdl"))

Running sbt compile will then call the avro-tools compiler and generate java files into the directory specified by the avroCodeOutputDirectory SBT key.

Typically, although by no means necessarily, one would write a scala case class wrapper around the java class(es) generated in order to use the scala reader and writer described below.

A complete demonstration project is available as a reference.

# cd demo
# sbt run
[info] Running oyster.scavrodemo.ReadWriteDemo
{"name": "Plywood table top", "price": 13.95, "quantity": 1}
{"name": "2x4 raw lumber", "price": 2.95, "quantity": 2}
{"name": "Wood Screws -- 3in No. 10", "price": 0.065, "quantity": 30}
{"name": "Wood varnish (can)", "price": 3.95, "quantity": 1}
-------------------------
The order total comes to $25.75
[success] Total time: 1 s, completed Sep 2, 2015 12:32:41 PM

Scavro Reader and Writer

Scavro also provides a lightweight scala wrapper for Avro's read and write functionality through the AvroReader and AvroWriter classes. They can be used to serialize or deserialize a Seq of objects that implements the AvroSerializeable trait. Additionally, there must be an implicit instance of AvroMetadata to map the scala class to the code generated java class. This requirement can be met by using LineItem from the demo project as a boilerplate template.

case class LineItem(name: String, price: Double, quantity: Int) 
    extends AvroSerializeable {
  // Additional boilerplate omited
}

object LineItem {
  implicit def reader = new AvroReader[LineItem] { override type J = ... }
  implicit val metadata: AvroMetadata[LineItem, JLineItem] = 
    new AvroMetadata[LineItem, JLineItem] { ... }
  }
}

dataToWrite: Seq[LineItem] = ...

// Write an avro file
val writer = AvroWriter[LineItem](filename)
writer.write(dataToWrite)

// Read an avro file
val reader: AvroReader[LineItem] = AvroReader[LineItem]
val dataRead: Seq[LineItem] = reader.read(filename)

To utilize the Scavro runtime classes add the following to your build.sbt or Build.scala file. This dependency is independent of the addSbtPlugin command described in the Scavro Plugin section. Use that if you want the compile time code generation functionality, use this if you want the runtime functionality, and use both if you want both.

libraryDependencies ++= Seq(
  "com.oysterbooks" % "scavro_2.10_0.13" % "0.9.0" from
    "https://oss.sonatype.org/content/repositories/releases/com/oysterbooks/" +
      "scavro_2.10_0.13/0.9.0/scavro-0.9.0.jar",

The manual url specification is required because the runtime library and SBT plugin are distributed in a single package. This may change in version 1.0.

Contributing

Contributions are welcome. Submit pull requests to the master branch for core improvements and bug fixes. If you feel more ambitious, check out the code_generation feature branch.

The demo project is considered an integration test and must run correctly before pull requests will be accepted. Run stb test in both the project root directory and also in the demo directory.

Authors

Thanks for assistance:

... and the Oyster engineering team.

License

Copyright (C) 2015 Oyster

Licensed under the Apache 2.0 license. http://www.apache.org/licenses/LICENSE-2.0

scavro's People

Contributors

brianlondon 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.