Neo4j With Scala: Rest End Points with AKKA HTTP


Hello Folks,

Welcome back again in the series of Neo4j with Scala ūüėČ. Let‚Äôs start our journey again. But before starting our journey here is the links for recap who join late us in the journey :

  1. Getting Started Neo4j with Scala : An Introduction
  2. Neo4j with Scala: Defining User Defined Procedures and APOC
  3. Neo4j with Scala: Migrate Data From Other Database to Neo4j
  4. Neo4j with Scala: Awesome Experience with Spark
  5. Neo4j vs ElasticSearch and Full Text Search

Today we will discuss about Neo4j, Scala with Akka-HTTP. We will create rest-endpoints and try to connect with server with them.

The basic idea behind using Akka-HTTP for creating Rest end points is modules implement a full server and client-side HTTP stack on top of akka-actor and akka-stream. We can customize these routes according to our use case. It’s not a web-framework but rather a more general toolkit for providing and consuming HTTP-based services.

We will talk step by step to development this Rest API after which you will able to run this API on your server and start hitting Rest end points.

Now we start coding :

  1. build.sbt:
    We will start our implementation with the build.sbt file. We will keep all require dependencies in this file.
  2. Factories:
    This is the part where we interact with the Neo4j database. This code shows how to do CRUD operations and Create Relation between the nodes. We will define our Cypher here and execute them. Example:

     def retrieveRecord(email: String): Option[User] = {
        val driver = GraphDatabase.driver(neo4jUrl, AuthTokens.basic(userName, userPassword))
        val session = driver.session
        val script = s"MATCH (a:Users) WHERE a.name = '$email' RETURN a.name AS name, a.email AS " +
                     s"email, a.age AS age, a.city AS city"
        val result = session.run(script)
        val record_data: Option[User] = if (result.hasNext()) {
          val record = result.next()
          val results: User = new User(record.get("name").asString(),
            record.get("email").asString(),
            record.get("age").asInt(),
            record.get("city").asString())
          Some(results)
        } else {
          None
        }
        session.close()
        driver.close()
        record_data
      }

    Continue reading

Posted in akka-http, Scala, scalatest | Tagged , , | 1 Comment

Object Oriented JavaScript: Polymorphism with examples


oopjsAgain this is not the advance topic of JavaScript but it relies under Object Oriented JavaScript & polymorphism is one of the tenets of Object Oriented Programming (OOP), we all know what is Polymorphism from other languages (like C#, Java etc) but we always think when to use it, why to use it and how to this, and most of us are still in confusion that should we really use it? blah !!!.

JavaScript is dynamically typed language [a big issue to discuss], but for understanding purpose:

Statically typed programming languages do type checking (the process of verifying and enforcing the constraints of types) at compile-time as opposed to run-time. (Java, C etc)

Dynamically typed programming languages do type checking at run-time as opposed to Compile-time. (JavaScript etc)

Though in JavaScript it is a bit more difficult to see the effects of polymorphism because the more classical types of polymorphism are more evident in statically-typed systems.

 

Continue reading

Posted in JavaScript, Scala | Tagged , , , , | 2 Comments

AWS | Running micro-services on Amazon ECS


Hi all,

Knoldus had organized half an hour session on 27th January 2017 at 4:00 PM. Topic was Walk-through on Amazon ECS. Many enthusiasts joined and learned from the session. Below are slide and video from the session. Please let me know if you have any questions/concerns.

Slide:

Video :

Thanks !! ūüôā


KNOLDUS-advt-sticker

Posted in Amazon, Amazon EC2, AWS, AWS Services, Scala | Leave a comment

Spark Cassandra Connector On Spark-Shell


Using Spark-Cassandra-Connector on Spark Shell

Hi All , In this blog we will see how we can execute our spark code on spark shell using Cassandra . This is very efficient at testing or learning time , where we have to execute our code on spark shell rather than doing on any IDE .

Here we will use spark version –¬† 1.6.2¬†

you can download the version from Here

and off course its appropriate spark Cassandra connector as

Cassandra Connector –¬† spark-cassandra-connector_2.10-1.6.2.jar .

you can download the connector(jar file) from Here .

So lets Begin : –

Step 1 ) Create any test table in your Cassandra ( I am using Cassandra version Cassandra 3.0.10 ) .

CREATE TABLE test_smack.movies_by_actor (
actor text,
release_year int,
movie_id uuid,
genres set,
rating float,
title text,
PRIMARY KEY (actor, release_year, movie_id)
) WITH CLUSTERING ORDER BY (release_year DESC, movie_id ASC)

Continue reading

Posted in apache spark, big data, Cassandra, Scala, Spark | 2 Comments

JWT Authentication with Play Framework


In this blog, I will demonstrate how to implement JWT Authentication with Play Framework.

JSON Web Token (JWT) is a compact, self-contained which means securely transfer the information between two parties. It can be sent via Post request or inside the HTTP header. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret or a public/private key pair using RSA.  It consists three parts separated by the dot(.)  i.e Header, Payload and Signature. The header consists type of the token and hashing algorithm, the payload contains the claims and the claims in a JWT are encoded as a JSON object that is used as the payload or as the plaintext, the signature part is used to verify that the sender of the JWT.

