Search This Blog

2021/02/18

Importing Json to Elastic Search using Postman

  Download file https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true

 

 File looks like below,it is not json array but in different form

 

{"index":{"_id":"1"}}

{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}

{"index":{"_id":"6"}}

{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}



Now in postman 

    Keep Endpoint as localhost:9200/accounts/docs/_bulk

    set request header Content-Type:application/json

     select body to be binary and select file as account.json


 

Response:

{

    "took": 593,

    "errors": false,

    "items": [

        {

            "index": {

                "_index": "accounts",

                "_type": "docs",

                "_id": "1",

                "_version": 1,

                "result": "created",

                "_shards": {

                    "total": 2,

                    "successful": 1,

                    "failed": 0

                },

                "_seq_no": 0,

                "_primary_term": 1,

                "status": 201

            }

        },

        {

            "index": {

                "_index": "accounts",

                "_type": "docs",

                "_id": "6",

                "_version": 1,

                "result": "created",

                "_shards": {

                    "total": 2,

                    "successful": 1,

                    "failed": 0

                },

                "_seq_no": 1,

                "_primary_term": 1,

                "status": 201

            }

        },

//more entries here

    ]

}


With Postman you can view all inserted records with GET request against URL localhost:9200/accounts/_search?pretty&size=1000


2021/02/06

normal function vs fat arrow function

Fat Arrow function and normal function differ in following aspect

new keyword use:

    Regular functions created using function declarations or expressions are constructible and callable. Since regular functions are constructible, they can be called using the new keyword.

            function add(x,y)
            {
                console.log(x+y)
            }
            new add(5,6)
        output:
             11

    However, the arrow functions are only callable and not constructible.Hence, they can never be invoked with the new keyword.
        sub =(x,y)=>{
            console.log(x-y)
        }


        new sub(5,6)
        
        Output:
            TypeError: sub is not a constructor


    Fat Arrow function can not be used as a constructors.



Duplicate Named function parameter:
    function add(x,x)
    {
        console.log(x);
    }
    add(5,7)
    
    Output:
        7
        
    but
    
    add=(x,x)=>{
       console.log(x);
    }

    add(5,6)        
        
    Output:    
        SyntaxError: Duplicate parameter name not allowed in this context
    
    With arrow functions, duplicate named arguments are always, regardless of strict or non-strict mode, invalid.
    
    
use of this keyword:
    let fruit = {
        name: "Mango",
        FatArrow: () => {
        console.log("My name is " + this.name); // no 'this' binding here
        },
        Regular() {
        console.log("My name is " + this.name); // 'this' binding works here
        }
    };

    fruit.FatArrow();
    fruit.Regular();    
    
    output:
        My name is undefined
        My name is Mango
        
        
    fat arrow functions do not have their own this. The value of this inside an fat arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in         the closest non-arrow parent function
arguments keyword:

   let myFunc = {  
    showArgs(){
     console.log(arguments);
    }
   };
   myFunc.showArgs(1, 2, 3, 4,5);
   
   
   Output:
       [Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5 }
    
   but
   
    let myFunc = {  
        showArgs : () => {
        console.log(...arguments);
      }
    };
    myFunc.showArgs(10,30,45,78);    
   
   output:
    [Arguments] {
    '0': {},
    '1': [Function: require] {
    resolve: [Function: resolve] { paths: [Function: paths] },
    main: Module {
    id: '.',
    path: '/home/sangram/projects/test10',
    exports: {},
    filename: '/home/sangram/projects/test10/test.js',
    loaded: false,
    children: [],
    paths: [Array]
    },
    extensions: [Object: null prototype] {
    '.js': [Function (anonymous)],
    '.json': [Function (anonymous)],
    '.node': [Function (anonymous)]
    },
    cache: [Object: null prototype] {
    '/home/sangram/projects/test10/test.js': [Module]
    }
    },
    '2': Module {
    id: '.',
    path: '/home/sangram/projects/test10',
    exports: {},
    filename: '/home/sangram/projects/test10/test.js',
    loaded: false,
    children: [],
    paths: [
    '/home/sangram/projects/test10/node_modules',
    '/home/sangram/projects/node_modules',
    '/home/sangram/node_modules',
    '/home/node_modules',
    '/node_modules'
    ]
    },
    '3': '/home/sangram/projects/test10/test.js',
    '4': '/home/sangram/projects/test10'
    }

   Arrow functions do not have an arguments binding. However, they have access to the arguments object of the closest non-arrow parent function.

