Giter Site home page Giter Site logo

jacob119 / utility_scala Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yennanliu/utility_scala

0.0 0.0 0.0 17.34 MB

Scala programming language basic & functional programming (fp) & Spark Scala basic demo | #SE

Scala 97.71% Dockerfile 0.20% Perl 0.21% Shell 0.26% Python 1.07% Java 0.54%

utility_scala's Introduction

Utility_Scala

Introduction of Scala programming language as well as Spark Scala via couples of basic scripts in common use cases. Please check the Main files for more information.

Scala Projects

Note

Main Files

  1. Utility Scala - Scala scripts for utility

  2. Spark Scala Demo - Scala spark basic demo

  3. Scala basic - variable, data structure

  4. Scala basic - function, class, case class, constructor...

     // *** declare a Employer_03 instance, but transform it to Person_03 class
     val emp:Person_03 = new Employer_03
     // *** transform emp to Employer_03 class (asInstanceOf)
     emp.asInstanceOf[Employer_03].sayHello() // downward transform
  5. Scala class, trait, abstract class..

    • Class
      • Method create class instance in Scala
        • New className
        • Apply : example
        • Dynamic mixing : example
        • Anonymous sub class
    • Trait
      • Trait
      • TraitAbstract
      • TraitDemo2
      • TraitDemo3
      • TraitDemo4
      • TraitDemo5
      • TraitDemo6
      • TraitDemo7
      • TraitDemo8
      • TraitDemo9
      • TraitDemo10
        • Compare the ordering with trait construct ways
          • way 1) create class instance -> class hasn't been created yet when mixing trait
          • way 2) create "anonymous" sub class -> class already been created when mixing trait
      • TraitDemo11
      • TraitDemo12
      • TraitAsInterface
      • TraitAbstractOverwrite1
      • Scala trait : a "supplement" of scala Inheritance (scala only allows "single Inheritance", e.g. a class can only have one parent class), so trait offers more flexibility
      • Scala trait can do sth like "java interface"
      • trait can have both abstract method and regular method (a method that Not implemented in trait is the "abstract" method)
      • ALL Java interface can be used in scala as Scala trait
      • if there are "composition" traits (class className extends trait1 with trait2 with trait3 ...) when creating, then scala will
        • declare the instance from left to right
        • execute the method from right to left
      • Scala TraitDemo, Trait Abstract basic op, properties. NOTICE : Scala traits don’t allow constructor parameters (so that's why we use abstract class)
      • pattern
       // if no parent class
       class className extends trait1 with trait2 with trait3 ...
       //
       // if there is parent class
       class className extends parentclassName with trait1 with trait2 with trait3 ...
      • TraitMixin1
      • TraitMixin2
      • TraitMixin3
      • TraitMixin:
        • Can mixin trait when construct the object (class) => to extend functionality
        • also can implement on abstract class
        • TraitMixin only works in Scala (not Java)
        • Can do the extension but not change inheritance status
    • Abstract Class
      • Abstract Class
      • AbstractDemo
      • Scala also has a concept of an abstract class that is similar to Java’s abstract class. But because traits are so powerful, you rarely need to use an abstract class. In fact, you only need to use an abstract class when:
      • You want to create a base class that requires constructor arguments
      • Your Scala code will be called from Java code
    • Class VS Object 1, Class VS Object 2 - Compare Class, object difference, feature in Scala
    • Type Parameterlization Demo1 - Type Parameterlization in Scala
    • Apply
    • anonymousClass_java, anonymousClass- *** Override/implement abstract attribution/method.. in java & scala
    • Nest class
       // java example 1
       class Outer{  // outer class
       	class inner{  // inner class
       // code
       	}
       }
       class Other{ // outer other class
       // code
       }
       // java example 2
       class OuterClass{  // outer class
       	class InnerClass{ // inner class
       		publice void test (InnerClass ic){
       			System.out.Prlintln(ic);
       		}
       	}
       	static class StaticInnerClass {  // static inner class
       		// code
       	}
       }
  6. Scala object

    • LoadPackageDemo1, LoadPackageDemo2, LoadPackageDemo3, LoadPackageDemo4 - Scala load package examples

    • pkgObject - package can have class, object, and trait... but it CAN'T HAVE function, var... In order to solve it, scala offers the package object concept

    • Singleton pattern

    • Companion

      • CompanionDemo1
      • CompanionDemo2
      • CompanionDemo3
      • CompanionDemo4
      • CompanionDemo5
      • Companion is the combinaton of class static method/value.. + class basic method/value..
      • Scala Companion demo (An object that has the same name as a class is called a companion object of the class, and it is often used to contain factory methods for the class that it complements)
      • Since there is no static class/method.. in Scala, so Scala offers the Companion for similIar functionality
      • In development, we put the basic attribution, method ... in Companion class ; and we put the static consents in the Companion object

    • VisibilityDemo1 - extend package visibility. e.g. : private[ScalaBasic] val name = "jackkkk"

    • ImportPackage1 - Scala import package demo

  7. Scala implicit

  8. Scala OOP

    • Features : Encapsulation, Inheritance, Polymorphism

    • AbstractDemo1

    • encapsulationDemo1, encapsulationDemo2

      • encapsulation is one of the features in OOP, abstract the data and methods and encapsulate them, so only the "defined" method can be implimented to the data
      • Pros on encapsulation
        • hide the implementation details
        • validate the data, make it safe and feat the business needs
      • Steps do encapsulation:
        • step1) "private" the method/attributions
        • step2) offer "public" methods (e.g. : getter, setter or @BeanProperty...) (getter : set attribution value, getter : get attribution value)
    • OOP hw 1

  9. Scala Script example

  10. Scala exception, error handling

    • ExceptionDemo1
    • ExceptionDemo2 - format : try - catch - finally
      • there is NO compile exception in Scala (only java has), all exceptions in Scala happen during runtime. All exceptions inherit from the "throwable" class, which is the class with "nothing" type that is acceptable to all class/method...
  11. Others

  12. Spark basic

