Install and Uninstall mysql server and workbench in Ubuntu(linux)

If you have a problem in mysql server to set new password and anything just backup your current database schema and run following commands in terminal to uninstall and install again in ubuntu.



first you have to uninstall all mysql client, mysql server, mysql server core and mysql workbench.

$ sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.7 mysql-client-core-5.7
$ sudo rm -rf /etc/mysql /var/lib/mysql
$ sudo apt-get autoremove
$ sudo apt-get autoclean

Now you have to install , new mysql server in your machine,

$ sudo apt-get install mysql-server


Here you set carefully root password in this step and click ok , by using tab.

Note: In this case, you do not have to run sudo apt-get update prior to the command. This is because we recently ran it in the commands above to install Apache. The package index on our computer should already be up-to-date.

and now instal mysql workbench,

Installing mysql-workbench package on Ubuntu 16.04 (Xenial Xerus) is as easy as running the following command on terminal:

$ sudo apt-get update
$ sudo apt-get install mysql-workbench


Now. go to search application, mysql and you can see mysql workbench and go to database and click for new database.
now you have to fill up your mysql server password , that you already set during installation of mysql server.


Happy coding!!! Happy Ubuntu!!!

Spring part 1 [IOC Containers]


IOC container contains two types of containers which are core container and another is advanced J2EE container. Core container is called BeanFactory and J2EE container is called ApplicationContext also one more Configurable Application Context.

So we may be thinking like what actually the containers do?

  1. They create a instances for the POJO classes.
  2. manage lifecycle of POJO classes.
  3. they can also do dependency Injection in POJO classes.


Then there may be a question what is the difference between the two types of containers? Here the core containers will initialize the object upon the user request where as the ApplicationContext will create the object at the compile time to be used for the user. Lets say we have a class named Demo. Now lets say we are defining the beans of the Demo in the xml file like

...
<beans>
    <bean id="demo" class="Demo" />
</beans>
...

Lets say while using the application user need the object the Demo then using the core container the IOC will create a new object for the user only when user request for it. But the ApplicationContext will eagerly creates the object while running the app and when user want the object it will provide the created or provide the reference of the created object to the user. By default the the bean creation scope is singleton which means it can be created only once and later whoever want that object it will reference the previous of firstly created object. But we can use several scopes methods like prototype, session, global session etc. Fo example if we have used the prototype scope then the IOC will create the new object upon each user requests, but will not create any object at the beginning.

...
<beans>
    <bean id="demo" class="Demo"  scope="prototype"/>
</beans>
...


1. Bean Factory Container

public static void main(String args[]){
   Resource res = new ClassPathResource("YOUR_XML_FILE_NAME.xml");
   BeanFactory factory = new XmlBeanFactory(res);
   Demo d = (Demo) factory.getBean("demo"); //typeCast because factory always return Object type
}

2. ApplicationContext Container

public static void main(String args[]){
  ApplicationContext app = new ClassPathXmlApplicaitonContext("some_location/your_xml_file.xml");
  Demo d = (Demo)  app.getBean("demo");
}


How the IOC creates the object ?
1. User request IOC to provide the instance of some class by using the bean id/name
2. Then IOC will look up for the bean with given id/name in the bean configuration file and take the name of the class
3. It execute the method called Class.forName("Demo").newInstance();

Simple Mouse Mover MAZE game in JQuery

Recently, i have developed simple mouse mover MAZE game , which helpful for your career to understand how to work mouse mover in JQuery.

Here is the HTML5 Code:


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
  <title>Maze!</title>
  
  
  <link href="maze.css" type="text/css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="maze.js" type="text/javascript"></script>
  
 </head>

 <body>
  <h1>The Amazing Mouse Maze!</h1>
  <h2 id="status">Click the "S" to begin.</h2>
  
  <!-- This part of the page represents the maze -->
  <div id="maze">
   <div id="start">S</div>
   <div class="boundary" id="boundary1"></div>
   <div class="boundary"></div>
   <div class="boundary"></div>
   <div class="boundary"></div>
   <div class="boundary"></div>
   <div id="end">E</div>
  </div>
  
  <p>
   The object of this game is to guide the mouse cursor through the 
start area and get to the end area.  Be sure to avoid the walls:
  </p>
  
  <div class="boundary example"></div>
  
  <p>
   Good luck!
  </p>
 

