Saturday, May 2, 2020

Stream API in Java

Stream in Java:

Stream API is used to process collection of objects. It is a sequence of objects that supports various methods which can be pipelined to produce desired result.

Following are the characteristic of the Stream -
  • Sequence of elements: A stream provides set of elements of specific type in a sequential manner. It get/commutes elements in demand. It never stores the elements
  • Source: Stream takes Arrays, Collections or I/O resource as input resource. 
  • Aggregate Operations: Stream supports aggregate operations like filter, map, limit, reduce, find, match etc.
  • Pipelining: Most of the stream operation returns stream itself so that their results can be pipelined. This operations are called as Intermediate Operations and their function is to take input, process them and return output to the target. collect() method is the terminal operation which is normally present at the end of the pipelining operation to mark the end of the Stream.
  • Automatic iteration: Stream operations does the iteration internally over the source elements provided.
In Java 8 Collection interface has two methods to generate the Stream:
  1. stream(): Returns a sequential stream.
  2. parallelStream(): Returns the parallel stream.
Example: 


The output will be:

Friday, May 1, 2020

Java 8 Functional Interface


A Functional interfaces have single functionality to exhibit. Just like compareTo function is used for comparison purpose.

1. Function<T, R>

Function takes one argument of type T and Returns one argument of type R.

Here,
T -> Type of input to the function.
R -> Type of output/result of the function.

Example: 

Function<String, Integer> getStringLengthFunction = x->x.length()
Integer length = getStringLengthFunction.apply("abhay");
System.out.println(length);

The output will be: 5

2. Chaining Function <T, R>


We can chain multiple functions:

Example: 

Function<String, Integer> getStringLengthFunction = x->x.length();
Function<Integer,Integer> multiplyFunction = x->x*2;

Integer value = getStringLengthFunction.andThen(multiplyFunction).apply("abhay");
System.out.println(value);

The output will be: 10

3. BiPredicate Function <T, R>


The BiPredicate function takes two arguments as input and returns boolean value. Meaning you can input two arguments, process it and return the result in boolean.

Example:

BiPredicate <String,Integer> validateLength = (x,y) -> {
          return x.length() == y;
};

boolean isCorrect = validateLength.test("abhay",5);
System.out.println(isCorrect);

isCorrect = validateLength.test("abhay",10);
System.out.println(isCorrect);

The output will be: 
true
false

4. Consumer <T>


The Consumer function takes one argument as input and doesn't return anything.

Example:

Consumer <String> value = x ->{
          System.out.println("The value is: "+x);
};

value.accept("abhay");

The output will be: The value is: abhay

5. Supplier <R>


The Supplier is opposite of the Consumer, it takes nothing but returns object.

Example:

Supplier<LocalDateTime> s = () -> LocalDateTime.now();
LocalDateTime localDateTime = s.get();
System.out.println(localDateTime);

The output will be: 2020-05-02T10:26:38.454

Monday, March 20, 2017

GitLab Setup

GitLab Server Setup on AWS:

Select this AMI from your desired region:

bitnami-gitlab-8.17.2-0-linux-ubuntu-14.04.3-x86_64-hvm-ebs

Select this Instance Type:

any instance > t2.xlarge

Disk Size:

Select disk size as per your project need.

............................. WIP

Wednesday, February 15, 2017

Postgres backup and restore using pg_dump

Backup & Restore commands for postgres database


