High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC...

33
High Productivity Big Data Analytics with SparkR 2015. 6. 21 종합기술원/NIC기술원 정상오

Transcript of High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC...

Page 1: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

High Productivity Big Data Analytics with SparkR

2015. 6. 21종합기술원/NIC기술원

정 상 오

Page 2: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

목차

l R

§ 장단점 및 특징

l Spark

§ Ecosystem

§ RDD & DataFrame

§ MLlib

l SparkR

§ Architecture & Data Flow

§ SparkR Examples

§ Big Data Analytics with GLM

§ Scalable Big Data Visualization with ggplot2.SparkR

2

Page 3: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

R

l R 이란: 오픈소스 기반의 통계적 처리를 위한 프로그래밍 언어

(Flexible, extensible and comprehensive for high productivity)

l R 의 장점

§ data.frame 구조로 사용자가 데이터 조작을 용이하게 함

§ 다양한 시각화 툴이 존재하여 데이터에 대한 특성을 파악하기 용이

§ 6,000 개 이상의 용도별 패키지(package) 존재로 몇 라인으로 원하는 작업 수행

§ 학계와 연구개발에 점점 더 널리 사용되고 있음

(2 백만 이상의 data scientists, statisticians and analysts 등이 사용)

§ 활발한 오픈소스 커뮤니티 활동

§ Free software (no cost)

§ 다른 많은 free 혹은 상용 툴들과 integration 가능

§ 다양한 형태의 데이터 입력을 위한 다수의 커넥터 지원

§ 분석 (analytics) 뿐만 아니라, data discovery, 리포트 작성 등에도 활용 가능

3

Page 4: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

R

l R 의 단점

§ 처리속도 한계: Single thread만 지원 (multiple thread 실행 불가)

§ Single machine 에 맞는 데이터만 처리 가능 (빅데이터 처리 불가)

§ 학습이 다른 소프트웨어에 비해 다소 어려운 편

§ Help 파일이 비교적 고급 사용자들에게 용이하게 만들어졌음

§ 너무나 많은 패키지들이 존재하여, 적합한 것을 찾기가 쉽지 않음

§ R 코어는 안정적이나, 때때로 어떤 패키지들의 변화나 종속성이 업데이트 안됨

§ 웹 기반 애플리케이션과의 통합이 아직 부족

4

Page 5: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

R among Top 10 Programming Languages

2014년2015년

Source: http://spectrum.ieee.org/computing/software/the-2015-top-ten-programming-languages

5

Page 6: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

R: Data import

Source: Telefonica – R Tools Evaluation (2015)6

Page 7: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

Spark

Source: https://databricks.com/spark/about

l Spark 이란?

§ 빠른 속도와 사용 편의성, 그리고 정교한 분석이 가능한 오픈소스 데이터 처리 엔진

§ UC Berkeley 에서 2009 년에 개발

§ 250 개 이상의 조직에서 1,000명 이상의 Contributor들 참여

l Spark Ecosystem

Source: Databricks – Apache Spark Analytics Made Simple (2016)

7

Page 8: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

Spark: Ecosystem Components

l Spark SQL

§ 구조적 데이터 처리를 위한 Spark 모듈

§ DataFrames 라는 프로그래밍 abstraction 을 제공

§ 분산 SQL query 엔진l Spark Streaming

§ 작은 단위의 batch로 데이터를 나누어 순차적으로 처리

§ 스트리밍과 저장 데이터에 대한 application이 가능하도록 함

§ 다양한 데이터 소스 (HDFS, Flume, Kafka, and Twitter 등)와 결합l MLlib

§ Java, Scala, Python 등으로 사용 가능한 scalable한 머신러닝 라이브러리

§ High-quality 알고리즘들로, MapReduce 보다 100배까지도 빠른 속도l GraphX

§ 대용량 그래프 구조의 데이터를 만들고, 변환 및 처리할 수 있는 그래프 계산엔진l Spark Core

§ Spark 플랫폼의 근원을 이루며, 다른 모든 기능들이 토대를 두는 일반 실행 엔진

