34
Hadoop and the Data Scien/st Takahiro Inoue (@ doryokujin ) Treasure Data, Inc. Chief Data Scien/st 2NHNテクノロジーカンファレンス (2012/08/18)

Hadoop and the Data Scientist

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Hadoop and the Data Scientist

Hadoop    and    

the  Data  Scien/st

Takahiro  Inoue  (@doryokujin)  Treasure  Data,  Inc.  Chief  Data  Scien/st

第2回NHNテクノロジーカンファレンス  (2012/08/18)

Page 2: Hadoop and the Data Scientist

Introduc/on

•  Takahiro  Inoue  (TwiFer:  @doryokujin  )  

• Majored  in  Mathema/cs  

•  Chief  Data  Scien/st  @  Treasure-­‐Data  

•  Leader  of  Japanese  MongoDB  Community,  Mongo  Masters  

Page 3: Hadoop and the Data Scientist
Page 4: Hadoop and the Data Scientist

Challenges  with  building  your  own  cloud  based  data  warehouse  

Subscribe

Spread Sheets

BI Tools

Operational Analytics

Databases

CLI

Treasure Data Data Warehouse

td-agent SQLInterface

JDBCODBC

Treasure Data High-Level Architecture

Log Data

Application Data

3rd Party Data

Sensor Data

Web/Mobile Data

Page 5: Hadoop and the Data Scientist
Page 6: Hadoop and the Data Scientist

Agenda:  Data  Scien/stの立場からのHadoop利用    

思考フロー  1.  Goalの設定:  上記の4つのBusiness  Goalの内,どれを実行するか  

2.  ツールの選択:   適なツールの選択,その上で何を出すか(分析するか)の決定  

3.  中間データ形式の選択:  ツールが使用するためのデータの構造・持ち方の検討  

4.  Hadoopの活用:  3.  を(HDFS/S3上の)元データからどのようにして取ってくるか  

Monitoring BI  Tools ML  /  Graph  Mining Data  Analysis

〜4つの  Business  Goal  から考える〜    

Our  Business  Goals  Hadoop  PlaVorm

Page 7: Hadoop and the Data Scientist

Database

Cube

File  /  HDFS

Hive

Pig

MapReduce

Mahout

Hama/Giraph

Monitoring

BI  Tools

ML  /    Graph  Mining

Data  Analysis

Data  Insight Deep

Date,  Product  Category,  Area     User  Id

Granularity

Small

Auto

What  Helps?

Human  Decision

Machine  Learning

Intermediate  Data Aggregator Business  

Goal

PIG-­‐2167

Batch  Process Interac/ve

Page 8: Hadoop and the Data Scientist

Database

Cube

Hive Monitoring

BI  Tools

Intermediate  Data Aggregator Business  

Goal

Tenjing

Dremel

PowerDrill

Ad-­‐hoc  Queries

Batch  Process Interac/ve

Hadoop

Google

SQL-­‐like 

Que

ry  Lan

guage

Database

Cube

File  /  HDFS Data  Analysis

BI  Tools

Main  Topic

Page 9: Hadoop and the Data Scientist

Why  Hive  is  Good  for  Data  Scien/st?  

Monitoring

BI  Tools

Data  Analysis

SQL-­‐style  Query  Language  –  ラーニングコストが低い  –  情報が多い(実行したいクエリの例を入手しやすい)  –  Join,  Group  by,  Where  (Having)  の概念は馴染みやすい  

 

JDBC/ODBC  Driver  –  大多数のBIツールがJDBC/ODBCに対応している  –  独自ツールでも接続できる可能性が増す  

For  R/Excel/SPSS  –  解析に必要なデータセットを素早く得られる  –  Hiveの出力データ構造と解析ツールの入力形式の親和性  –  入力データ抽出→解析→入力データ修正→…  のイテレーションが容易  

 

Page 10: Hadoop and the Data Scientist

1.  Monitoring  

hRp://www.metricinsights.com/

hRp://www.chartbeat.com/