Here, we are using JSON Web Token for authentication with Play Framework, This is a most common way of using JWT. Once the user is logged in, we need to include JWT with each request that allows the user to access the routes, service and resources which authenticate the token in order to permit the request.

Let’s start with the implementation of¬†JWT Authentication using Play¬†Framework:

1. First, we need to include JWT library in our project. There are several libraries for JWT, you can use any of them depending on your requirement, here I am using one of the library i.e.

"com.jason-goodwin" %% "authentikat-jwt" % "0.4.1"

2. Next, you need to create JWT utility class in which you need to add all these methods for creating, verifying and decode the JWT token and provide the secret key and algorithm such as HMAC, SHA256.

val JwtSecretKey = "secretKey"
val JwtSecretAlgo = "HS256"

def createToken(payload: String): String = {
  val header = JwtHeader(JwtSecretAlgo)
  val claimsSet = JwtClaimsSet(payload)

  JsonWebToken(header, claimsSet, JwtSecretKey)
}

def isValidToken(jwtToken: String): Boolean =
  JsonWebToken.validate(jwtToken, JwtSecretKey)

def decodePayload(jwtToken: String): Option[String] =
  jwtToken match {
    case JsonWebToken(header, claimsSet, signature) => Option(claimsSet.asJsonString)
    case _                                          => None
  }

3. To implement JWT authentication, we need to create reusable custom secured action by using ActionBulder that authenticates the each subsequent request and verifies the JWT in order to permit the request to access the corresponding service. ActionBulder is the special case of functions that take request as input and thus can build actions and provides several factory methods that help for creating action. To implement ActionBuilder, we need to implement invokeBlock method. So here I have created a custom JWTAuthentication by using ActionBuilder.

case class User(email: String, userId: String) 
case class UserRequest[A](user: User, request: Request[A]) extends WrappedRequest(request)

object JWTAuthentication extends ActionBuilder[UserRequest] {
  def invokeBlock[A](request: Request[A], block: (UserRequest[A]) => Future[Result]): Future[Result] = {
    implicit val req = request

    val jwtToken = request.headers.get("jw_token").getOrElse("")

    if (JwtUtility.isValidToken(jwtToken)) {
      JwtUtility.decodePayload(jwtToken).fold(
        Future.successful(Unauthorized("Invalid credential"))
      ) { payload =>
        val userInfo = Json.parse(payload).validate[User].get

        // Replace this block with data source
        if (userInfo.email == "test@example.com" && userInfo.userId == "userId123") {
          Future.successful(Ok("Authorization successful"))
        } else {
          Future.successful(Unauthorized("Invalid credential"))
        }
      }
    }
    else {
      Future.successful(Unauthorized("Invalid credential"))
    }
  }
}

4. Now we can use “JWTAuthentication” the same way we use Action.

def index = JWTAuthentication { implicit request =>
  Ok(views.html.index("Hello world"))
}

5.¬†¬†You can test it through Postman(on which you can send the request and view response),¬† all we need to do is create a JWT and pass it to the Headers, corresponding to the field “jw_token”. You can create JSON web token by “createToken”¬†method that I have shown you in step 2 by passing payload as a parameter which can be anything, here is the example:¬†

val payload = """{"email":"test@example.com","userId":"userId123"}"""

JWT looks like this:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAZXhhbXBsZS5jb20iLCJ1c2VySWQiOiJ1c2VySWQxMjMifQ.mjMQN8m_wH1NSE9GGexCW_GUh8uruNco18jgt7AWuO4

jwt.png

You can get the source code from here.

I hope this blog is helpful to you!

Thanks 

References:
 

KNOLDUS-advt-sticker

Posted in JWT, Play 2.4.X, Play Framework, Scala | Tagged , , , | Leave a comment

Building Microservices Based Enterprise Applications in Java Using Lagom – Part I


As we know, now days, most of the enterprise applications design as a “Microservices Architecture” because of scalability, sharding, loosely-coupling and many others reasons are there. On the other hand ¬†JavaEE help us for building an Enterprise Applications. As we know, Java help us for building a good applications, but with JavaEE monolithic approach our applications are not scalable as compare to microservices.

That’s why, Lagom comes into picture. Lagom provides a way for building an Enterprise Application with design of Microservices¬†based architecture and also give us Responsive, Resilient, Elastic and Message Driven¬†features or in other words a Reactive Approach. Lagom design philosophy as :

  1. Asynchronous.
  2. Distributed Persistent.
  3. Developer Productivity.

In this blog, we are building a sample application, for managing user module and performing CRUD on user.

Note: Lagom gives us strict approach for designing applications with Domain Driven Design (DDD) manner and also follow CQRS for event sourcing.

Step I:

We are building a maven based project using Lagom and our project structure is as below:

 lagom-structure

Continue reading

