1. Hadoop
and
the
Data
Scien/st
第2回NHNテクノロジーカンファレンス
(2012/08/18)
Takahiro
Inoue
(@doryokujin)
Treasure
Data,
Inc.
Chief
Data
Scien/st
2. Introduc/on
• Takahiro
Inoue
(TwiFer:
@doryokujin
)
• Majored
in
Mathema/cs
• Chief
Data
Scien/st
@
Treasure-‐Data
• Leader
of
Japanese
MongoDB
Community,
Mongo
Masters
3.
4. Challenges
with
building
your
own
cloud
based
data
warehouse
Treasure Data High-Level Architecture
Log Data Spread Sheets
BI Tools
Application Data
Treasure Data
Subscribe
Data Warehouse SQL
td-agent Operational
3rd Party Data Interface Analytics
JDBC
ODBC
Databases
Sensor Data
Web/Mobile Data CLI
5.
6. Agenda:
Data
Scien/stの立場からのHadoop利用
〜4つの
Business
Goal
から考える〜
Hadoop
PlaVorm
Our
Business
Goals
Monitoring
BI
Tools
Data
Analysis
ML
/
Graph
Mining
思考フロー
1. Goalの設定:
上記の4つのBusiness
Goalの内,どれを実行するか
2. ツールの選択:
最適なツールの選択,その上で何を出すか(分析するか)の決定
3. 中間データ形式の選択:
ツールが使用するためのデータの構造・持ち方の検討
4. Hadoopの活用:
3.
を(HDFS/S3上の)元データからどのようにして取ってくるか
7. Intermediate
Business
What
Helps?
Data
Insight
Granularity
Aggregator
Batch
Process
Data
Interac/ve
Goal
Date,
Product
Category,
Area
Hive
Database
Monitoring
Human
Decision
PIG-‐2167
Pig
Cube
BI
Tools
User
Id
MapReduce
File
/
HDFS
Data
Analysis
Machine
Learning
Mahout
ML
/
Deep
Small
Auto
Graph
Mining
Hama/Giraph
8. Intermediate
Business
Aggregator
Batch
Process
Data
Interac/ve
Goal
Hive
Database
Monitoring
SQL-‐like Query
Language
Hadoop
Google
Cube
BI
Tools
Main
Topic
Tenjing
File
/
HDFS
Data
Analysis
Dremel
Ad-‐hoc
Queries
PowerDrill
BI
Tools
9. Why
Hive
is
Good
for
Data
Scien/st?
SQL-‐style
Query
Language
– ラーニングコストが低い
Monitoring
– 情報が多い(実行したいクエリの例を入手しやすい)
– Join,
Group
by,
Where
(Having)
の概念は馴染みやすい
JDBC/ODBC
Driver
– 大多数のBIツールがJDBC/ODBCに対応している
BI
Tools
– 独自ツールでも接続できる可能性が増す
For
R/Excel/SPSS
– 解析に必要なデータセットを素早く得られる
Data
Analysis
– Hiveの出力データ構造と解析ツールの入力形式の親和性
– 入力データ抽出→解析→入力データ修正→…
のイテレーションが容易
12. Monitoring
Intermediate
Data
Format
Very
Simple
Data
Format:
Tuple
or
Triplet
– Tuple:
{
dateYme,
numeric
value
},
– Triplet:
{
dateYme,
segment
name,
numeric
value
}
Database
Hive
Query
(Ex.
Hourly
Sales
Segmented
by
Item
)
SELECT
date,
segment,
COUNT(disYnct
userid)
FROM
some_metric
Date
Segment
Value
WHERE
1345262400
<=
Ymestamp
2012-‐08-‐18
13:00
Item
A
$10,000
AND
Ymestamp
<
1345266000
2012-‐08-‐18
13:00
Item
B
$8,000
2012-‐08-‐18
13:00
Item
C
$6,000
GROUP
BY
segment
…
…
…
2012-‐08-‐18
14:00
Item
A
$7000
16. Business
Intelligence
Intermediate
Data
Format
– Data
Cube:
dimension,level-‐of-‐detail
を固定することで各dimension
を次元軸に,値を各セ
ルに取ったData
Cubeを作成できる
Ex.
Cube
for
(
Country,
Car
Name,
Year
)
Products
Date
Car
Name
Level
Cell:
(
Prius,
Korea,
2005)
PRIUS
-‐-‐>
10,000
MARK
X
2007
FAIRLADY
Z
2006
Year
Level
ROAD
STAR
2005
Loca/on
USA
Canada
Japan
Korea
Country
Level
17. Business
Intelligence
Star
Schema
Dimension
tables
Fact
table
Loca/on
State
Date
Country
Hierarchy Month
Year
State
(level-of-detail)
Car
Name
Month
City
Profit
Day
Sales
Products
Payroll
Company
Marke/ng
Car
Type
Inventory
Car
Name
Margin
...
Hierarchy
Car
18. Business
Intelligence
Cube
Lacce
(Loca/on
+
Company)
Hive
Query
Algebraic
Measure:
f(x+y)
=
f(x)
+
f(y)
Most
Detailed
<city,
car
name>
(SUM,
STD_DEV)は,最下層のdimension
の組合せ集計のみ保持しておけば,上位
階層は和をとる事で導出可能。
<state,
car
name>
<city,
car
type>
GRUP
BY
country,
state,
city,
company,
car_type,
car_name
<country,
car
name>
<state,
car
type>
<city,
company>
Holis/c
Measure
(TOP-‐K,
MODE,
MEDIAN)は,全ての層で
<*,
car
name>
<country,
car
type>
<state,
company>
<city,
*>
集計を行う必要がある。
GROUP
BY
country,
state,
city,
<*,
car
type>
<country,
company>
<state,
*>
company,
car_type,
car_name
UNION
ALL
GROUP
BY
country,
state,
city,
<*,
company>
<country,
*>
company,
car_type
…
UNION
ALL
<*,
*>
GROUP
BY
Least
Detailed
country,
company
…
19. Business
Intelligence
*
Date
Dimension
はツール側で自動的にlevelを考慮してくれる
Intermediate
Data
Format
– For
Algebraic
Measures
Algebraic
Most
Detailed
Dimensions
Measures
County
State
City
Company
Car
Type
Car
Name
Date
Units
Sales
Most
Detailed
Dimensions
USA
California
San
Jones
TOYOTA
Sedan
Corolla
2012-‐08-‐15
36
$3,000
<city,
car
name>
USA
California
Palo
Alto
TOYOTA
Sedan
Alion
2012-‐08-‐15
24
$2,000
USA
California
Los
Altos
NISSAN
SUV
X-‐TRAIL
2012-‐08-‐16
100
$1,000
USA
New
York
ManhaRan
NISSAN
Sport
FAIRLADY
Z
2012-‐08-‐16
64
$500
Canada
Alberta
Airdrie
MAZDA
Sport
Road
Star
2012-‐08-‐15
4
$3,000
LocaYon
Hierarchy
Products
Hierarchy
Date
Hierarchy
Holis/c
– For
HolisYc
Measures
(tend
to
become
very
large
table)
Measure
<city
,
car County
State
City
Company
Car
Type
Car
Name
Date
Avg
of
Top
20
nam
<sta e>
te,
c USA
California
San
Jones
TOYOTA
Sedan
Corolla
2012-‐08-‐15
$3,600
ar
n
<cou ame
nty, >
USA
California
ALL
TOYOTA
Sedan
Alion
2012-‐08-‐15
$2,400
car
<*,
c nam
eUSA
>
ALL
ALL
NISSAN
SUV
X-‐TRAIL
2012-‐08-‐16
$1,000
ar
n
<cou ame
ntry >
ALL
ALL
ALL
NISSAN
Sport
FAIRLADY
Z
2012-‐08-‐16
$640
,
car
<cou
typ
ntry eUSA
>
California
San
Jones
TOYOTA
Sedan
ALL
2012-‐08-‐15
$3,600
,
car
nam
<cou e>
USA
California
San
Jones
TOYOTA
ALL
ALL
2012-‐08-‐15
$1,100
ntry
,
*>
USA
California
San
Jones
ALL
ALL
ALL
2012-‐08-‐15
$2,300
…
…
…
…
…
…
…
…
<*,
*
>
ALL
ALL
ALL
ALL
ALL
ALL
ALL
$720
20. Business
Intelligence
Problem
– Dimension
+
Level
の組合せによる (1)
計算量・(2)
レコード数の爆発に注意
– HiveはCube生成のための関数群を持たないため,UNION
ALL
を多用するので
(3)
SQLが長くなる
– (4)Cubeが大きければBI側の操作も遅くなる
– 必要な Dimension,
Level
を適切に定義することは重要
– BI
ツールは比較的高価なので,インフラ側のコストだけに気を取られないように
Solu/on
for
(1):
MR
CUBE
– MapReduceを利用して効率良くHolisYc
MeasuresのCubeを求める方法
– MR-‐CUBE:
Distributed
Cube
MaterializaYon
on
HolisYc
Measures
– MR
CUBEは次のポイントを抑えた
2-‐phase
cube
materializaYon
algorithm:
1.
Par/ally
Algebraic
Measures
(Subset
of
HolisYc
Measures)
の導入
2.
Value
Par//oning
と Batch
Area
Iden/fica/on
テクニックの利用
– Pig
にも実装提案。 MR-‐Cube
implementaYon
(pig-‐2831)
21. Business
Intelligence
Polaris:
Visual
Abstrac/on
– A
UI
for
exploraYon,
analysis
for
Data
Cube
– A
formal
Language
for
specifying
queries
&
visualizaYons
Data
abstrac/on
VIsual
abstrac/on
hRp://www.graphics.stanford.edu/projects/polaris/infovis2002.ppt
22. Business
Intelligence
Example:
Tableau
–
大多数の企業の導入実績(Zynga,
Nokia,
eBay,
etc…)
–
Polaris:
Visual
AbstracYon
⇔
Cube:
Data
AbstracYon
–
豊富なチャートライブラリとデータソースコネクタを持つ
23. Business
Intelligence
Reference
– 2012
Gartner
Magic
Quadrant
Report
– The
Forrester
Wave(TM):
Advanced
Data
VisualizaYon
(ADV)
Plavorms,
Q3
2012
Gartner
Magic
Quadrant
Report
24. Google’s
Solu/on
Cube
OperaYon
Tenjing
Cube
Dremel
eries
Ad-‐ hoc
Qu
BI
Tools
PowerDrill
25. Tenjing:
SQL
Implementa/on
on
MR
Solu/on
for
(3):
Tenjing
– Tenzing:
A
SQL
ImplementaYon
on
The
MapReduce
Framework
– MapReduce
Enhancements
• Worker
Pool:
(1)
master
watcher
/
(2)
master
pool
/
(3)
worker
pool
• Streaming
&
In-‐memory
Chaining:
中間データをGFSに保存せず,Streaming
でMR間をつなぐ
• Sort
Avoidance:
Hash
Join
や
Hash
AggregaYon
などではSortしない
• Block
Shuffle:
Row
SerializaYon,
DeserializaYon
の効率化
– 主にGoogle内の非エンジニアコミュニティが使用
– 1000
Users,
2
Data
Centers,
2000
Cores,
1.5PB
of
Compressed
Data
– Hiveよりも進んだSQL
Extensionを実装:
1. HASH
BASED
AGGREGATION
• Reduce上でのsortが不要に
→
パフォーマンスの向上
2. Analy/c
Func/ons
• RANK,
LEAD,
LAG,
NTILE
3. OLAP
Extensions
• ROLLUP,
CUBE
→
Cubeの作成が容易に
26. Dremel:
Interac/ve
Analysis
of
Large-‐Scale
Datasets
message Document {
required int64 DocId;
Solu/on
for
(4):
Dremel
optional group Links {
repeated int64 Backward;
repeated int64 Forward;
– Dremel:
InteracYve
Analysis
of
Web-‐Scale
Datasets
}
repeated group Name {
repeated group Language {
– Scalable,
InteracYve
Ad-‐hoc
Query
System
required string Code;
optional string Country;
}
– Nested
Columnar
Storage
optional string Url;
}
• 各レコードをColumnar
Formatへ変換
}
• 構造情報を保持するための2つの値:
(1)
RepeYYon
Levels
(2)
DefiniYon
Levels
– SQL-‐like
query
language
DocId: 10
Links
• Nested
Data
Support
Forward: 20
Forward: 40
• MulY-‐level
ExecuYon
Trees
Forward: 60
Name
• Read-‐only
Ad-‐hoc
Query
Language
Code: 'en-us'
Country: 'us'
Big
Query
Language
Code: 'en'
Url: 'http://A'
– Hosted
Dremel
(Dremel
as
a
Service)
Name
Url: 'http://B'
Name
– WHERE,
AggregaYon
FuncYons,
サブクエリ、中間テーブル作成,
部分 Language
Code: 'en-gb'
一致検索、部分的なJOIN(相手テーブルが数MB以下のもの)
Country: 'gb'
– Google
Cloud
Storage
へのデータインポート
DocId: 20
Links
• Nested
Data
Format非対応
Backward: 10
Backward: 30
• 現状はCSVファイルのみ,スキーマ定義必須
Forward: 80
Name
Url: 'http://C'
27. PowerDrill:
Alterna/ve
Columnar
Storage
Solu/on
for
(4):
PowerDrill
– Processing
a
Trillion
Cells
per
Mouse
Click
– InteracYve
Ad-‐hoc
Query
System
(Dremel
とは違ってNested構造は扱わない)
– Google内ではInteracYveなWebUIを実現するために使用。(マウスのクリックによるド
リルダウン操作が瞬時にSQLクエリに変換されPowerDrillを通じて瞬時に結果を返す)
– AnalyYcsに関して,次の仮定に基づいた設計:
• 大量のデータをスキャンする必要はあるが,ほとんどのデータは使用されない
• The
majority
of
queries
are
fairly
discriminaYve,
similar,
uniform
→
キャッシュが有効に使える
– Composite
Range
ParYYoning:
• データインポート時にデータを細かいChunkに分割
• カラム毎に,値特定のために2つのdictを使用:
(1)
global-‐dicYonary
/
(2)
chunk-‐dicYonary
• これらによってQuery実行時にChunkを読み込むかスキップするかを瞬時に判断
• 統計的に95%のChunkがスキップされる→瞬時に結果を返すことが可能
• Chunk単位で圧縮してメモリーキャッシュして再利用
28. 3.
Data
Analysis
hRp://www.r-‐project.org/
hRp://www-‐06.ibm.com/soUware/jp/analyYcs/spss/products/staYsYcs/
hRp://numpy.scipy.org/
hRp://www.gigawiz.com/
30. Data
Analysis
Example:
Survival
Analysis
For
Web
Service
– ユーザーの「退会」というイベントが発生するまでの時間(生存時間)に関する分析
– この分析によって以下のような事がわかる:
• 「入会からnヶ月後にはx%のユーザーが辞めてしまう」
• 「m1〜m2日目の間がユーザーの退会リスクが高い」
• 「セグメントAはBに比べて生存時間が長い(優良顧客である)」
– 特定の調査期間でイベントを観測できるユーザーと,観測できない(調
査以降も生存している:右側打ち切り)ユーザーの2種類のパターンが
存在。双方を考慮して,推定を行う必要がある
31. Data
Analysis
Intermediate
Data
Format
Userid
Time
To
Event
(Days)
Flag
Event
Occurred
Device(Segment
)
Country(Segment
)
000001
12
1
Smart
Phone
Japan
000002
36
0
PC
USA
000003
120
0
Smart
Phone
USA
…
…
…
…
…
100000
1
1
PC
Japan
File
/
HDFS
Hive
Query
SELECT
uid,
datediff(latest_login,
registered_day)
AS
Yme_to_event,
flag_event_occurred,
device,
country
…
FROM
…
#
event
occurred
UNION
ALL
…
#
!event
occurred