Sunday, October 22, 2017

The Vertx of it all


What is Vert.X

In a single sentence, Vert.X is an event based application framework running on JVM.

Why Vert.X
1. Support for multiple languages
2. Simple asynchronous model
3. Event Bus
4. No multithreading nightmares
5. No Http overhead

Simple Example ( Java )

// The Server Initialization

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CookieHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.LocalSessionStore;
import io.vertx.ext.web.sstore.SessionStore;

public final class VertxExample extends AbstractVerticle {

public static void main(String[] args) throws Exception {
                // Set the server start parameters VertxOptions options = new VertxOptions().setClustered(false);

                // Create instance of vertx with the set options
    Vertx vertx = Vertx.vertx(options);
                
                // Deploy the verticle
    vertx.deployVerticle(new VertxExample());
}

@Override
public void start() throws Exception {

// Set the vertx router with the serving context
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());

                // Acknowledge end points with the handlers for the same
router.post("/myEndPoint").handler(new MyEndPointHandler());

                // Start the http server
vertx.createHttpServer().requestHandler(router::accept).listen(8080);

}
}

// The Handler

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import com.softcede.quizwe.controller.UserManager;
import com.softcede.quizwe.util.Constants;
import com.softcede.quizwe.vo.UserVO;
import com.softcede.quizwe.worker.user.Authenticator;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;


public final class MyEndPointHandler implements Handler {

public MyEndPointHandler() {
}

@Override
public void handle(RoutingContext routingContext) {

HttpServerResponse response = routingContext.response();
HttpServerRequest request = routingContext.request();

try {

String reqUrl = request.uri();

// Get the request parameters
String requestJsonStr = request.getParam("requestJson");


// Do something with the request
// ...

                        // Send the response
response.putHeader("content-type", "application/json")
                                 .setStatusCode(200)
                                 .end(response);

} catch ( Exception e ) {
e.printStackTrace();
}
}
}



Thursday, May 18, 2017

Loading Postgres data into Spark



When loading data from postgres in Spark 2.11, I got the below exception. 
java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/scala/DefaultScalaModule$

All you need to fix is to add the following dependency.

   com.fasterxml.jackson.module
   jackson-module-scala_2.11
   2.4.5-1

Hope it helps.


Tuesday, March 7, 2017

Creating ReadOnly user in Postgres



Very simple but easily forgotten. :)

1. Grant permissions to connect to your database
GRANT CONNECT ON DATABASE my_db TO my_user;
2. Grant permission to use the schema that you want to
GRANT USAGE ON SCHEMA public TO my_user;
GRANT USAGE ON SCHEMA my_schema TO my_user;
3. Grant only select for the user
GRANT SELECT ON ALL TABLES IN SCHEMA public TO my_user;
GRANT SELECT ON ALL TABLES IN SCHEMA my_schema TO my_user;
4. Sometimes it might be necessary to run this also
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO my_user;
Cheers.