§ 인메모리 컴퓨팅 방식으로 빠른 처리 속도 가능8

Page 9: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

Spark: RDD & DataFrame

Source: IEEE BigData 2015 –Conquering Big Data with Spark

l 1. RDD (Resilient Distributed Datasets)

§ 대형 클러스터에서 fault-tolerant한 방식으로 in-memory 컴퓨팅을 가능하게 해주는

분산형 메모리 abstraction (< Spark 1.4)

§ Collections of objects distributed across a cluster (Stored in RAM or on Disk)l 2. DataFrame (>= Spark 1.4)

§ RDD에서처럼 분산형, 병렬처리에 사용 가능

§ RDD보다 차원 높은 구조: 데이터에 대한 쉬운 접근과 조작이 용이

§ Distributed collection of data grouped into named columns (i.e. RDD with schema)• 메타데이터, column명, 데이터 유형 등 지원

§ Available in Python, Scala, Java, and R

9

Page 10: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

Spark: MLlib

Source: https://spark.apache.org/docs/latest/mllib-guide.html

l MLlib 이란?

§ Spark의 코어 머신 러닝 library

§ 간단한 개발환경

§ Scalable 하고 빠름

§ Scala, Python, 및 Java로 활용

§ SparkR (R on Spark) 로 가능한

알고리즘 제한적: GLM

§ Streaming, Spark SQL, GraphX

와도 사용 가능

(Spark 1.6.1)

10

Page 11: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR (R on Spark)

l SparkR 이란

§ R환경에서 Apache Spark을 사용하기 위한 하나의 R 패키지

§ 경량의 R frontend 제공, 사용자는 R명령어를 통해 분산환경에서 job들을 수행 가능

§ 자체의 실행 shell이 있으나, RStudio에서 표준 R 패키지로서 import 해서 사용 가능

§ Spark 작업을 위해 새로운 프로그래밍 언어를 배울 필요가 없음

§ R에서 이미 사용되고 있는 표준화된 구조로서 자연스런 확장

: ‘$’ 나 ‘[ ]’를 사용한 column access

l 개발자 community

§ UC 버클리 AMPLab에서 시작, Alteryx, Intel, Databricks등 ( > 20 Contributors)

§ Spark 패키지들과의 integration 수행중

§ 참고: https://amplab-extras.github.io/SparkR-pkg/

11

Page 12: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR

l SparkR SW Stack

분산 컴퓨팅 엔진 + High-level 프로그래밍 언어

l SparkR 개념

Source: Databricks12

Page 13: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Atchitecture & Data flow

R SparkContext

JavaSpark

Context

RSparkExecutor

JNI

Worker

exec

RSparkExecutor

Worker

exec

Local

SparkR 패키지 로딩

사용자

R IDE (ex. Rstudio)

R-JVM bridge

(JVM)

R Excutables

(JVM)

(JVM)

R 프로세스

Source: Databricks13

Page 14: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Example Program

Source: Databricks14

Page 15: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Example API’s

Source: Databricks15

Page 16: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

R 과 SparkR 의 주요 특징 비교

R SparkR

데이터 크기 § 해당 노드의 메모리 크기만큼 § 사용되는 모든 노드들의 메모리의 합만큼 (빅데

이터 처리에 적합)

분산 컴퓨팅 § single machine & single thread:

“parallel” 패키지 존재하나, single

machine 에서의 multicore 용임

§ multiple machines & multiple threads (빅데이

터 처리에 적합)

API 의 범용성 § 통계 분석 및 다양한 데이터 처리

를 위한 다수의 패키지 존재

§ 제한된 처리방법과 데이터 타입만 지원

ü 제한된 SQL query와 dplyr-like 패키지 API

ü 사용자 정의 함수를 SparkR과 같이 사용불가

ü 오직 DataFrame 구조만 가능 (Spark 1.4)

Usability § 데이터 입력에 대한 필요성: csv, JSON, JDBC 같은 데이터를 읽을 수 있음

§ 데이터 조작을 위한 High-level API 존재

§ schema에 대한 지원

§ dplyr-like syntax

16

