Design a site like this with WordPress.com
Get started

Apache Spark – Beginner Example

Lets look at a simple spark job

We are going to look at a basic spark example. At the end of the tutorial, we will come to know

  1. A basic spark project structure
  2. Bare minimum libraries required to run a spark application
  3. How to run a spark application on local

Git Repo – https://github.com/letsblogcontent/SparkSimpleExample

Pre-requisites
1. Java
2. Maven
3. Intellij or STS (Optional but recommended)

Follow below steps to complete your first spark application

  1. Create a new maven project without any specific archetype. I am using IntelliJ editor but you may choose any other suitable editor as well. I have created a project with name “SparkExample”
    • Navigate to File-> New Project
    • Select Maven from Left Panel
    • Do not select any archetype
    • Click on “Next”
    • Name the project “SparkExample”
    • Click on “Finish”
      This should create a new maven project like below
      project structure
  2. Next we update the pom.xml with spark-core dependency as below.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SparkExamples</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>2.0.0</version>
        </dependency>

    </dependencies>

</project>

3. Now we create a new Class “WordCount” in “com.examples” package and copy below contents.

package com.examples;


import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.Arrays;
import java.util.Map;

public class WordCount {

    public static void main(String[] args) throws Exception {

        SparkConf conf = new SparkConf().setAppName("wordCounts").setMaster("local[3]");
        JavaSparkContext sc = new JavaSparkContext(conf);

        JavaRDD<String> lines = sc.textFile("in/word_count.txt");
        JavaRDD<String> words = lines.flatMap(line -> Arrays.asList(line.split(" ")).iterator());

        Map<String, Long> wordCounts = words.countByValue();

        for (Map.Entry<String, Long> entry : wordCounts.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}

4. create a new directory folder “in” at the project root and add below file into the “in” directory. In this example we are going to read this file and use spark to count the occurrence of each word in the file.

5. Now we have to build our project to see our output. Since we are using maven, we can run the “mvn clean install” from the command prompt or we can use the rebuild from the intellij. both works and once that is done we can run our application. So basically we have to run the WordCount class so right click on the class and run “WordCount.main()”

6. This should fire up a standalone spark application and run our job of “WordCount”. This job basically counts for the occurrences of words in the file “word_count.txt”. The output should look like below

Twenties, : 1
 II : 2
 industries. : 1
 economy : 1
  : 7
 ties : 2
 buildings : 1
 for : 3
 eleventh : 1
 ultimately : 1
 support : 1
 channels : 1
 Thereafter, : 1
 subsequent : 1
.....
..

7. Now that we have successfully ran the program, lets learn what really happened
The below code configures the name of our spark application and we set the master to be local, which basically tells spark to run this application locally and run it on 3 cores.

SparkConf conf = new SparkConf().setAppName("wordCounts").setMaster("local[3]");

8. Initializes the spark context

 JavaSparkContext sc = new JavaSparkContext(conf);

9. The below code reads the file and converts it into what is called as Resilient Distributed Dataset (RDD) . This will distribute our file into 3 cores to be processed further and returns us with a single reference to the RDD for manipulation

JavaRDD<String> lines = sc.textFile("in/word_count.txt");

10. Rest of the code is self explanatory. The RDD api provides with certain apis like the one we have used. The countByValue as name suggest counts the occurrence of values in our text file and then when we print the values from the map, we get a consolidated view of the aggregation.

So as can be seen, writing a spark application is really easy and its only with a single class we can start writing a spark application. Please comment if you have faced any issue following this tutorial or like if you would like to see more.

Advertisement

Apache Spark – Introduction

In plain simple computing language, spark is an open source cluster computing framework. It is used to solve big data problems. Spark distributes the data on the cluster nodes and then will process the distributed data on each of the nodes against the local data and then send the consolidated response back to the requested of the spark job. If someone is going to ask you about spark, the above explanation is good enough.

Big data has gained lot of traction in last decade or so as users of the internet are continuously creating huge amount of data and processing huge data was something our older frameworks were not capable of handling. Such huge amount of data requires special handling and that was provided initially by Hadoop. What spark provides over Hadoop is the speed. Spark in most cases will perform better than Hadoop. Spark does all the processing in-memory whereas Hadoop writes it on the disk. When it is in-memory processing spark performs upto 100 times better than Hadoop and upto 10 times faster when we write to disk in spark.

It does appear that it will be complex and difficult to follow and understand spark however, most of the complexity is abstracted by the spark and it is extremely easy to start coding in spark. If you know basics of Java, Scala, Python or R, then you can easily write a spark job. In terms of java, we are supposed to write everything inside a main program and we can submit the same to a spark cluster.

A spark cluster typically looks like below. Suppose we are having a spark cluster of 3 nodes. So one of the node will become the master node and rest as worker nodes. Spark has a standalone cluster manager which will basically drive your program across the cluster and act as master. A spark job is submitted to a spark cluster ( a spark job is nothing but a main program bundled in a jar), the node on which we submit a spark job is called the driver program and will have the instance of spark context. All the processing will happen on the worker nodes. Spark can work with HDFS, Hive, Cassandra, Hbase as its storage. We will come to know why do we need storage in future posts.

This is a small introduction to Apache Spark. I will be writing more about the Spark Architecture, Core components, Running your first spark application in future posts. Let me know what you would like to see first.

Also please comment or like if you liked the explanation. Thank you for spending time reading this post.

Python : Basic Syntax

Today we will be covering basic syntax of python

Printing any string

python provided print function to print any string

 print("Hello, World!")

Comments in python

“#” is getting used to comment in python

#This is a comment.
print("Hello, World!")

Python interpreter also supports comment in below format with three dots

...
This iscomment
comment
...

Python Variables

In Python, variables are created when you assign a value to it:

a = 335
b = "Hello, World!"

Python Indentation

python used space to indicate block of code.

Below is example for wring indentation

Now we are correcting it and rerunning.

Before print we have added space so code of block is correctly indented

Multi-Line Statements

Python supports multiline statements with below syntax so you can have code written on multiple lines

print("multiline comment" + \
      "multiline comment" + \
      "multiline comment"
      )

Below is example

Sample code for your reference

print("Hello World")
if 5 > 2:
    print('5 is greater')

print("multiline comment" + \
      "multiline comment" + \
      "multiline comment"
      )

x= 2 + \
   3 + \
    4

print("value of " , x)

How to install Python

1 go to below URL

https://www.python.org/

For windows user you can download installed from below URL

https://www.python.org/downloads/windows/

For Ubuntu user

$ sudo apt-get update
$ sudo apt-get install python3.6

Setting path at Windows

Follow below link

Testing Python installation

Run Python and execute basic operation

We have installed python and ran basic program with it.

How to edit Environment Variables on Windows.

  1. Go to search option and type “env” and select edit environment variables for your account

2. Click on new button and add variable name and path

3. Save your variable and it will appear in variables

4. You can add it in path of variable so command prompt will detect command and execute respective file present on that path

a) Select path variable and click on edit button.

b) Click on New button to add path