hRp://www.geckoboard.com/  

hRps://www.leUronic.com/

Page 11: Hadoop and the Data Scientist

Monitoring  

Goal  –  毎日更新されるデータ  (KPI)  を素早く参照する  

–  解析者に関わらず全てのユーザーが参照する  

–  異常値やイベントなどの効果を素早く把握する  

–  Smart  Phone  や  Tablet  からも参照する  

Demand  –  異常検出機能,およびアラート機能を備えていること  

–  チャートへのアノテーション機能を備えていること  

–  チャートの一覧性・わかりやすさを重視していること  

–  (任意の時間インターバルでの)データ自動更新機能を持っていること  

–  素早く編集可能な互いに独立したパネル(ウィジェット)を持っていること  

–  様々なデータベース・ファイル形式と接続できるコネクタを備えていること  

Page 12: Hadoop and the Data Scientist

Monitoring  

Intermediate  Data  Format  

 Very  Simple  Data  Format:  Tuple  or  Triplet  

–  Tuple:      {  dateYme,  numeric  value  },  

–  Triplet:  {  dateYme,  segment  name,  numeric  value  }  

Hive  Query  (Ex.  Hourly  Sales  Segmented  by  Item  )  

SELECT  date,  segment,  COUNT(disYnct  userid)  

FROM  some_metric  

WHERE  1345262400  <=  Ymestamp  

AND    Ymestamp  <  1345266000  

GROUP  BY  segment  

Date Segment Value

2012-­‐08-­‐18  13:00 Item  A $10,000

2012-­‐08-­‐18  13:00 Item  B $8,000

2012-­‐08-­‐18  13:00 Item  C $6,000

… … …

2012-­‐08-­‐18  14:00 Item  A $7000

Database

Page 13: Hadoop and the Data Scientist

Monitoring  

Example:  Metricinsights  –  アラート・アノテーション機能  –  1つ1つのパネル(ウィジェット)にクエリを埋め込める  –  {  クエリ,  集計インターバル,  チャートタイプ,  データソース  }  指定のみで自動更新  –  ピボットテーブル,{  バブル,  ボックス,  ファンネル }  チャートにも対応  

 

Page 14: Hadoop and the Data Scientist

2.  Business  Intelligence  

hRp://www.tableausoUware.com/ hRp://www.indicee.com/  

hRp://www.gooddata.com/ hRp://www.birst.com/

Page 15: Hadoop and the Data Scientist

Business  Intelligence  

Goal  –  Overview  first,  zoom  and  filter,  then  details-­‐ondemand  

–  様々の切り口・セグメントの組合せでデータを閲覧する  

–  インタラクティブな操作でドリルダウンや軸の切り替えを行う  

–  様々なチャートとテーブルを組合せた情報表現を行う  

–  プレゼンに耐えうるクオリティの高いレポートを作成する  

 

 Demand  –  インタラクティブな操作が可能なこと  

–  豊富なチャートライブラリ,ダッシュボードエディタの実装していること  

–  適化された中間データ構造(Data  Cubeなど)を備えていること  

–  マウス操作によってデータの深堀りや切り口の切り替えが可能なこと  

–  JDBC  /  ODBC  コネクタを初めとした様々なデータソースとの接続口を持つこと  

Page 16: Hadoop and the Data Scientist

Intermediate  Data  Format  –  Data  Cube:

Business  Intelligence  

Ex.  Cube  for  (  Country,  Car  Name,  Year  )

Year    Level

Cell:  (  Prius,  Korea,  2005)    -­‐-­‐>  10,000

Products     Date  

Loca/on  

PRIUS  

MARK  X  

FAIRLADY  Z  

ROAD  STAR  

USA   Canada   Japan   Korea  

2005  

2006  

2007  

Car  Name  Level

Country  Level  

dimension,level-­‐of-­‐detail  を固定することで各dimension  を次元軸に,値を各セ

ルに取ったData  Cubeを作成できる

Page 17: Hadoop and the Data Scientist

