Tuesday, 11 June 2019

Cassandra Database Setup

Cassandra Database Setup

Cassandra DB can be set up easily by using the docker image. If you do not have docker installed in your machine, please do it and follow the steps.

 #Pull the docker image
docker pull cassandra
 #Run the docker image in a container
docker run -p 9042:9042 --name my-cassandra -d cassandra:latest

 #Stop the docker container
docker stop my-cassandra
 #Start the docker container
docker start my-cassandra
 #Stop & remove the container
docker stop my-cassandra
docker rm my-cassandra

The docker exec command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your cassandra container.

docker exec -it my-cassandra bash  

Now you your inside the container. Type cqlsh which will allow you enter into the cassandra query language shell Where you can execute the below queries.

 #This will list the available keyspaces
describe keyspaces;

 #Create a new namespace with the name arun_keyspace.
CREATE KEYSPACE arun_keyspace
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

 #If list the keyspaces again, you can see newly created keyspace arun_keyspace
describe keyspaces;
 #Command to Use the keyspace to create/update & manage tables
use arun_keyspace;


 #Create a new table, products
create table products (
          productid uuid ,
          productname text,
          productdescription text,
          PRIMARY KEY(productid)
      );

 #Insert
insert into products(productid, productname, productdescription)
values (now() , 'Fitbit' , 'Fitbit versa');

 #Select the table
select * from products;


Thursday, 23 May 2019

PostgreSQL Overview

Setting up the server

To get start with postgreSQL, download the Postgres.app & pgadmin 4(for macOS).
Postgres.app is a simple, native macOS app that runs in the menubar without the need of an installer.
pgAdmin 4 is a feature rich open source PostgreSQL client.

Open Postgres.app > Click on Start - This will start the postgres server

Open pgadmin 4 - This will start the server & opens http://127.0.0.1:56791/browser/ in the browser.
Right Click on server > Create > Server > Fill the details as below & Save,



You will be connected to the server which was started by Postgres.app. You will see the below hierarchy on the right side.

Right click on Databases > Create > Database > enter any name for database (ex : employee_db)
Select Tools > Query Tool - This will open a Query Editor.

Hands on

Run the below queries one by one and watch what it does,
create table employee(employee_id integer not null,
   employee_name text not null,
   education_qualification text not null,
   gender varchar(6) not null,
   employee_role text not null,
   payload text);
      
--- INSERTING VALUES ---
insert into employee
values('1001', 'Arun Velusamy', 'Engineering', 'male', 'application developer');

insert into employee
values('1002','Ashok Subburaj', 'Engineering', 'male', 'application developer');

insert into employee
values('1003','Lakshmi Velan', 'PHD', 'female', 'Lead Engineer');

insert into employee
values('1004','Veerammal Ramasamy', 'MBA', 'female', 'Product designer');

insert into employee
values('1005','Gautham Kumarasamy', 'Not Available', 'male', 'Cheif Executive');

--- SELECT THE EMPLOYEE TABLE ---
select * from employee;


select to_jsonb(emp)
from
 (
  select employee_id,employee_name,education_qualification,gender,employee_role
  from employee
 ) emp;

--- UPDATE THE PAYLOAD ---
update employee
set payload = 
 (
  select to_jsonb(emp)
  from (
   select employee_id,employee_name,education_qualification,gender,employee_role
   from employee
  ) emp
where employee.employee_id = emp.employee_id);

--- SELECT THE EMPLOYEE TABLE ---
select * from employee;



--- ALTER TABLE TO DROP 2 COLUMNS ---
alter table employee
drop column education_qualification;

alter table employee
drop column gender;

select * from employee;

--- SELECT VALUES FROM JSON PAYLOAD COLUMN ---
select employee.employee_id, employee.employee_name, employee.employee_role,
 emp_payload.education_qualification, emp_payload.gender
from employee, jsonb_to_record(employee.payload::jsonb) as emp_payload (
 education_qualification text,
 gender varchar(6)
);

Saturday, 11 May 2019

Openshift


Openshift is the platform offered as software as service. It is developed by Redhat. It is built around Docker containers orchestrated and managed by kubernetes. Openshift automates the build, deployment and management of application.

Core Concepts of Openshift:

  1. Containers and images
  2. Pods and Users
  3. Projects and Users
  4. Builds and Image Streams
  5. Deployments
  6. Routes
  7. Templates
  8. Orchestration

Containers

(basic units of openshift)
Containers aims to create portable software which should run from local environment to production in a same way.
Containers wrap the software in a customised operating system. This minimizes the chance of saying that application works in local & not working in UAT or works in non-prod & not working in production
So containers are actually a light weight operating system which host one or more software applications. But typically each container will host only one application which we used to call "Micro Service"


Images