</body></html>



you have to write this JQuery code:

"use strict";

$(document).ready(function(){
 var start =  $("#start");
 var end = $("#end");
 var maze = $("#maze");
 var boundary = $(".boundary");
 var gameStart = false;
 maze.mouseleave(function(){ loss() });

 start.click(function(){
  $("#status").text('Move mouse to End in order to win the game');
  gameStart = true;
  if(boundary.hasClass('youlose')){
   boundary.removeClass('youlose');
  }
  boundary.mousemove(function(){ loss(); }) 
 });

 end.mousemove(function(){
  if(gameStart == true) won(); 
  else if(gameStart && boundary.hasClass('youlose'))  loss(); 
 
 });

 function won(){
  alert("Hurry!!! you won");
  gameStart = false;
  $("#status").text("Hurry!!! you won! please collect your prize money.");
 }

 function loss(){
  if(gameStart){
   gameStart = false;
   boundary.addClass('youlose');
   $("#status").text('So Sad !!!You lose the game, Try next time');
  }
 }


})


finlly, some adding of css code:

h1, h2 {
 text-align: center;
}

p {
 width: 600px;
 margin: 1em auto;
}

#maze {
 margin: auto;
 position: relative;
 height: 300px;
 width: 500px;
}

#start, #end {
 position: absolute;
 top: 205px;
 height: 30px;
 width: 30px;
 border: 1px solid black;
 padding: 5px;
 cursor: default;
 
 font-family: "Helvetica", "Arial", sans-serif;
 font-size: 25pt;
 text-align: center;
}

#maze #start {
 background-color: #88ff88;
 left: 0;
}

#maze #end {
 background-color: #8888ff;
 right: 0;
}

div.boundary {
 background-color: #eeeeee;
 border: 1px solid black;
}

div.boundary.example {
 margin: auto;
 width: 100px;
 height: 25px;
}

div.youlose {
 background-color: #ff8888;
}

/*
Hack hack hack; these are CSS "sibling selectors" for selecting
neighboring elements.  Necessary to avoid giving ids to the boundary divs
*/

#maze div.boundary {
 position: absolute;
 
 top: 0;
 left: 0;
 height: 200px;
 width: 150px;
 z-index: 1;
}

#maze div.boundary + div.boundary {
 border-left: none;
 border-right: none;
 z-index: 2;
 
 left: 151px;
 height: 50px;
 width: 198px;
}

#maze div.boundary + div.boundary + div.boundary {
 border-left: 1px black solid;
 border-right: 1px black solid;
 z-index: 1;
 
 left: 348px;
 height: 200px;
 width: 150px;
}

#maze div.boundary + div.boundary + div.boundary + div.boundary {
 border: 1px black solid;
 
 top: 250px;
 left: 0;
 height: 48px;
 width: 498px;
}

#maze div.boundary + div.boundary + div.boundary + div.boundary + div.boundary {
 border-bottom: none;
 
 top: 100px;
 left: 200px;
 height: 150px;
 width: 98px;
}


and output like this:





Happy Coding !!!


Elasticsearch part 6 [Nested Queries]

Lets first create the mapping, here in this tutorial we are using the example of book document which will contain he nested author field.

PUT /books/
{
    "mappings": {
        "book":{
            "properties": {
                "id":{
                    "type": "integer"
                },
                "title":{
                    "type": "string"
                },
                "categories":{
                    "type": "integer"
                },
                "tag":{
                    "type": "string"
                },
                "author":{
                    "type": "nested",
                    "properties": {
                        "firstname":{
                            "type": "string"
                        },
                        "lastname":{
                            "type": "string"
                        },
                        "id":{
                            "type": "integer"
                        }
                    }
                }
            }
        }
    }    
}

Here the type of author is nested which means it acts as object.
Note:: "nested" is more advanced form of "object" which allow us to put objects in the arrays.

Lets populate some data 

PUT /books/book/101
{
        "title" : "An article title",
        "categories" : [1,3,5,7],
        "tag" : ["elasticsearch", "symfony", "Obtao"],
        "author" : [
            {
                "firstname" : "Francois",
                "surname": "francoisg",
                "id" : 18
            },
            {
                "firstname" : "Gregory",
                "surname" : "gregquat",
                "id" : "2"
            }
        ]
}