you can append path using other variable e,g.%JAVA_HOME%/bin

or you can give complete path

e.g. C:\Users\admin\AppData\Local\Programs\Python\Python38-32\

Testing path

open command prompt

type echo %variablename%

you can not see variables are printed and you can execute java and python command easily

P8 -Adding Pagination to ViewSets

Lets see how to add pagination to viewset. As will all other things in django, this is super easy

Assuming you have already done the the tutorial P6 – Django – Viewsets it is possible that we might need pagination on some of the rest calls. So we may have a lot of data on the server and sending it all at once back to the client may not be required and may not be the efficient way to handle the data. The client on the other hand might need only a handful of data and would not want all the data which will become difficult to process. So Lets see how do we set up pagination on view sets.

lets take example of rest services which returns “Ads” and it may have thousands of ads. At any point of time the client will need only a handful of them to process. So the client must be able to pass the rest service some parameter identifying the page number it desires.

For our example. We create a pagination.py file and add below content.


from rest_framework.pagination import PageNumberPagination

class PaginationMeta(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 10000

Here we have set three parameters. One is the “page_size” which means the amount of data the server will send if requested with page number i.e with parameter “page” . The next is “page_size_query_param” which can be used if want a specified number of records to be retrieved and override the page_size. “max_page_size” which is the max page size for our request.

The next step is to set this class in our viewset as below.

from django.shortcuts import render
from .models import Ads
from .serializers import AdsSerializer
from rest_framework import viewsets
from .pagination import PaginationMeta
# Create your views here.


class AdsView(viewsets.ModelViewSet):
    queryset = Ads.objects.all()
    serializer_class = AdsSerializer
    pagination_class = PaginationMeta


Here we set the pagination_class to the PaginationMeta we created. And this is all we will have to do to set up pagination. I am providing other classes required for this example.

from django.db import models

# Create your models here.

class Ads(models.Model):
    category = models.CharField(max_length=50)
    city= models.CharField(max_length=50)
    email = models.CharField(max_length=50)
    brand = models.CharField(max_length=50)
    make = models.CharField(max_length=50)
    capacity = models.CharField(max_length=50)
    desc = models.CharField(max_length=150)
    price = models.FloatField(max_length=20)
from rest_framework import serializers
from .models import Ads


class AdsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ads
        fields = ['id','category', 'city','email','brand','make','capacity','price','desc']
from rest_framework.routers import DefaultRouter
from . import views
from django.urls import path, include

router = DefaultRouter()
router.register('ads', views.AdsView)



urlpatterns = [
    path('', include(router.urls))
]

Now we have set the url pattern for our views to “/ads” so to access the views so a call to http://localhost:8000/api/ads/ will return the first page as below.

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 17,
    "next": "http://localhost:8000/api/ads/?page=2",
    "previous": null,
    "results": [
        {
            "id": 1,
            "category": "Cars & MotorCycles",
            "city": "Pune",
            "email": "",
            "brand": "MARUTI",
            "make": "Brezza",
            "capacity": "1.2",
            "price": 123213.0,
            "desc": "dadsadsad"
        },
        {
            "id": 2,
            "category": "Cars & MotorCycles",
            "city": "Pune",
            "email": "",
            "brand": "HYUNDAI",
            "make": "I20",
            "capacity": "1.2",
            "price": 34324234.0,
            "desc": "asdsadsads"
        },
        {
            "id": 3,
            "category": "Cars & MotorCycles",
            "city": "Pune",
            "email": "",
            "brand": "MARUTI",
            "make": "Brezza",
            "capacity": "1.2",
            "price": 122132.0,
            "desc": "sadsadad"
        }
    ]
}