Page 17: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Big Data Analytics with GLM

Source: Databricks

l Generalized Linear Model (GLM)

§ Linear least squares 와 logistic regression 은 GLM의 special case.

§ GLM 은 다음의 두 가지로 규정됨

• A distribution of the response (from the exponential family),

• A link function g such that

§ Maximizes the sum of log-likelihoods

17

Page 18: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Big Data Analytics with GLM

§ m observations:

§ x: explanatory variables, y: dependent variable

§ x 와 y 사이의 선형성을 가정:

§ minimizes the sum of the squares of the errors:

l Linear least squares

Source: Databricks18

Page 19: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

§ Binary classification에 유용:Ex) true/false, clicked/not clicked, liked/disliked

§ Logistic 함수를 사용하여 likelihood를 나타냄:

§ Maximizes the sum of the log-likelihoods, i.e.,

l Logistic regression

SparkR: Big Data Analytics with GLM

Source: Databricks19

Page 20: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Big Data Analytics with GLM

Source: Databricks

l Distributions and link functions

20

Page 21: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR: Big Data Analytics with GLM

Source: Databricks

21

Page 22: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR for Big Data Visualization

l A Business Unit at SK Telecom§ Typical DB size: 70M records with 330 columns§ Analyzes the DB using R on a single-node scale-up server§ Has much bigger DB’s generated by network data

l Needs from the Unit for Big Data Visualization§ Use R for visualization of big data§ More scalable solution for bigger DB’s

ggplot2

ggvis

basegraphics

igraph

R Has Great Visualization Packages, but these packages cannot process Spark DataFrames.

Source: Spark Summit East 201622

Page 23: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR for Big Data Visualization: ggplot2 in R

l (Arguably) the most popular visualization package for R§ Maximizes the Based on the “layered” grammar of graphics§ Making it easy to produce high-quality graphs§ Single node processing

Source: http://www.computerworld.com/article/2920117/business-intelligence/most-downloaded-r-packages-last-month.html

Top package for visualization

May, 2015Source: ggplot2 documentation, http://docs.ggplot2.org/current

ggplot2 Example Plots

23

Page 24: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR for Big Data Visualization: ggplot2.SparkR = SparkR+ggplot2!

l An R package extending ggplot2 to visualize big data represented in Spark DataFrame

l http://skku-skt.github.io/ggplot2.SparkR/l https://github.com/SKKU-SKT/ggplot2.SparkR/l https://spark-summit.org/east-2016/events/ggplot2sparkr-rebooting-

ggplot2-for-scalable-big-data-visualization/

ggplot2.SparkR

ggplot2R data.frame

Variety of graphs

Simple and easy API

Spark DataFrame

Scalability

Distributedprocessing

Source: Spark Summit East 2016

24

Page 25: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR for Big Data Visualization: ggplot2.SparkR Simplifies Plotting

Example: Draw a histogram using DataFrame

BEFORE# Pre-processing Spark DataFrame using SparkR API

range <- select(df, min(df$Price), max(df$Price))

breaks <- fullseq(range, diff(range / binwidth))

left <- breaks[-length(breaks)]; right <- breaks[-1]

breaks_df <- createDataFrame(sqlContext, data.frame(left = left, right = right))

histogram_df <- join(df, breaks_df, df$Price >= breaks_df$left & df$Price < breaks_df$right, “inner”)

histogram_df <- count(groupBy(histogram_df, “left”, “right”))

# Draw histogram chart using ggplot2 API

ggplot(collect(histogram_df), aes(xmin = left, xmax = right, ymin = 0, ymax = count)) + geom_rect()

AFTER# It just takes one line!

ggplot(df, aes(x = Price)) + geom_histogram()

Source: Spark Summit East 201625

Page 26: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR: Supported Graph Types and Options

Name Descriptions

Graph types

geom_bar Bars, rectangles with bases on x-axis.

geom_histogram

Histogram.

stat_sum Sum unique values.

geom_boxplot

Box and whiskers plot.

geom_bin2d

Heatmap of 2d bin counts.

geom_freqpoly

Frequency polygon.