Business  Intelligence  

Car  Hierarchy

Loca/on  Country  State  City  

Date  Year  Month  Day  

Products  Company  Car  Type  Car  Name  

Fact  table  Dimension  tables  

State  Month  Car  Name    

Profit  Sales  Payroll  Marke/ng  Inventory  Margin  ...  

Star  Schema  

Hierarchy (level-of-detail)

Page 18: Hadoop and the Data Scientist

Hive  Query  

Business  Intelligence  

Algebraic  Measure:  f(x+y)  =  f(x)  +  f(y)    (SUM,  STD_DEV)は, 下層のdimensionの組合せ集計のみ保持しておけば,上位

階層は和をとる事で導出可能。

Holis/c  Measure  (TOP-­‐K,  MODE,  MEDIAN)は,全ての層で

集計を行う必要がある。

GRUP  BY          country,  state,  city,        company,  car_type,  car_name

GROUP  BY          country,  state,  city,          company,  car_type,  car_name  UNION  ALL  GROUP  BY          country,  state,  city,          company,  car_type  …  UNION  ALL  GROUP  BY            country,  company  …  

Cube  Lacce  (Loca/on  +  Company)  

<*,  *>

<*,  company> <country,  *>

<*,  car  type> <country,  company> <state,  *>

<*,  car  name> <country,  car  type> <state,  company> <city,  *>

<country,  car  name> <state,  car  type> <city,  company>

<state,  car  name> <city,  car  type>

<city,  car  name> Most  Detailed

Least  Detailed

Page 19: Hadoop and the Data Scientist

Intermediate  Data  Format  –  For  Algebraic  Measures  

–  For  HolisYc  Measures  (tend  to  become  very  large  table)  

Business  Intelligence  

County State City Company Car  Type Car  Name Date Units Sales

USA California San  Jones TOYOTA Sedan   Corolla   2012-­‐08-­‐15 36 $3,000

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

Algebraic  Measures Most  Detailed  Dimensions

Most  D

etailed  Dimen

sion

s  <city

,  car  nam

e>

County State City Company Car  Type Car  Name Date Avg  of  Top  20

USA California San  Jones TOYOTA Sedan   Corolla   2012-­‐08-­‐15 $3,600

USA California ALL TOYOTA Sedan Alion 2012-­‐08-­‐15 $2,400

USA ALL ALL NISSAN SUV X-­‐TRAIL 2012-­‐08-­‐16 $1,000

ALL ALL ALL NISSAN Sport FAIRLADY  Z 2012-­‐08-­‐16 $640

USA California San  Jones TOYOTA Sedan   ALL   2012-­‐08-­‐15 $3,600

USA California San  Jones TOYOTA ALL ALL 2012-­‐08-­‐15 $1,100

USA California San  Jones ALL ALL ALL 2012-­‐08-­‐15 $2,300

… … … … … … … …

ALL ALL ALL ALL ALL ALL ALL $720

Holis/c  Measure

*  Date  Dimension  はツール側で自動的にlevelを考慮してくれる

<country,  car  name> <country,  *>

<*,  *>

<country,  car  type>

<*,  car  name>

<county,  car  name>

<state,  car  name>

<city,  car  name>

LocaYon  Hierarchy Products  Hierarchy Date  Hierarchy

Page 20: Hadoop and the Data Scientist

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)  

Page 21: Hadoop and the Data Scientist

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

Page 22: Hadoop and the Data Scientist

Business  Intelligence  

Example:  Tableau  –   大多数の企業の導入実績(Zynga,  Nokia,  eBay,  etc…)  –   Polaris:  Visual  AbstracYon  ⇔  Cube:  Data  AbstracYon  –   豊富なチャートライブラリとデータソースコネクタを持つ  

Page 23: Hadoop and the Data Scientist

Business  Intelligence  

Reference  –  2012  Gartner  Magic  Quadrant  Report  

–  The  Forrester  Wave(TM):  Advanced  Data  VisualizaYon  (ADV)  Plavorms,  Q3  2012  

 