As you can see, it has returned with the first page and also various others attributes to navigate to next page. We can jump to any page we wish by providing the page parameter to the url e.g. http://localhost:8000/api/ads/?page=4

Please let us know if you liked this post and if it helped you. Thank you for spending time reading my post.

Angular JS Data inbuilt Directives NgStyle and NgClass

In this Example we see inbuilt angularjs Directives

  1. NgStyle
  2. NgClass

NgClass and NgStyle

Lets go through basics first

NgClass


This directive is used when you want to select class of element on certain conditions

The syntax is: 

<h3 [ngClass]="{'myclass': flag}"> Change Class</h3>

NgStyle


This directive is getting used to apply multiple styles at a same time as array from variable or it can be also used to change style base on conditions

The syntax is:   

 <h3 [ngStyle]="{'color': flag  ? 'red':'green' }">Change Style</h3>

  1. Create your AngularJs Application

Please follow steps mentioned in below post

Hello World with Angular JS

Prerequisites Angularj JS new version is using nodejs for setting up application and for running applicaion you will also need any code editor tool. For current setup we are using visual code studio Kindly down load latest version of nodejs and visual code studio on your machine and complete setup Node Installation 1 Go to…

Create Components

In this example we have created multiple component which can be refereed in git repo

https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app

Use below command to generate these components