Quick Start

Quick start manually
# DEMO 1) run scala hello world 
$ git clone https://github.com/yennanliu/utility_Scala.git
$ cd utility_Scala
$ scala src/main/scala/UtilityScala/HelloWorld.scala 
#$ Hello World


# DEMO 2) run scala spark hello world via sbt 
$ cd utility_Scala
$ sbt package
$ sbt
# inside sbt console
sbt:Simple Project> run 
# [warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list

# Multiple main classes detected, select one to run:

#  [1] AnonymousFuncDemo
#  [2] ClassDemo
#  [3] FileIODemo
#  [4] ForLoopDemo
#  [5] FunctionChangeableParameterDemo
#  [6] FunctionCompositionDemo
#  [7] HelloWorld
#  [8] IfElseDemo
#  [9] OperatorDemo
#  [10] PatterMatchDemo
#  [11] SimpleApp
#  [12] Test
#  [13] UderDefinedDefaultParamFuncDemo
#  [14] UderDefinedFuncDemo

Enter number: 11

# [info] Running SimpleApp 
# ...
#  >>>>>>>>>>>>>> OUTPUT
# Lines with a: 21, Lines with b: 9
#  >>>>>>>>>>>>>> OUTPUT
# ...

# DEMO 3) run scala spark hello world
$ cd utility_Scala
$ sbt clean compile && sbt assembly 
$ spark-submit \
  --class "SimpleApp" \
  --master local[4] \
  target/scala-2.11/simple-project_2.11-1.0.jar
# REPL via sbt console
$ sbt
console
scala> 

# ✘ yennanliu@MacBook-Pro  ~/utility_Scala   master ●  
# ✘ yennanliu@MacBook-Pro  ~/utility_Scala   master ●  sbt  
# [info] Loading settings for project utility_scala-build from plugins.sbt ...
# [info] Loading project definition from /Users/yennanliu/utility_Scala/project
# [info] Loading settings for project utility_scala from build.sbt ...
# [info] Set current project to UtilityScala (in build file:/Users/yennanliu/utility_Scala/)
# [info] sbt server started at local:///Users/yennanliu/.sbt/1.0/server/ff2f518f2235c5fb0743/sock
# sbt:UtilityScala> console
# [info] Starting scala interpreter...
# Welcome to Scala 2.11.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_252).
# Type in expressions for evaluation. Or try :help.

# scala> import slick.driver.H2Driver.api._
# import slick.driver.H2Driver.api._

# scala> 
Quick start via Docker
$ git clone https://github.com/yennanliu/utility_Scala.git
$ cd utility_Scala
$ docker build . -t spark_env
$ docker run  --mount \
type=bind,\
source="$(pwd)"/.,\
target=/utility_Scala \
-i -t spark_env \
/bin/bash
Quick start via `Spark-submit`
# package the scala saprk scripts
$ sbt package
# list the current classes
$ ls target/scala-2.11/classes
# run ForLoopDemo
$ spark-submit \
  --class ForLoopDemo \
  target/scala-2.11/utilityscala_2.11-1.0.jar 
# run LambdaFuncDemo
$ spark-submit \
  --class LambdaFuncDemo \
  target/scala-2.11/utilityscala_2.11-1.0.jar 
# run spark_basic_demo_4
$ spark-submit \
  --class SparkBasic.spark_basic_demo_4 \
  target/scala-2.11/utilityscala_2.11-1.0.jar
# run MovieSimilarities
$ spark-submit \
  --class SparkBasic.MovieSimilarities \
  target/scala-2.11/utilityscala_2.11-1.0.jar 50 

Development

  • Trouble shooting

  • Clean cache (sbt) : in case when there is issue build the project via IntelliJ sbt

ls -al
rm .idea

scala-learn-material

scala-learn-material

Ref

Ref

utility_scala's People

Contributors

yennanliu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.