It is a binary includes all requirements to run a single container. Metadata describing its needs and capabilities. You can think of war/jar packaging.
Containers are create from images that specify their precise contents. Image is a definition of the container where it used to run.
On a simple note, image is just a war/jar + Some Infra details(like os details, jdk version etc)


Orchestration

It is about how you manage the containers & automate the management. Below are the few process to manage the containers
Spin up the number of containers from three to five & scale down to two. You can spin up five containers on five five different hosts.
Zero down time deployments. Spin up the containers running the new code. Once it is fully up, redirect the user traffic from old container to this new container. So it is zero or near to zero down time deployment.


Sunday, 16 April 2017

Installing NOOBS in SD card

To use Raspberry device, we need NOOBS boot loader to install any IoT Operating system such as Raspbian or Ubuntu mate.

Following are the steps to install the NOOBS in SD card.

1. Insert an SD card that is 4GB or greater in size into your computer.
(Preferred to use SD card adapter or SD Card reader).

2. Format the SD card using the platform-specific instructions below:  (Formatting tool might have only less than 500kb.)
   a. Windows
      i. Download the SD Association's Formatting Tool from SDFormatter windows
      ii. Install and run the Formatting Tool on your machine
      iii. Set "FORMAT SIZE ADJUSTMENT" option to "ON" in the "Options" menu
      iv. Check that the SD card you inserted matches the one selected by the Tool
      v. Click the "Format" button
   b. Mac
      i. Download the SD Association's Formatting Tool from SDFormatter Mac
      ii. Install and run the Formatting Tool on your machine
      iii. Select "Overwrite Format"
      iv. Check that the SD card you inserted matches the one selected by the Tool
      v. Click the "Format" button
   c. Linux
      i. We recommend using gparted (or the command line version parted)
      ii. Format the entire disk as FAT

3. Extract the files contained in this NOOBS zip file.
(You can download NOOBS.zip from the following link.
  NOOBS Latest )

4. Copy the extracted files onto the SD card that you just formatted so that this file is at the root directory of the SD card. Please note that in some cases it may extract the files into a folder, if this is the case then please copy across the files from inside the folder rather than the folder itself.
(You can find these instructions in readme.txt in extracted files)

5. Insert the SD card into your Pi and connect the power supply.


Post Installation instructions :



Your Pi will now boot into NOOBS and should display a list of operating systems that you can choose to install.
(You need to connect to internet to download the Operating system by NOOBS)
If your display remains blank, you should select the correct output mode for your display by pressing one of the following number keys on your keyboard:

1. HDMI mode - this is the default display mode.
2. HDMI safe mode - select this mode if you are using the HDMI connector and cannot see anything on screen when the Pi has booted.
3. Composite PAL mode - select either this mode or composite NTSC mode if you are using the composite RCA video connector.
4. Composite NTSC mode

Thursday, 1 December 2016

MongoDB with Spring framework

As we know, MongoDB is a no-SQL database. Before looking at the example of MongoDB with spring framework, we need to know few things about it.
We can go for mongodb instead of relation databases, when your data is sort of document data which means you just need to open the document, do the changes and save it again, it doesn’t have any relationship among them. we can also have the mongodb when we want to compute user data before accessing it and having it ready to read be the user at anytime which we use to call precomputation.

Before seeing an example, we’ll see how to set up mongodb, 

Command to start the server : 
<mongodb_Home_directory>/bin/mongod --dbpath <mogodb_workspace> &
Default port : 27017
DB client, we used here : Robomongo (version : 0.8.5)

Connected the mongodb server from this Robomongo and created a database named Mine. Inside the database, we can see Collections, Functions and Users. I have create my own collection named studentdocs.