ng generate component <componentname>

For demonstrating this we have created below component in git project

ng generate component NgclassandngstyleComponent

Please refer:

https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app/ngclassandngstyle

Lets look at code of this one

ngclassandngstyle.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-ngclassandngstyle',
  templateUrl: './ngclassandngstyle.component.html',
  styleUrls: ['./ngclassandngstyle.component.css']
})
export class NgclassandngstyleComponent implements OnInit {

  constructor() { }
  flag = false;

  ngOnInit() {
  }

  changeClass() {
    this.flag = true;
  }

}


We have declared variable which is boolean and initial value is false and it will get changed when use clicks on button from UI and flag will become true

ngclassandngstyle.component.html

<p>ngclassandngstyle works!</p>

<h3 [ngClass]="{'blue-text text-darken-2': flag}"> Change Class</h3>
<h3 [ngStyle]="{'color': flag  ? 'red':'green' }">Change Style</h3>
<button (click) ="changeClass()"> Change Class</button>

In this code we are dynamically applying class and style to h1 element base on “flag” declared in component above. Initially its false so no class will be applied but style color will be green for false value.

Once use click button and “changeClass()” function will called value of flag will be true and class will get applied as “blue-text text-darken-2′” and style color will be red.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TwowaybindingwithNGModelComponent } from './twowaybindingwith-ngmodel/twowaybindingwith-ngmodel.component';
import { FormsModule } from '@angular/forms';
import { NgclassandngstyleComponent } from './ngclassandngstyle/ngclassandngstyle.component';