How to use arguments in arrow function?

    var bar = (...arguments) => console.log(arguments);

    bar(1,3,6,89,67)   
    Output:
        [ 1, 3, 6, 89, 67 ]

       
Call Apply bind:   
   Arrow function not suitable for call, apply and bind methods, which generally rely on establishing a scope.
   
  

2021/02/05

POSTGRES -RANK,DENSE_RANK,ROW_NUMBER difference

what is difference between row-number,rank & dense-rank in postgres ?

Lets understand by examples.


create table for purpose of explination    
    CREATE TABLE emp(id serial,name varchar(100))

insert some rows

    insert into emp(name) values ('sagar'),('SAGAR'),('sangram'),('sangram'),('sachin'),('swapnil')
    
    
ROW_NUMBER function:
    Example:
    
    select name, ROW_NUMBER() OVER(ORDER BY name asc) from emp;    
    output:
        name         row_number
        "sachin"    1
        "sagar"    2
        "SAGAR"    3
        "sangram"    4
        "sangram"    5
        "swapnil"    6

        This function will sort record based on name and assign rank to it based on order.
        
 
 RANK function:
      Example:
          select name, RANK() OVER(ORDER BY name) from emp;
      
      Output:
        name    rank
        sachin    1
        sagar    2
        SAGAR    3
        sangram    4
        sangram    4
        swapnil    6


    Here Rank of sangram is 4 for both entries as two entries for sangram next record swapnil has rank 6 instead of 5.
    
    This function is similar to row number except identical rows get same rank & rank is skipped if identical records are found previously
    
Dense_Rank:
   Example:
   
       select name, DENSE_RANK() OVER(ORDER BY name) from emp
       
   Output:
    name        dense_rank
    "sachin"    1
    "sagar"    2
    "SAGAR"    3
    "sangram"    4
    "sangram"    4
    "swapnil"    5    
    
    Here sangram occurs twice so both get same rank 4 but next record swpnil get 5th rank & rank are not skipped if identical rows found.
   

2021/02/02

INSERT or update on postgres View

we usually create view is it feasible to insert or update on views instead of table lets explore.

   point to note that if insert succeed on view then data essentially got inserted into table only.


case simple view:
    create new view as follows

        create view vw_15
        as
        select * from book where id <=15

    attempt an insert

        insert into vw_15(title,author) values('my book','test')
    Output:

    INSERT 0 1

    Query returned successfully in 235 msec.

    Inference : insert got succeeded

Case view with computed column

    First we will create a view as follows
        create view vw_lw_author
        as
        select title,lower(author) as author from book

    try to insert into view

        insert into vw_lw_author(title,author) values('my book','SANGRAM')



    Output:
        ERROR:  cannot insert into column "author" of view "vw_lw_author"
        DETAIL:  View columns that are not columns of their base relation are not updatable.
        SQL state: 0A000
        
    Inference : insert failed
    
Case view with check option    
    create view vw_15_check
    as
    select * from book where id <=15 with check option    
    
     try to insert here
    
    insert into vw_15_check(title,author) values('my book','SANGRAM')

                                                 or

    insert into vw_lw_author(title,author) values('my book','sangram')
    
    Output:
        ERROR:  new row violates check option for view "vw_15_check"
        DETAIL:  Failing row contains (20, my book, SANGRAM, null, null, null, null).
        SQL state: 44000
    
    Inference : insert failed    
    
    
Updating with check option column
      our view

    create view vw_price_lte_200
    as
    select * from public."Movies" where "Price" <=200 with check option    
    
    we try to update
    
        update vw_price_lte_200 set "Price" = 150
        
        output:
            UPDATE 1

            Query returned successfully in 106 msec.
            
        Here update succeed


    Now try update that violate constraint
    
    update vw_price_lte_200 set "Price" = 600
    
    Output:
        ERROR:  new row violates check option for view "vw_price_lte_200"
        
    Here update fails.
        DETAIL:  Failing row contains (5, Sholay, 2019-10-10, Action, 600).
        SQL state: 44000