0

I want to generate an avro schema from a scala case class.

Suppose I have the following scala case class :

case class User(name : String, favorite_number: Int, favorite_color: String)

The related avro schema is :

{"namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": "int"},
     {"name": "favorite_color", "type": "string"}
 ]
}

Is there a way to generate at build time, the avro scehma ? using sbt for example ? I saw sbt-avro4s that permit to build a scala class from an avro schema, but I need to do the inverse.

Thanks in advance for your help

Driss NEJJAR
  • 872
  • 5
  • 22
  • 1
    You may write your own [**SBT _task _**](https://www.scala-sbt.org/1.x/docs/Tasks.html#Defining+a+Task) using [**Shapeless**](https://github.com/milessabin/shapeless) generic derivation, it is totally possible to do... but, it wont be easy at first because you will have to learn a lot - [this](https://underscore.io/books/shapeless-guide/) may help you to get started. **_Disclaimer:_** I had never done something like this before _(although I'm wanting to do soon)_, thus, I cannot help you - but I know this can be done that way. – Luis Miguel Mejía Suárez Jan 31 '19 at 15:25
  • Never done that before, do you have an example of it please ? – Driss NEJJAR Jan 31 '19 at 15:28
  • As I said, I have not done it either, so I don't have any toy example... However, I know the [**Circe-generic** _package_](https://github.com/circe/circe/tree/master/modules/generic/shared/src/main/scala/io/circe/generic) uses **Shapeless** to provide automatic derivation of _case classes_, but I am not sure that would be a good place to start. In the book they provide an [example of generic derivation of typeclasses](https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:generic) for product types using `HList` which is similar to what you want. – Luis Miguel Mejía Suárez Jan 31 '19 at 15:58
  • Thanks for your answer. I will try that. – Driss NEJJAR Jan 31 '19 at 17:57

1 Answers1

1

Avro4s contains the logic to generate an Avro Schema from a case class.

https://github.com/sksamuel/avro4s#schemas

Following the project example:

import com.sksamuel.avro4s.AvroSchema

case class User(name : String, favorite_number: Int, favorite_color: String)
val userSchema = AvroSchema[User]

Hopefully that provides enough to get started on a sbt task if needed.

Ken
  • 7,847
  • 1
  • 21
  • 20
  • You are right ! I have never written an sbt task. I will make some recherches and let you know. Thanks ! – Driss NEJJAR Jan 31 '19 at 22:47
  • 1
    How is it possible to store the avsc schema, by adding an sbt task ? I only found simple examples with hello world or something like that. Is it possible to do it in build.sbt ? – Driss NEJJAR Jan 31 '19 at 23:21