Code Monkey home page Code Monkey logo

ojai-generics's Introduction

ojai-generics

Maven Central

Thin, generic, scala idiomatic layer on top of MapR OJAI

ojai-generics presents a very thin layer on top of OJAI that easies working with OJAI from Scala by adding idiomatic Scala constructs.

QueryConditions Add-Ons

The OJAI API is written in Java and functions around QueryCondition use method overriding for different data types. In other words, in order to build a query, we need to know the exact type we are using at compile time, and while this seems like a good idea, it is far from convient in most occations.

Let's look at some examples to show our case.

Using ojai-generics

We can use ojai-generics to reduce the boilerplate code we are forced to write when using OJAI Java like API by using generics and idiomatic Scala.

Let's build an example from scratch so show in more details the advantages of using ojai-generics.

Suppose we want to create a QueryCondition for a value coming from a Spark DataFrame, normally coming as Any.

def buildEqualToCondition(field: String, value: Any)(implicit connection: Connection): QueryCondition = any match {
  case _: Int    => connection.newCondition().is(field, Op.EQUAL, value.asInstanceOf[Int]).build()
  case _: Double => connection.newCondition().is(field, Op.EQUAL, value.asInstanceOf[Double]).build()
  ....
}

def buildLessThanCondition(field: String, value: Any)(implicit connection: Connection): QueryCondition = any match {
  case _: Int    => connection.newCondition().is(field, Op.LESS_THAN, value.asInstanceOf[Int]).build()
  case _: Double => connection.newCondition().is(field, Op.LESS_THAN, value.asInstanceOf[Double]).build()
  ....
}

That really is a problem if we have many types and many operations, since the same we are doing for all types have to be done for all operations we have.

Using ojai-generics we can do this as follow.

def buildEqualToCondition(field: String, value: Any)(implicit connection: Connection): QueryCondition = 
  connection.newCondition().field(field) === value
  
def buildLessThanCondition(field: String, value: Any)(implicit connection: Connection): QueryCondition =
  connection.newCondition().field(field) < value

Notice that we have reduced the priously shown code by removing the pattern matching on the type. That means, we are using a generic API that is able to accept all possible types. Also, ojai-genercis takes care of the castings and convertions for us. In addition, it adds operators so we can think about these operations in a very natural way.

If we prefer a more Java like API, we can still do the following without loosing type safty and generics.

def buildEqualToCondition(field: String, value: Any)(implicit connection: Connection): QueryCondition = 
  connection.newCondition().equalTo(field, value)
  
def buildLessThanCondition(field: String, value: Any)(implicit connection: Connection): QueryCondition =
  connection.newCondition().lessThan(field, value)

These options are aliases that produce the same results as before just using a more verbose API.

Linking

Maven

<dependency>
  <groupId>com.github.anicolaspp</groupId>
  <artifactId>ojai-scala-generics_2.11</artifactId>
  <version>1.0.0</version>
</dependency>

SBT

libraryDependencies += "com.github.anicolaspp" % "ojai-scala-generics_2.11" % "1.0.0"

Important Notice

It is very important to notice, that we are only adding a thin layer on top of the existing OJAI API. Everything that works there will work while using our library. We only add extended functionality. We don't modify existing functionality in any way.

ojai-generics's People

Contributors

anicolaspp avatar

Stargazers

 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.