@NgModule({
  declarations: [
    AppComponent,
    TwowaybindingwithNGModelComponent,
    NgclassandngstyleComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Add Materialize css in index.html

In Index.html we will add materialize css and js file to use elegant UI

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularJSTwoWayDataBanding</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

  <!-- Compiled and minified JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
       
</head>
<body>
  <app-root></app-root>
</body>
</html>

We have added js file and css file specific to materilize css

https://materializecss.com/

Edit app.component.ts : This is parent component

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'AngularJSInBuiltDirectives';

}



Edit app.component.html

We have added ngmodel component in app.component.html

We have added all components mentioned above in tutorial .You can remove or add as per you need.

app.component.html

<app-ng-if-ng-for-ng-switch></app-ng-if-ng-for-ng-switch>

<div>
    <div class="card-panel   yellow lighten-3">
        <app-ngclassandngstyle></app-ngclassandngstyle>
    </div>
    <div class="card-panel    lighten-3">
        <app-twowaybindingwith-ngmodel></app-twowaybindingwith-ngmodel>

    </div>

</div>




Run Application

If you clone entire project then first run npm install so all dependencies will get downloaded.

Then run below command

ng serve --open

You will get below kind of output for your project once it get compiled and it will open your application in browser

Check Outpput

go to http://localhost:4200 path in browser

In main html we have imbedded all three components.

Lets see output of this

Now click button and check for class and style

After clicking on button it changes style and color of text

We have combined all above mentioned examples on single page in app.component.html and it will look like below on browser so you can play with all 3 components

Source Code:https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives

Angular JS NgIf and NgFor and NgSwitch

In this Example we will see inbuilt angularjs Directives

  1. NgIf
  2. NgFor
  3. NgSwitch

NgIf ,NgFor and NgSwitch

Lets go through basics first

NgIf


This directive is used when you want to display or remove an element based on a condition.

The syntax is: 

*ngIf=”<condition>”

NgFor


This directive is getting used to iterate through collection

The syntax is:    

<ul *ngFor =" let n of newcars">

NgSwitch


NgSwitch is like the JavaScript switch statement. It displays one element from among several possible elements, based on a switch condition. Angular puts only the selected element into the DOM.

NgSwitch is actually a set of three, cooperating directives: NgSwitchNgSwitchCase, and NgSwitchDefault as in the following example.

The syntax is:    

<ul *ngFor=" let c of newcars" [ngSwitch]="c.name">

            <li *ngSwitchCase="'city'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>
            <li *ngSwitchCase="'polo'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>

            <li *ngSwitchDefault class="text-success">{{ c.name }} {{ c.average }}
            </li>

        </ul>

Create your AngularJs Application

Please follow steps mentioned in below post

Hello World with Angular JS

Prerequisites Angularj JS new version is using nodejs for setting up application and for running applicaion you will also need any code editor tool. For current setup we are using visual code studio Kindly down load latest version of nodejs and visual code studio on your machine and complete setup Node Installation 1 Go to…

Create Components

In this example we have created multiple component which can be refereed in git repo

https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app

Use below command to generate these components

ng generate component <componentname>

For demonstrating this we have created below component in git project

ng generate component NgIfNgForNgSwitchComponent

Please refer:


https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app/ng-if-ng-for-ng-switch

Lets look at code of this one

ng-if-ng-for-ng-switch.component.html

<p>ng-if-ng-for-ng-switch works!</p>

<div class="row">

    <div class="col s3" style="border: brown; border-style: dashed; margin: 10px;">
        <h4>{{title}}</h4>

        <ul *ngFor=" let c of cars">

            <li> {{c}}</li>


        </ul>
    </div>
    <div class="col s3" style="border: brown; border-style: dashed; margin: 10px;">
        <h4>Display data with ngFor and ngIf</h4>

        <ul *ngFor=" let n of newcars">

            <li style="color:red" *ngIf="n.average < 7 "> {{n.name}}</li>

        </ul>

    </div>

    <div class="col s4" style="border: brown; border-style: dashed; margin: 10px;">
        <h4>Display data with Switch Directive</h4>
        <ul *ngFor=" let c of newcars" [ngSwitch]="c.name">

            <li *ngSwitchCase="'city'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>
            <li *ngSwitchCase="'polo'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>

            <li *ngSwitchDefault class="text-success">{{ c.name }} {{ c.average }}
            </li>

        </ul>
    </div>






</div>

In this code we are first printing all car names using for loop

In second for loop we are filtering cars whose average is less than 7.

In third block we are using switch case so we can apply different class to different cars based on name comparison

ng-if-ng-for-ng-switch.component.ts

import { Component, OnInit } from '@angular/core';
import { Car } from '../car';

@Component({
  selector: 'app-ng-if-ng-for-ng-switch',
  templateUrl: './ng-if-ng-for-ng-switch.component.html',
  styleUrls: ['./ng-if-ng-for-ng-switch.component.css']
})
export class NgIfNgForNgSwitchComponent implements OnInit {

  constructor() { }

  title = 'Display Cars';
  cars = ['city', 'nexon' , 'vento'];

  newcars = [
new Car('city', 10),
new Car('polo', 5),
new Car('vento', 6),
  ];

  ngOnInit() {
  }

}

In this component we are populating array of cars.

car.ts

export class Car {
    name: string;
    average: number;

    constructor( name , average) {
        this.name = name;
        this.average = average;
    }

}

In Above code we have declared car as class and with if we are filtering out some of cars based on use of different combination of ngIf,ngfor and ngswitch directives

After running this it will show below output

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TwowaybindingwithNGModelComponent } from './twowaybindingwith-ngmodel/twowaybindingwith-ngmodel.component';
import { FormsModule } from '@angular/forms';
import { NgclassandngstyleComponent } from './ngclassandngstyle/ngclassandngstyle.component';

@NgModule({
  declarations: [
    AppComponent,
    TwowaybindingwithNGModelComponent,
    NgclassandngstyleComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Add Materialize css in index.html

In Index.html we will add materialize css and js file to use elegant UI

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularJSTwoWayDataBanding</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

  <!-- Compiled and minified JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
       
</head>
<body>
  <app-root></app-root>
</body>
</html>

We have added js file and css file specific to materilize css

https://materializecss.com/

Edit app.component.ts : This is parent component

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'AngularJSInBuiltDirectives';

}



Edit app.component.html

We have added ngmodel component in app.component.html

We have added all components mentioned above in tutorial .You can remove or add as per you need.

app.component.html

<app-ng-if-ng-for-ng-switch></app-ng-if-ng-for-ng-switch>

<div>
    <div class="card-panel   yellow lighten-3">
        <app-ngclassandngstyle></app-ngclassandngstyle>
    </div>
    <div class="card-panel    lighten-3">
        <app-twowaybindingwith-ngmodel></app-twowaybindingwith-ngmodel>

    </div>

</div>




In main html we have imbedded all three components.

Run Application

If you clone entire project then first run npm install so all dependencies will get downloaded.

Then run below command

ng serve --open

You will get below kind of output for your project once it get compiled and it will open your application in browser

9. Check Outpput

go to http://localhost:4200 path in browser

This example demonstrated that ngIF and NgFor working fine

We have combined all above mentioned examples on single page in in git repo in app.component.html and it will look like below on browser so you can play with all 3 components

Source Code:https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives

Angular JS Data inbuilt Directives

In this Example we see inbuilt angularjs Directives

  1. NgIf
  2. NgFor
  3. NgSwitch
  4. NgModel
  5. NgStyle
  6. NgClass

  1. Create your AngularJs Application

Please follow steps mentioned in below post

Hello World with Angular JS

Prerequisites Angularj JS new version is using nodejs for setting up application and for running applicaion you will also need any code editor tool. For current setup we are using visual code studio Kindly down load latest version of nodejs and visual code studio on your machine and complete setup Node Installation 1 Go to…

2. Create Components

In this example we have created multiple component which can be refereed in git repo

https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app

Use below command to generate these components

ng generate component <componentname>

NgIf ,NgFor and NgSwitch

Lets go through basics first

NgIf


This directive is used when you want to display or remove an element based on a condition.

The syntax is: 

*ngIf=”<condition>”

NgFor


This directive is getting used to iterate through collection

The syntax is:    

<ul *ngFor =" let n of newcars">

NgSwitch


NgSwitch is like the JavaScript switch statement. It displays one element from among several possible elements, based on a switch condition. Angular puts only the selected element into the DOM.

NgSwitch is actually a set of three, cooperating directives: NgSwitchNgSwitchCase, and NgSwitchDefault as in the following example.

The syntax is:    

<ul *ngFor=" let c of newcars" [ngSwitch]="c.name">

            <li *ngSwitchCase="'city'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>
            <li *ngSwitchCase="'polo'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>

            <li *ngSwitchDefault class="text-success">{{ c.name }} {{ c.average }}
            </li>

        </ul>

For demonstrating this we have created below component in git project

ng generate component NgIfNgForNgSwitchComponent

Please refer:


https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app/ng-if-ng-for-ng-switch

Lets look at code of this one

ng-if-ng-for-ng-switch.component.html

<p>ng-if-ng-for-ng-switch works!</p>

<div class="row">

    <div class="col s3" style="border: brown; border-style: dashed; margin: 10px;">
        <h4>{{title}}</h4>

        <ul *ngFor=" let c of cars">

            <li> {{c}}</li>


        </ul>
    </div>
    <div class="col s3" style="border: brown; border-style: dashed; margin: 10px;">
        <h4>Display data with ngFor and ngIf</h4>

        <ul *ngFor=" let n of newcars">

            <li style="color:red" *ngIf="n.average < 7 "> {{n.name}}</li>

        </ul>

    </div>

    <div class="col s4" style="border: brown; border-style: dashed; margin: 10px;">
        <h4>Display data with Switch Directive</h4>
        <ul *ngFor=" let c of newcars" [ngSwitch]="c.name">

            <li *ngSwitchCase="'city'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>
            <li *ngSwitchCase="'polo'" class="blue-text text-darken-2">{{ c.name }} {{ c.average }}
            </li>

            <li *ngSwitchDefault class="text-success">{{ c.name }} {{ c.average }}
            </li>

        </ul>
    </div>






</div>

In this code we are first printing all car names using for loop

IN second for loop we are filtering cars whose average is less than 7.

In third block we are using switch case so we can apply different class to different cars based on name comparison

ng-if-ng-for-ng-switch.component.ts

import { Component, OnInit } from '@angular/core';
import { Car } from '../car';

@Component({
  selector: 'app-ng-if-ng-for-ng-switch',
  templateUrl: './ng-if-ng-for-ng-switch.component.html',
  styleUrls: ['./ng-if-ng-for-ng-switch.component.css']
})
export class NgIfNgForNgSwitchComponent implements OnInit {

  constructor() { }

  title = 'Display Cars';
  cars = ['city', 'nexon' , 'vento'];

  newcars = [
new Car('city', 10),
new Car('polo', 5),
new Car('vento', 6),
  ];

  ngOnInit() {
  }

}

In this component we are populating array of cars.

car.ts

export class Car {
    name: string;
    average: number;

    constructor( name , average) {
        this.name = name;
        this.average = average;
    }

}

In Above code we have declared car as class and with if we are filtering out some of cars based on use of different combination of ngIf,ngfor and ngswitch directives

After running this it will show below output

NgClass and NgStyle

Lets go through basics first

NgClass


This directive is used when you want to select class of element on certain conditions

The syntax is: 

<h3 [ngClass]="{'myclass': flag}"> Change Class</h3>

NgStyle


This directive is getting used to apply multiple styles at a same time as array from variable or it can be also used to change style base on conditions

The syntax is:   

 <h3 [ngStyle]="{'color': flag  ? 'red':'green' }">Change Style</h3>

For demonstrating this we have created below component in git project

ng generate component NgclassandngstyleComponent

Please refer:

https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives/src/app/ngclassandngstyle

Lets look at code of this one

ngclassandngstyle.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-ngclassandngstyle',
  templateUrl: './ngclassandngstyle.component.html',
  styleUrls: ['./ngclassandngstyle.component.css']
})
export class NgclassandngstyleComponent implements OnInit {

  constructor() { }
  flag = false;

  ngOnInit() {
  }

  changeClass() {
    this.flag = true;
  }

}


We have declared variable which is boolean and initial value is false and it will get changed when use clicks on button from UI and flag will become true

ngclassandngstyle.component.html

<p>ngclassandngstyle works!</p>

<h3 [ngClass]="{'blue-text text-darken-2': flag}"> Change Class</h3>
<h3 [ngStyle]="{'color': flag  ? 'red':'green' }">Change Style</h3>
<button (click) ="changeClass()"> Change Class</button>

In this code we are dynamically applying class and style to h1 element base on “flag” declared in component above. Initially its false so no class will be applied but style color will be green for false value.

Once use click button and “changeClass()” function will called value of flag will be true and class will get applied as “blue-text text-darken-2′” and style color will be red.

Lets see output of this

Now click button and check for class and style

You can see NgClass and NgStyle are working as expected

NgModel

  1. NgModel Overview

This directive allows you to display a data property and update that property when the user makes changes. You have to use FormModule and need to import in your module to use this.

This supports two way data binding in forms

2. Create Child Component

ng generate component twowaybindingwithngmodel

3. Edit twowaybindingwith-ngmodel.component.ts

In this component we have declared variables to demonstrate one way and two way binding initially both will have red color

When user will click button it will log color and we will see if it reflects in console as per change.

twowaybindingwith-ngmodel.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-twowaybindingwith-ngmodel',
  templateUrl: './twowaybindingwith-ngmodel.component.html',
  styleUrls: ['./twowaybindingwith-ngmodel.component.css']
})
export class TwowaybindingwithNGModelComponent implements OnInit {