Now we used these connection details and the collection in the below configuration bean. It extended the AbstractMongoConfiguration class from spring and Overrode the methods getDatabaseName and mongo which returns the Database name and the MongoClient respectively. MongoClient object was created from the hostname 127.0.0.1 and port 27017. So now, spring can take care of creating connection and managing it.
@Configuration
public class SpringMongoDBConfig extends AbstractMongoConfiguration {
    @Override
    public String getDatabaseName() {
        return "Mine";
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient("127.0.0.1",27017);
    }
}
Created a Student class and mapped it with the collection studentdocs we created earlier. 
@Document(collection = "studentdocs")
public class Student implements Serializable {
    @Id
    private String studentDocId;
    private String studentId;
    private String studentName;
    private String dob;
    private String contactNo;
    private Address address;
    private StudentCourse studentCourse;
    private Set<MonthAttendance> monthAttendances;
    private Set<Subject> subjects;
//accessors and mutators are here }
Now we had the connection and collection class. We’ll see how to fetch the student records by the contactNo and by the courseId. For this, we created StudentRepository interface which extends another interface CrudRepository of spring framework.
public interface StudentRepository extends CrudRepository<Student, String> {     Iterable<Student> findByContactNo(String contactNo);     Iterable<Student> findByStudentCourseCourseId(String courseId); }
The first method findByContactNo will fetch the student objects which has the contactNo which we pass and return the same. Note that the second method findByStudentCourseCourseId will fetch the student object which has the courseId which we pass. Here, courseId is the property of studentCourse.
CrudRepository has some default methods like save, delete, findAll and etc. We can also use these methods.
Spring framework has the implementation for all these methods. Please see the below service class where we used all these methods.
@Service public class StudentServiceImpl implements StudentService {     @Autowired     private StudentRepository studentRepository;     public Map<String, Iterable<Student>> getStudentsByContactNo(String contactNo) {         Map<String, Iterable<Student>> studentData = new HashMap<String, Iterable<Student>>();         Iterable<Student> iStudent = studentRepository.findByContactNo(contactNo);         studentData.put(contactNo,iStudent);         return studentData;     }     public Map<String, Iterable<Student>> getStudentsByCourseId(String courseId) {         Map<String, Iterable<Student>> studentData = new HashMap<String, Iterable<Student>>();         Iterable<Student> iStudent = studentRepository.findByStudentCourseCourseId(courseId);         studentData.put(courseId,iStudent);         return studentData;     }     public boolean saveContactsProducts(List<Student> students) {         Iterable<Student> resultUserProducts = studentRepository.save(students);         if(null != resultUserProducts)             return true;         return false;     } }
Click here to download this project from GitHub.

Sunday, 27 November 2016

Communication between Backbone Views

There are few ways to communicate between two backbone views and model.
Consider an application contains Menu view and you want to update the application area based on the menu selection.
For example, consider an e-commerce application which contains the following views.
1. MenuView
2. Recharge View
3. Shopping View
4. Book Ticket View

For this the very basic approach or the easiest way is that creating the object for all other three views in MenuView. But by doing this, all views are tightly coupled with one another.
To overcome this we’re going to use Event aggregator pattern. 
Backbone provides Router concept for event aggregator.

In index.html we’ve included all the html templates.

index.html


 


After that, related Backbone view files are created with the basic information.

MenuView.js

var MenuView = Backbone.View.extend({
 
 model : new MenuModel(),
 initialize : function(){
  
 },
 events : {
  
 },
 render : function(){
  var menuTemplate = _.template($('#menu-template').html());
  this.$el.html(menuTemplate(this.model.toJSON()));
  return this;
 }
});


RCView.js

var RCView = Backbone.View.extend({
 el: '#contentArea',
 initialize : function(){
  
 },
 
 events : {
  'click #rcSubmitButton':'doOnRCSubmitButton'
 },
 render : function(){
  var rcTemplate = _.template($('#recharge-template').html());
  this.$el.html(rcTemplate(null));
  return this;
 },
 
 doOnRCSubmitButton : function(){
  alert("RC click!!");
 }
});
As of now, views are containing only the render part which renders the corresponding template into the application area.

We’ve defined three links in the index.html menu area. For every menu item, there are independent actions. The event should be passed to corresponding views without any dependency. For this purpose we’ve created the router in the main.js file. Once the routes and its functions are defined, for all the menu selection it’ll redirected to its route.

main.js

$(document).ready(function(){
 var menuView = new MenuView();
 menuView.setElement($('#menuArea'));
 menuView.render();
 new MyRouter();
 Backbone.history.start();
});

var MyRouter = Backbone.Router.extend({
 routes : {
  "recharge":"onRecharge",
  "bookticket" : "onBookTicket",
  "shop":"whileShopping"
 },

 viewFactory : function(){
  return{
   rcView : typeof rcView == 'undefined' ? new RCView() : rcView,
   ticketView : typeof ticketView == 'undefined' ? new TicketView() : ticketView,
   shopView : typeof shopView == 'undefined' ? new ShopView() : shopView
  };
  
 },
 onRecharge : function(){
  this.viewFactory().rcView.render();
 },
 
 onBookTicket : function(){
  this.viewFactory().ticketView.render();
 },
 
 whileShopping : function(){
  this.viewFactory().shopView.render();
 }
});



You can download this working sample in this Github URL.

Reference for event aggregator pattern :  http://martinfowler.com/eaaDev/EventAggregator.html

Wednesday, 16 November 2016

Spring Singleton vs Prototype

                            Bean scopes will be specified by the scope attribute of the <bean/> element. We will see the example as we go down to this article. Spring bean has 5 different scopes which we listed below,
  1. Singleton
  2. Prototype 
  3. Global
  4. Session
  5. Application
When it comes to standalone application, we don't have the last three scopes that are applicable only for web applications. Here, In this article, we'll just look at the first two, singleton and the prototype.

                            Singleton beans are created by the spring container only once which means you will have only one instance of the bean through out our application. Prototype beans are right opposite to this. It's a multiton bean which means more than one instance can be created. When we call the getBean method of ApplicationContext or inject in other beans, we will get a new instance of it. Let's see this by an example,

ApplicationContext.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