PUT /books/book/102
{
        "title" : "Elasticsearch",
        "categories" : [1,3,5,7],
        "tag" : ["elasticsearch", "bigdate", "Text search"],
        "author" : [
            {
                "firstname" : "yubraj",
                "surname": "pokharel",
                "id" : 18
            },
            {
                "firstname" : "dharma",
                "surname" : "kshetri",
                "id" : "2"
            }
        ]
}

Now lets say we need those books whose author name is yubraj pokharel ( thats me ;) ) so what we do now umm lets try some queries


GET /books/book/_search
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested" : {
          "path" : "author",
          "filter":{
              "bool": {
                  "must": [
                     {
                         "term": {
                            "author.firstname": "yubraj"
                         }
                     },
                     {
                         "term": {
                            "author.surname": "pokharel"
                         }
                     }
                  ]
              }
          }
        }
      }
    }
  }
}


so the output will be 

{
   "took": 29,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "books",
            "_type": "book",
            "_id": "102",
            "_score": 1,
            "_source": {
               "title": "Elasticsearch",
               "categories": [
                  1,
                  3,
                  5,
                  7
               ],
               "tag": [
                  "elasticsearch",
                  "bigdate",
                  "Text search"
               ],
               "author": [
                  {
                     "firstname": "yubraj",
                     "surname": "pokharel",
                     "id": 18
                  },
                  {
                     "firstname": "dharma",
                     "surname": "kshetri",
                     "id": "2"
                  }
               ]
            }
         }
      ]
   }
}

OR you can also use the query like this

GET /books/book/_search
{
    "query": {
        "bool": {
            "must": [
               {
                "match": {
                   "title": "elasticsearch"
                }
               },
               {
                   "nested": {
                      "path": "author",
                      "query": {
                          "bool": {
                              "must": [
                                 {
                                     "term": {"author.firstname": {"value": "yubraj"}}
                                 },
                                 {
                                     "term": {"author.surname": {"value": "pokharel"}}
                                 }
                              ]
                          }
                      }
                   }
               }
            ]
        }
    }

}



ElasticSearch Part 5 [Selecting Shards Algorithm]

Let me explain this in the natural language. Lets suppose there are 10 shards across all the network, and we need to write a document in the shard. And you start to think about it like how should I select the shards to which we need to write. Don't worry it can ES can select the shards randomly. Yes you think you are smart? think again what will happen when you want to retrieve the data again do you want to use random again fuck no because it may not get the required data in the random shards we pick.

Don't worry there is a algorithm that is used by the partitioner in the ES which determines the shards where we need to write the document. For this the document id is used.

shard = hash(routing) % number_of_primary_shards

here routing value is the arbitrary string which by default is the doc_id. Lets take a example to understand it. Let us suppose we have 10 shards all across the network and we want to put a user detail in one of the shards. Let user doc_id be 100 and after hashing we get some number like 66 then

shard = 66 % 10 = 6

hence the partitioner will write this document in the shard with index 6 i.e. the 7th one. And also use the same algorithm to retrieve it. In the above process the remainder will always be in the range of 0 to number_of_primary_shards -1.

But there is some drawbacks in the scaling of ES. Users sometimes think that having a fixed number of primary shards makes it difficult to scale out an index later. In reality, there are techniques that make it easy to scale out as and when you need. For this we need to re-index all the data and there are some techniques to do it which we will discuss in later post.

ElasticSearch Part 4 [Advanced Query]

lets focus in the Query string what we can achieve and how we can perform advanced queries using the query string