  twowaycolor = 'red';
  onewaycolor = 'yellow';
  constructor() { }

  ngOnInit() {
  }

  click() {
    console.log('Two way color:' + this.twowaycolor);
  }

  clickme() {
    console.log('One way color:' + this.onewaycolor);
  }
}



4. Edit HTML File twowaybindingwith-ngmodel.component.html

Here we are adding button which will be get clicked to print color on console.

IN first block we have used two way binding with

Enter Color  : <input [(ngModel)] ='twowaycolor'> 

And in second blod we are using it for one way data binding

 Enter Color  : <input [ngModel] ='onewaycolor'> 

When in first block twowaycolor value get changed and will click button it will be get printed because data will be get passed from component to view and view to Component in bi-direction.

When in second block onewaycolor value get changed and will click button it will be print old value because new value from view is not getting passed to component as its using unidirectional data binding with [ngModel

twowaybindingwith-ngmodel.component.html

<p>One and Two way Binding with NG Model</p>
<div class="card-panel   blue ">
    <h2>Two way Binding</h2>
    Enter Color : <input [(ngModel)]='twowaycolor'>
    <br>
    Color value is: {{twowaycolor}}

    <br>
    <button (click)="click()"> CLickme</button>


</div>
<div class="card-panel   blue lighten-3">
    <h2>One way Binding</h2>
    Enter Color : <input [ngModel]='onewaycolor'>
    <br>
    Color value is: {{onewaycolor}}

    <br>
    <button (click)="clickme()"> CLickme</button>
</div>






Ng Model need to declare form module in app.module.ts else it will give you error .Kindly add FormsModule in app.module.ts so kindly add like below code base

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TwowaybindingwithNGModelComponent } from './twowaybindingwith-ngmodel/twowaybindingwith-ngmodel.component';
import { FormsModule } from '@angular/forms';
import { NgclassandngstyleComponent } from './ngclassandngstyle/ngclassandngstyle.component';