   <bean id="student" class="com.studentact.model.Student" scope="prototype" destroy-method="destroy" init-method="init"/>  

   <bean id="studentDao" class="com.studentact.dao.StudentDaoImpl" destroy-method="destroy" init-method="init">  
     <property name="student" ref="student"/>  
   </bean>  

   <bean id="studentServiceProxy" class="com.studentact.proxy.StudentServiceProxy" init-method="init" destroy-method="destroy">  
     <property name="student" ref="student"/>  
   </bean>  

   <bean id="studentService" class="com.studentact.service.StudentService" destroy-method="destroy" init-method="init">  
      <property name="studentServiceProxy" ref="studentServiceProxy"/>  
      <property name="studentDao" ref="studentDao"/>  
   </bean>  

 </beans>  

We can see that the scope of student bean is specified as prototype and other beans don't have this scope attribute which means they are all singleton.
Let's say StudentServiceProxy's method getVOCTopStudent gets student data from some web service and assign them in student object. Likewise StudentDaoImpl's method getGovtStudent gets the from DB and assign them in student object. StudentService will call these methods and store the return student object in a list as below,

StudentService.java
 public class StudentService implements IStudentService {  
      private IStudentDao studentDao;  
      private IStudentServiceProxy studentServiceProxy;  
      public IStudentDao getStudentDao() {  
           return studentDao;  
      }  
      public void setStudentDao(IStudentDao studentDao) {  
           this.studentDao = studentDao;  
      }  
      public IStudentServiceProxy getStudentServiceProxy() {  
           return studentServiceProxy;  
      }  
      public void setStudentServiceProxy(IStudentServiceProxy studentServiceProxy) {  
           this.studentServiceProxy = studentServiceProxy;  
      }  
      @Override  
      public List<Student> getTopStudents() {  
           List<Student> topStudents = new ArrayList<Student>();  
           topStudents.add(studentDao.getGovtStudent());  
           topStudents.add(studentServiceProxy.getVOCTopStudent());  
           return topStudents;  
      }  
      public void destroy(){  
           System.out.println("detroy method of StudentService");  
      }  
      public void init(){  
           System.out.println("init method of StudentService");  
      }  
      //Other operations go here  
 }  

We can use the below code to test whether the list has same student object or different objects.
 StudentService studentService = (StudentService) applicationContext.getBean("studentService");  
 List<Student> students = studentService.getTopStudents();  
 System.out.println(students);  
 Student aStudent = null;  
 for(Student student:students){  
      if(null == aStudent){  
           aStudent = student;                      
      } else if(student == aStudent) {  
           Assert.assertFalse(true);  
      }  
 }  
We mentioned student bean scope as prototype and injected it in other beans like studentDao and studentServiceProxy. So the resultant list will have different student objects. If it’s a singleton, then both studentDao and studentServiceProxy will have the same object and the resultant list will have the same student objects.

Initialization and destruction:
                            If you assign any method to init-method attribute of bean element, the method will be called after the bean properties is set, but before the bean is completely initialized. We can use this, when we need to initialize something like opening file, creating or checking the database connections. This method will be called by spring container regardless of singleton or prototype beans. 
                            But destroy-method will be called by the spring container, only if the bean is singleton. It will never be called in case of the bean scope is prototype. The methods which we assign to this destroy-method attribute will be called by the spring container, before the object is destroyed. But we have to use this AbstractApplicationContext interface and call the registerShutdownHook method as we used in below test class,
 public class StudentServiceTest extends BaseTest {  
      protected AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");  
      @Test  
      public void testGetTopStudents() {  
           applicationContext.registerShutdownHook();  
           StudentService studentService = (StudentService) applicationContext.getBean("studentService");  
           List<Student> students = studentService.getTopStudents();  
           System.out.println(students);  
           Student aStudent = null;  
           for(Student student:students){  
                if(null == aStudent){  
                     aStudent = student;                      
                } else if(student == aStudent) {  
                     Assert.assertFalse(true);  
                }  
           }  
      }  
 }  
                            The methods we use as init-method and destroy-method should not accept any arguments, but can be defined to throw exceptions. We can define initialization and destruction methods in all beans with same method signature and can specify it in default-init-method and default-destroy-method of beans elements, instead of specifying it in all the bean elements. Also please have a look at other beans attribute like default-autowire-candidates and default-lazy-init.

                            Singleton bean objects will be created when application context is loaded. Though it’s not used anywhere. Prototype scoped bean objects will always be created when it’s needed. But if we want singleton objects to be created when it’s needed, we can use the lazy-init attribute of bean element. 

Please do more examples like singleton-scoped bean has prototype-scoped beans and vice versa.

Click here to download this example project from GitHub.