Name Descriptions

Positions

position_stack

Stack overlapping objects on top of one another

position_fill

Same as above, but the range is standardized.

position_dodge

Adjust position by dodging overlaps to the side

Facets

facet_null

Facet specification: a single panel

facet_grid

Lay out panels in a grid

facet_wrap

Wrap a 1d ribbon of panels into 2d

Scales

scale_x_log10

Put x-axis on a log scale

scale_y_log10

Put y-axis on a log scale

Name Description

Coords

coord_cartesian

Cartesian coordinates

coord_flip Flip cartesian coordinates

Ranges

xlim Set the ranges of the x axis

ylim Set the ranges of the y axis

Texts

xlab Change the label of x-axis

ylab Change the label of y-axis

ggtitle Change the graph title

Source: Spark Summit East 201626

Page 27: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR: Supported Graph Types

stat_sum

geom_boxplot

geom_bin2d

geom_freqpoly

Source: Spark Summit East 201627

Page 28: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR: Supported Graph Options

position_stack position_fill

position_dodge facet_grid facet_wrap

1 2 3

4 5 6

7 8 9

a b c

1

2

3

a1 b1 c1

a2

a3

b2

b3

c2

c3

Source: Spark Summit East 201628

Page 29: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR: Architecture

Three-stage pipeline:

• Parameter extraction

– x, y, colour, facet, scale, geom, etc.

• Data processing– Get data from the original source

– Process data using graph parameters

• Plotting– Draw graphs on display windows

R data.frame orSpark DataFrame

Parameter extraction

Dataprocessing(R data.frame)

Plotting

Parameter extraction

g Dataprocessing(Spark DataFrame)

ggplot2.SparkRggplot2

Input Checking

Source: Spark Summit East 2016

29

Page 30: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR : Data Flow (1)

R SparkContext

JavaSpark

Context

SparkExecutor

Worker

SparkExecutor

Master

Worker

PartitionedDataFrame

NewDataFrame

PartitionedDataFrame

NewDataFrame

FunctionsBaseobject

JNI

Graph type

Parameters

Layer option

ggplot2.SparkR

Graph function

Source: Spark Summit East 2016

30

Page 31: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR: Data Flow (2)

SparkExecutor

SparkExecutor

R SparkContext

JavaSpark

Context

Worker

Master

Worker

CombinedDataFrame

ggplot2.

SparkR

PartitionedDataFrame

PartitionedDataFrame

JNI

Rdata.frame

Plottingfunctions

Source: Spark Summit East 2016

31

Page 32: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

ggplot2.SparkR Performance: Varying Data Size

l Throughput (inverse of slope) remains relatively stable

Largest data for R single node

Based on 12 runs

Source: Spark Summit East 2016

32

Page 33: High Productivity Big Data Analytics with SparkRC1%A4%BB... · 2016-06-13 · §UC 버클리AMPLab에서시작, Alteryx, Intel, Databricks등( > 20 Contributors) §Spark 패키지들과의integration

SparkR Summary & Future

l 추가될 특징들

§ 더 많은 column 함수들 (ex. math, strings)

§ UDF (User-Defined Function) support: DataFrame내의 특정 Cell에 대한 사용자 정의

된 R함수의 실행.

§ SparkR에 대한 간단한 병렬처리 가능한 API (Ex. Parameter Tuning, Model Averaging)

§ R로 Spark MLlib (Machine Learning library) 호출/실행

§ R 프로세스들에 대한 Daemon (새로운 프로세스 forking보다 백그라운드 실행) 제공

l 더 많은 기여가 가능

§ Spark MLlib 으로부터 더 많은 SparkR 알고리즘 support가 필요

§ ggplot2.SparkR 에 대한 더 많은 graph types 와 options 추가 필요

§ SparkR 관련 분야는 contribution 하기가 상대적으로 수월함

l Summary

§ SparkR 을 사용하여, R환경에서 생산성 향상을 위한 빅데이터 처리 가능

§ 빅데이터에 대해, SparkR로서 제한된 형태의 머신러닝 및 시각화 가능

33