@NgModule({
  declarations: [
    AppComponent,
    TwowaybindingwithNGModelComponent,
    NgclassandngstyleComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

5. Add Materialize css in index.html

In Index.html we will add materialize css and js file to use elegant UI

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularJSTwoWayDataBanding</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

  <!-- Compiled and minified JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
       
</head>
<body>
  <app-root></app-root>
</body>
</html>

We have added js file and css file specific to materilize css

https://materializecss.com/

6. Edit app.component.ts : This is parent component

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'AngularJSInBuiltDirectives';

}



7. Edit app.component.html

We have added ngmodel component in app.component.html

We have added all components mentioned above in tutorial .You can remove or add as per you need.

app.component.html

<app-ng-if-ng-for-ng-switch></app-ng-if-ng-for-ng-switch>

<div>
    <div class="card-panel   yellow lighten-3">
        <app-ngclassandngstyle></app-ngclassandngstyle>
    </div>
    <div class="card-panel    lighten-3">
        <app-twowaybindingwith-ngmodel></app-twowaybindingwith-ngmodel>

    </div>

</div>




In main html we have imbedded all three components.

8. Run Application

If you clone entire project then first run npm install so all dependencies will get downloaded.

Then run below command

ng serve --open

You will get below kind of output for your project once it get compiled and it will open your application in browser

9. Check Outpput

go to http://localhost:4200 path in browser

We need to enter new color in text box

After editing color in both input boxes. Change only reflected in first color and not in second one that says first one is having two way data binding and second one is having one way data binding.

Now we will click on button to check value in component.

Below screenshot displays One and Two way data binding between variables using ng model

This example demonstrated that ng-Model can be used for one and two way binding.

We have combined all above mentioned examples on single page in app.component.html and it will look like below on browser so you can play with all 3 components

Source Code:https://github.com/letsblogcontent/AngularJS/tree/master/AngularJSInBuiltDirectives