1. +/- Operators: This helps us to filter the words during the search. Let us take an example:
GET /ecommerce/product/_search?q=(name:(+dell)
in the above query the name feild must contain the dell word

GET /ecommerce/product/_search?q=(name:(+dell -hp))
in the above query the name field must contain the dell word and must not contain any hp word

2. Boolean Operators:: lets say we want to search the product with
  - name as "dell"
  - must have status active i.e. 1
  - which description must contain the word "code"

so we will do it by
GET /ecommerce/product/_search?q=(name:(+dell -hp) AND status:1 AND description:+code)


ElasticSearch Part 3 [Categories of Queries]


In elasticsearch there are two main categories in queries which are :

1. Leaf: It looks for particular value in particular fields like "dell" in the product name. This queries can be used by themselves without being part of the compound queries. And the best thing is it can be used as a part of compound query too for the most advanced queries.

2. Compound: This queries wrap the leaf queries and can wrap other compound queries. It combine multiple queries in logical fashion which means as a Boolean logic. Using this we can also alter the behavior of the queries.

3. Full Text: It is used for running full text search query i.e. looking for every fields in the document. Here values are analyzed when adding and updating the document. It is analyzed like by using the stop words like "the".

4. Term Value: Used to match exact matching values. Usually used for the numbers and date rather then the text. E.g. Finding peoples who are born between 2001 and 2010. Here search queries are not analyzed before executing.

5. Joining Queries: As we know that it is very expensive to perform joining in the distributed system so elasticsearch offers two forms of joins that are designed to scale horizontally they are:

I. Nested Queries
Lets go back, where we had defined a propertise called category in the product document which contains the array of the categories. This nested queries are used in such situation where each object can be queried as a nested query as a indepedent query. 

II. has_child and has_parent queries
has_child returns the parent document which child document match the query similarly has_parent returns the child document which parent document match the query.

6. Geo Queries
I. geo_point:: it is used for latitute/longitude pairs

II. geo_shape:: it is used for the shapes like triangle, polygons etc.

ElasticSearch Part 2 [Searching]

Lets populate some products first: 

PUT /ecommerce/product/1002
{
    "name": "Dell",
    "price": 1200.00,
    "description": "This product is awesome and I love to code in it",
    "status": 1,
    "quantity": 2,
    "categories": [
        {"name":"dell"},
        {"name":"laptop"}
        ],
    "tags":["dell", "programming", "laptop"]
}

PUT /ecommerce/product/1003
{
    "name": "Dell",
    "price": 1200.00,
    "description": "This product is awesome and I love to design in it",
    "status": 1,
    "quantity": 2,
    "categories": [
        {"name":"dell"},
        {"name":"laptop"}
        ],
    "tags":["dell", "desigining", "laptop"]
}

In the previous post I have explained about how to create, update, read and deleting the documents in ElasticSearch. Now lets talk about Searching, Oh yeah! searching for which ElasticSearch is best about. Basically there are two ways of searching techniques which are: Query String and Query DSL. But lets talk about some basic stuff that we need to know before starting Search.

Relevancy and Scoring: To rank document for query, a score is calculated for each document that matches a query. Which means the higher the search score the document is more relevant to search query.

1. Query String: Its done using the search parameter in the URI through the rest request. It is specilly used for simple queries as well as for ad-hoc. e.g.

GET https://localhost/ecommerce/product/_serarch?q=dell
GET https://localhost/ecommerce/product/_serarch?q=name:dell &desc:"SOME_TEXT"
GET https://localhost/ecommerce/product/_serarch?q=(name:(dell OR hp) AND status:1)

Here all fields are searched for the word "dell" by default.

NOTE :: _search is a API used to perform a search process and q is the query parameter

and the output will be: 

{
   "took": 30,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.3074455,
      "hits": [
         {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1003",
            "_score": 0.3074455,
            "_source": {
               "name": "Dell",
               "price": 1200,
               "description": "This product is awesome and I love to design in it",
               "status": 1,
               "quantity": 2,
               "categories": [
                  {
                     "name": "dell"
                  },
                  {
                     "name": "laptop"
                  }
               ],
               "tags": [
                  "dell",
                  "desigining",
                  "laptop"
               ]
            }
         }
      ]
   }
}


2. Query DSL :: In this technique the queries are defined within the request body of the JSON. It supports more feature then the query string approach. Its often easier to read and also we can perform more advanced queries. e.g.

GET /ecommerce/product/_search
{
    "query": {
        "filtered": {
           "filter": {
               "range": {
                  "price": {
                     "from": 1000,
                     "to": 2000
                  }
               }
           },
           "query": {
               "query_string": {
                  "fields": ["tags"],
                  "query": "laptop",
                  "query": "hp"
               }
           }
        }
    }
}

In the above query it will first filter the products having the price in between 1000 to 2000 and then looks up for the words laptop and hp in the tags field.

GET /ecommerce/product/_search
{
    "query": {
        "match": {
           "name": "dell"
        }
    }
}

and the result will be