Gartner  Magic  Quadrant  Report  

Page 24: Hadoop and the Data Scientist

Google’s  Solu/on  

Cube Tenjing

Dremel

PowerDrill

BI  Tools

Cube  OperaYon

Ad-­‐hoc  Queries

Page 25: Hadoop and the Data Scientist

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の作成が容易に  

Page 26: Hadoop and the Data Scientist

Dremel:  Interac/ve  Analysis  of  Large-­‐Scale  Datasets  

Solu/on  for  (4):  Dremel  –  Dremel:  InteracYve  Analysis  of  Web-­‐Scale  Datasets  –  Scalable,  InteracYve  Ad-­‐hoc  Query  System  –  Nested  Columnar  Storage  

•  各レコードをColumnar  Formatへ変換  •  構造情報を保持するための2つの値:  (1)  RepeYYon  Levels  (2)  DefiniYon  Levels  

–  SQL-­‐like  query  language  •  Nested  Data  Support  •  MulY-­‐level  ExecuYon  Trees  •  Read-­‐only  Ad-­‐hoc  Query  

message Document { required int64 DocId; optional group Links { repeated int64 Backward; repeated int64 Forward; } repeated group Name { repeated group Language { required string Code; optional string Country; } optional string Url; } }

DocId: 10 Links Forward: 20 Forward: 40 Forward: 60 Name Language Code: 'en-us' Country: 'us' Language Code: 'en' Url: 'http://A' Name Url: 'http://B' Name Language Code: 'en-gb' Country: 'gb'

DocId: 20 Links Backward: 10 Backward: 30 Forward: 80 Name Url: 'http://C'

Big  Query  –  Hosted  Dremel  (Dremel  as  a  Service)  –  WHERE,  AggregaYon  FuncYons,  サブクエリ、中間テーブル作成,  部分

一致検索、部分的なJOIN(相手テーブルが数MB以下のもの)  –  Google  Cloud  Storage  へのデータインポート  

•  Nested  Data  Format非対応  •  現状はCSVファイルのみ,スキーマ定義必須  

Page 27: Hadoop and the Data Scientist

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単位で圧縮してメモリーキャッシュして再利用  

Page 28: Hadoop and the Data Scientist

3.  Data  Analysis  

hRp://www-­‐06.ibm.com/soUware/jp/analyYcs/spss/products/staYsYcs/  

hRp://numpy.scipy.org/

hRp://www.r-­‐project.org/

hRp://www.gigawiz.com/

Page 29: Hadoop and the Data Scientist

Data  Analysis  

Goal  –  データを「見る」から「分析する」へ  

–  CubeのDimensionのような粒度ではなく,UserIDやItemIDといった細かい粒度  

–  様々な統計手法を用いて(実際に現場実行可能な)パターンやモデルを見出す  

–  解析用データ「抽出」はHadoop(サーバー側),「分析」はR/Excel/SPSS(クライ

アント)側で行う  

Demand  –  シングルマシンで分析できる程度のデータ量に抑えること  

(*時系列解析以外の分析は特定調査期間でのデータ取得になるのでデータ量

は全ユーザー数,全アイテム数に等しい→  高々数百万レコード程度)  

–  Hadoop側に任せられる部分(抽出・データ前処理)とクライアント解析ツールで

しかできない部分をうまく切り分けられること  

Page 30: Hadoop and the Data Scientist

Data  Analysis  

Example:  Survival  Analysis  For  Web  Service  –  ユーザーの「退会」というイベントが発生するまでの時間(生存時間)に関する分析  

–  この分析によって以下のような事がわかる:  •  「入会からnヶ月後にはx%のユーザーが辞めてしまう」  

•  「m1〜m2日目の間がユーザーの退会リスクが高い」  

•  「セグメントAはBに比べて生存時間が長い(優良顧客である)」  

–  特定の調査期間でイベントを観測できるユーザーと,観測できない(調

査以降も生存している:右側打ち切り)ユーザーの2種類のパターンが

