ojai-scala-generics


License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.github.anicolaspp
ArtifactId

ArtifactId

ojai-scala-generics_2.11
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

ojai-scala-generics
ojai-scala-generics
Project URL

Project URL

https://github.com/anicolaspp/ojai-generics
Project Organization

Project Organization

com.github.anicolaspp
Source Code Management

Source Code Management

https://github.com/anicolaspp/ojai-generics

Download ojai-scala-generics_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.anicolaspp/ojai-scala-generics_2.11/ -->
<dependency>
    <groupId>com.github.anicolaspp</groupId>
    <artifactId>ojai-scala-generics_2.11</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.anicolaspp/ojai-scala-generics_2.11/
implementation 'com.github.anicolaspp:ojai-scala-generics_2.11:1.0.0'
// https://jarcasting.com/artifacts/com.github.anicolaspp/ojai-scala-generics_2.11/
implementation ("com.github.anicolaspp:ojai-scala-generics_2.11:1.0.0")
'com.github.anicolaspp:ojai-scala-generics_2.11:jar:1.0.0'
<dependency org="com.github.anicolaspp" name="ojai-scala-generics_2.11" rev="1.0.0">
  <artifact name="ojai-scala-generics_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.anicolaspp', module='ojai-scala-generics_2.11', version='1.0.0')
)
libraryDependencies += "com.github.anicolaspp" % "ojai-scala-generics_2.11" % "1.0.0"
[com.github.anicolaspp/ojai-scala-generics_2.11 "1.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8
org.scalactic : scalactic_2.11 jar 3.0.5
com.github.anicolaspp : ojai-testing_2.11 jar 1.0.3

provided (4)

Group / Artifact Type Version
org.ojai : ojai jar 3.0-mapr-1808
org.ojai : ojai-scala jar 3.0-mapr-1808
com.mapr.db » maprdb jar 6.1.0-mapr
xerces : xercesImpl jar 2.11.0

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.5

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.0.0