Posted in Akka, Java, Microservices, Scala | Tagged , , , , , , , , , , | 2 Comments

Event Sourcing with Eventuate


Hi all,

Knoldus had organized an hours session on 3rd February 2017 at 4:00 PM. Topic was Event Sourcing with Eventuate. Many enthusiasts  joined and learned from the session. I am  sharing the slides of the session here. Please let me know if you have any question related to linked slides.

 

 

you can also watch the video on youtube :

 

You can find complete code here.

 

Thanks … !!!

KNOLDUS-advt-sticker

Posted in akka-http, Cassandra, Scala | Tagged , | 2 Comments

Introduction to Structured Streaming


Hello!!  Knoldus had organized half an hour session on Structured Streaming briefing about the API changes, how it is different from the early Stream Computation paradigm (DStreams) and example API demonstration. Hope you will enjoy. Below are the slides and Video from the session.

Slide:

Video:


KNOLDUS-advt-sticker

Posted in apache spark, Scala, Spark, Streaming | 1 Comment

Introduction To Quasiqoutes


Hi all,

Knoldus had organized an hours session on 16th December 2016 at 4:00 PM. Topic was Introduction to Quasiqoutes. Many enthusiasts  joined and learned from the session. I am  sharing the slides of the session here. Please let me know if you have any question related to linked slides.


KNOLDUS-advt-sticker

Posted in Scala | Leave a comment

An Alternative to Akka Quartz: Time & TimeZone(IST and others) based jobs using Akka Scheduler


In this blog, we will be scheduling jobs based on time using Akka scheduler. The jobs will be scheduled based on IST(Indian Standard Time). It is basically an alternative to Quartz Scheduler to some extent which is also used to schedule the time-based job. We can schedule jobs at any point of time i.e schedule jobs based on IST or any other TimeZone like UTC

Let’s say, If you want your job to be scheduled at 9:00:00 AM IST every day, What would you do?. Scheduling such a job using the normal Akka scheduler is not easy. So what I have done is, I have
written an extra function so that our normal Akka scheduler acts as a Quartz Scheduler but only ‚ÄėTime Based ‚Äô not Calendar based. With the example I am going to share you will be able to schedule time-based jobs for almost any TimeZone, you just need to mention the timezone. In this example, I am focusing on IST(Indian Standard Time) but you can tweak it.

Example
Send an E-mail every day at 09:00:00 AM every day.

Akka quartz is one way, but If you can play around with normal Akka scheduler why mess around with Akka quartz.

Alright Let’s head over to the code and see How can we accomplish it.

Here is the Schedule Actor Class

import akka.actor.Actor

class ScheduleActor extends Actor {

  import ScheduleActor._

  var count = 1
//I am using var over here because it's a simple example to increment count. However you should prefer immutability wherever possible.
  def receive: PartialFunction[Any, Unit] = {
    case IncrementNumber => count+=1
    println(count)
  }

}

/**
 * Created by deepak on 22/1/17.
 */
object ScheduleActor {

  case object IncrementNumber

}

Here is the implementation for the job.

import java.text.SimpleDateFormat
import java.util.{Date, TimeZone}

import scala.concurrent.duration._

import ScheduleActor.IncrementNumber
import akka.actor.{ActorSystem, Props}

/**
 * Created by deepak on 22/1/17.
 */
object ScheduleJob extends App {

  val system = ActorSystem("SchedulerSystem")
  val schedulerActor = system.actorOf(Props(classOf[ScheduleActor]),
 "Actor")
  implicit val ec = system.dispatcher
  system.scheduler
    .schedule(calculateInitialDelay().milliseconds, 60.seconds)(
      schedulerActor ! IncrementNumber)
//the first argument in the schedule function is the initial delay 
//the second argument in the schedule function is the interval
  def calculateInitialDelay(): Long = {
    val now = new Date()
    val sdf = new SimpleDateFormat("HH:mm:ss")
    sdf.setTimeZone(TimeZone.getTimeZone("IST"))
    val time1 = sdf.format(now)
    val time2 = "00:00:00" //this is where we provide the time(IST)
 for example I want the job scheduled at 9PM IST I would replace 00:00:00
 with 21:00:00 
    val format = new SimpleDateFormat("HH:mm:ss")
    val date1 = format.parse(time1)
    val date2 = format.parse(time2)
    val timeDifference = date2.getTime() - date1.getTime()
    val calculatedTime = if (timeDifference < 0) 
(Constant.DAYHOURS + timeDifference) else timeDifference
    // val modifiedDate = projectDbService.getModifiedDate("sumit")
    calculatedTime
  }
//Calculate initial delay method basically triggers the job at the IST 
time provided above.
}

That’s all, you are all set to schedule time-based jobs without using Quartz Scheduler.

Full code is available on github
If you find any challenge, Do let me know in the comments.
If you enjoyed this post, I’d be very grateful if you’d help it spread.Keep smiling, Keep coding! Cheers!


KNOLDUS-advt-sticker

Posted in Scala | Leave a comment