存在。双方を考慮して,推定を行う必要がある  

Page 31: Hadoop and the Data Scientist

Data  Analysis  

Intermediate  Data  Format  

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  

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

Page 32: Hadoop and the Data Scientist

R  Smaple  Code:    

>  library(survival)  

>  surv  <-­‐  read.csv("./survival.csv",  header=T)  

>  my.surv  <-­‐Surv(surv$Yme_to_event,surv$flag_event_occurred)  

>  my.fit  <-­‐  survfit(my.surv  ~  1)  

>  summary(my.fit)  

Call:  survfit(formula  =  my.surv  ~  1)  

 

 Yme  n.risk  n.event  survival    std.err  lower  95%  CI  upper  95%  CI  

       1    66028      32032      0.5149  0.001945              0.5111              0.5187  

       2    33996        3503      0.4618  0.001940              0.4580              0.4656  

       3    30444        1857      0.4336  0.001929              0.4299              0.4374  

       4    28540        1258      0.4145  0.001918              0.4108              0.4183  

…  

>  plot(my.fit,  main="Kaplan-­‐Meier  esYmate  with  95%  confidence  bounds",  xlab="Yme",  ylab="survival  funcYon”)  

>  H.hat  <-­‐  -­‐log(my.fit$surv);  H.hat  <-­‐  c(H.hat,  H.hat[length(H.hat)])  

>  h.sort.of  <-­‐  my.fit$n.event  /  my.fit$n.risk  

>  H.Ylde  <-­‐  vector()  for(i  in  1:length(h.sort.of))  H.Ylde[i]  <-­‐  sum(h.sort.of[1:i])  

>  H.Ylde  <-­‐  c(H.Ylde,  H.Ylde[length(H.Ylde)])  

>  plot(c(my.fit$Yme,  250),  H.hat,  xlab='Yme',  ylab='cumulaYve  hazard’,main='comparingcumulaYve  hazards',  ylim=range(c(H.hat,  H.Ylde)),  type='s’)  

>  points(c(my.fit$Yme,  250),  H.Ylde,  lty=2,  type='s’)  

>  legend(locator(1),  c("H.hat","H.Ylde"),  lty=1:2)  

Data  Analysis  

Page 33: Hadoop and the Data Scientist

0 50 100 150 200 250

0.0

0.2

0.4

0.6

0.8

1.0

Kaplan-Meier estimate with 95% confidence bounds

time

surv

ival

func

tion

0 50 100 150 200 250

0.5

1.0

1.5

2.0

2.5

3.0

3.5

comparing cumulative hazards

time

cum

ulat

ive

haza

rd

H.hatH.tilde

←図1.  生存関数曲線    •  期間0(登録日)で100%だった

ユーザーが期間x後には何%生き残っているかを示す  

•  この例では初めの1日目で50%以上が退会してしまっていることがわかる

図2.  累積ハザード曲線  →    •  どの期間において「退会する」

リスクが大きいか(傾きが急な部分)を示す累積曲線  

•  この例では初日を急勾配として,初めの10日間はリスクが大きい

Page 34: Hadoop and the Data Scientist

まとめ  –  データを「見る」という目的にも Monitoring  Tool  と BI  Tool  が存在し,その特徴

は異なる(共通する部分も多い)  

–  Hadoop側のBatch処理とBI  Tool側のAd-­‐hocな要請とのギャップをどう埋めるか  

–  Ad-­‐hoc  Queryに関する今後の技術動向に注目  

–  データを「見る」環境の構築構築は統計の専門家でなくても可能(「Data-­‐Driven」な意思決定文化の構築が も重要)  

–  分析を行う際も「分析用データ抽出フェーズ(大規模集計)」と「分析フェーズ」

をうまく切り分ける  

 

Offices:  -­‐  HQ:  Los  Altos,  CA  -­‐  Development  Center:  Tokyo,  Japan  

Contact:  -­‐  taka@treasure-­‐data.com