-Backup using dumpall command (take backup of all db's):

pg_dumpall -c -U postgres -h localhost -p 5432  -f sample.dump

-Restore using psql command

psql -U postgres -h localhost -p 5432 -f sample2.dump postgres

Friday, September 30, 2016

Enabling elasticsearch-readonlyrest-plugin

1. Install the plugin

export ES_VERSION=2.3.0
bin/plugin install https://github.com/sscarduzio/elasticsearch-readonlyrest-plugin/releases/download/v1.10.0_es-v$ES_VERSION/elasticsearch-readonlyrest-v1.10.0_es-v$ES_VERSION.zip

2. Configuration

Append either of these snippets to conf/elasticsearch.yml
# remember to set the right CORS origin (or disable it, if you're brave). See https://github.com/elastic/kibana/issues/6719
http.cors.enabled: true
http.cors.allow-origin: /https?:\/\/localhost(:[0-9]+)?/

readonlyrest:
    enable: true

    response_if_req_forbidden: Forbidden by ReadonlyREST ES plugin

    access_control_rules:

    - name: "Logstash can write and create its own indices"
      # auth_key is good for testing, but replace it with `auth_key_sha1`!
      auth_key: logstash:logstash
      type: allow
      actions: ["indices:data/read/*","indices:data/write/*","indices:admin/template/*","indices:admin/create"]
      indices: ["logstash-*", "<no-index>"]

    - name: Kibana Server (we trust this server side component, full access granted via HTTP authentication)
      # auth_key is good for testing, but replace it with `auth_key_sha1`!
      auth_key: admin:passwd3
      type: allow

    - name: Developer (reads only logstash indices, but can create new charts/dashboards)
      # auth_key is good for testing, but replace it with `auth_key_sha1`!
      auth_key: dev:dev
      type: allow
      kibana_access: ro+
      indices: ["<no-index>", ".kibana*", "logstash*", "default"]
Now activate authentication in Kibana server: let the Kibana daemon connect to ElasticSearch in privileged mode.
  • edit the kibana configuration file: kibana.yml and add the following:
elasticsearch.username: "admin"
elasticsearch.password: "passwd3"
This is secure because the users connecting from their browsers will be asked to login separately anyways

4. restart elastic search

ENABLING KIBANA AUTHENTICATION USING NGINX:


  1. Install nginx:
    1. rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    2. yum -y install nginx httpd-tools
  2. Set one username & password
    1. htpasswd -c /etc/nginx/conf.d/kibana.htpasswd admin
  3. Configure Nginx
    1. vi /etc/nginx/conf.d/kibana.conf
    2. server {
      listen *:8080;
      server_name 192.168.1.5;
      access_log /var/log/nginx/kibana-access.log;
      error_log /var/log/nginx/kibana-error.log;
      location / {
      auth_basic "Restricted Access";
      auth_basic_user_file /etc/nginx/conf.d/kibana.htpasswd;
      proxy_pass http://192.168.1.5:5601;
      #proxy_connect_timeout 150;
      #proxy_send_timeout 100;
      #proxy_read_timeout 100;
      }
      }
  4. Restart Nginx
    1. sudo service nginx restart
  5. Go to the URL : http://192.168.1.5:8080, we should get a authentication screen on successful setup

  NOTE: In case if it doesn't work:


  1. Disable the selinux by running the command
    1. sudo setsebool -P httpd_can_network_connect 1


    2. sudo service nginx restart

Thursday, September 1, 2016

Getting started with Ansible

Ansible Demo

Installing Ansible on CentOS 7 Machine 1 (Eg IP: 192.168.56.101)

yum install -y ansible

Creating inventory file

$ mv /etc/ansible/hosts /etc/ansible/hosts.orig
$ vi /etc/ansible/hosts 

[web]
192.168.56.102 ansible_ssh_pass=<Password> ansible_ssh_user=<UserName>

Save this file with "!wq" command.

Creating Playbook file 

$vi SamplePlaybook.yml
---
- hosts: web
  tasks:
   - name: install apache
     yum: name=httpd state=latest
   - name: copy my indexfile
     template: src=/root/index.html dest=/var/www/index.html
     notify:
      - restart apache
   - name: ensure apache is running
     service: name=httpd state=started enabled=yes
  handlers:
  - name: restart apache
    service: name=httpd state=restarted


Save this file with "!wq" command.

Now execute this file using ansible playbook command:

$ ansible-playbook SamplePlaybook.yml

Output
----------------------------------------------------------------------------------


PLAY [web] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.56.102]

TASK [install apache] **********************************************************
ok: [192.168.56.102]

TASK [copy my indexfile] *******************************************************
changed: [192.168.56.102]

TASK [ensure apache is running] ************************************************
changed: [192.168.56.102]

RUNNING HANDLER [restart apache] ***********************************************
changed: [192.168.56.102]

PLAY RECAP *********************************************************************
192.168.56.102             : ok=5    changed=3    unreachable=0    failed=0


Terraform Cheat Sheet [WIP]

Installing Terraform