{
   "took": 664,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1.4054651,
      "hits": [
         {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1003",
            "_score": 1.4054651,
            "_source": {
               "name": "Dell",
               "price": 1200,
               "description": "This product is awesome and I love to design in it",
               "status": 1,
               "quantity": 2,
               "categories": [
                  {
                     "name": "dell"
                  },
                  {
                     "name": "laptop"
                  }
               ],
               "tags": [
                  "dell",
                  "desigining",
                  "laptop"
               ]
            }
         },
         {
            "_index": "ecommerce",
            "_type": "product",
            "_id": "1002",
            "_score": 1.4054651,
            "_source": {
               "name": "Dell",
               "price": 1200,
               "description": "This product is awesome and I love to code in it",
               "status": 1,
               "quantity": 2,
               "categories": [
                  {
                     "name": "dell"
                  },
                  {
                     "name": "laptop"
                  }
               ],
               "tags": [
                  "dell",
                  "programming",
                  "laptop"
               ]
            }
         }
      ]
   }
}

ElasticSearch Part 1 [Mappings]


For this tutorial we are using the "Sense - a Json aware interface to ElasticSearch"

1. creating a index in elasticsearch
PUT /ecommerce {}


2. creating a mapping named product
PUT /ecommerce/
{
    "mappings": {
        "product":{
            "properties": {
                "name":{
                    "type": "string"
                },
                "price":{
                    "type": "double"
                },
                "description":{
                    "type": "string"
                },
                "quantity":{
                  "type":"string"
                },
                "status":{
                    "type": "integer"
                },
                "categories":{
                    "type": "nested",
                    "properties": {
                        "name":{
                            "type": "string"
                        }
                    }
                },
                "tags":{
                    "type": "string"
                }
            }
        }
    }
}


3. putting data in the schema with id 1001

PUT /ecommerce/product/1001
{
    "name": "Inception Innovation Center",
    "price": 120.00,
    "description": "Welcome to heaven of code",
    "status": 1,
    "quantity": 2,
    "categories": [
        {"name":"innovation"},
        {"name":"programming"}
        ],
    "tags":["inception", "programming"]
}


4. Getting the item info having ID 1001
GET /ecommerce/product/1001
{
   "_index": "ecommerce",
   "_type": "product",
   "_id": "1001",
   "_version": 1,
   "found": true,
   "_source": {
      "name": "Inception Innovation Center",
      "price": 120,
      "description": "Welcome to heaven of code",
      "status": 1,
      "quantity": 2,
      "categories": [
         {
            "name": "innovation"
         },
         {
            "name": "programming"
         }
      ],
      "tags": [
         "inception",
         "programming"
      ]
   }
}

5. Lets say we want to update the above doc with one more category then what we need to do is 

POST /ecommerce/product/1001/_update
{
  "doc":{
         "categories": [
                {"name":"innovation"},
                {"name":"programming"},
                {"name":"inception"}
            ]
  }
}

here doc represent the the key-value pair of fields that needs to be update. Its a simple way to update and there is also another way to update for that we need to write entire properties like:

PUT /ecommerce/product/1001
{
    "name": "Inception Innovation Center",
    "price": 120.00,
    "description": "Welcome to heaven of code",
    "status": 1,
    "quantity": 2,
    "categories": [
                {"name":"innovation"},
                {"name":"programming"},
                {"name":"inception"}
            ],
    "tags":["inception", "programming"]
}

among this this I prefer the first one. The main advantage of second one is, if there is no product with id 1001 then it will create a new product with the specified id, which means there will be no data loss.
And the following is the output

{
   "_index": "ecommerce",
   "_type": "product",
   "_id": "1001",
   "_version": 2,
   "found": true,
   "_source": {
      "name": "Inception Innovation Center",
      "price": 120,
      "description": "Welcome to heaven of code",
      "status": 1,
      "quantity": 2,
      "categories": [
         {
            "name": "innovation"
         },
         {
            "name": "programming"
         },
         {
            "name": "inception"
         }
      ],
      "tags": [
         "inception",
         "programming"
      ]
   }
}

.Here the version of the doc has been changed.

HTML Headings

Headings are defined with the <h1> to <h6> tags.
<h1> defines the most important heading. <h6> defines the least important heading.

This is heading 1

This is heading 2

This is heading 3

This is heading 4

This is heading 5
This is heading 6