SlideShare a Scribd company logo
PHP と MySQL で
    1-Click
  MapReduce
(@ニコニコ超会議)

  @yuya_takeyama
はじめに
1-Click
無理でした!!!
PHP と MySQL で
       1-Click
1カチャカチャカチャ...ッターン!
      MapReduce
    (@ニコニコ超会議)

     @yuya_takeyama
N番煎じでお送りします
•   MyMR on GitHub
    https://github.com/yuya-takeyama/mymr

•   PHP と MySQL でカジュアルに MapReduce する
    http://blog.yuyat.jp/archives/1706

•   もっとカジュアルに PHP と MySQL で MapReduce する
    http://blog.yuyat.jp/archives/1853

•   PHP と MySQL でカジュアルに MapReduce する
    (スライド・Long Version)
    http://www.slideshare.net/taketyan/php-mysql-mapreduce

•   PHP と MySQL でカジュアルに MapReduce する
    (スライド・Short Version)
    http://www.slideshare.net/taketyan/php-mysql-
    mapreduce-short-version
MapReduce とは
(word count による例)
MapReduce とは

データ処理のための
プログラミングモデル
入力
処理の流れ      ↓
         Map
           ↓
        Shuffle
           ↓
        Reduce
           ↓
          出力
Map


•入力データを受け取り
• 複数の Key/Value ペアを出力
•to be or not to be
•<"to", 1>
•<"be", 1>
•<"or", 1>
•<"not", 1>
•<"to", 1>
•<"be", 1>
Shuffle


•Map による Key/Value を
• Key ごとにまとめて出力
•<"to", 1>
•<"be", 1>
•<"or", 1>
•<"not", 1>
•<"to", 1>
•<"be", 1>
•<"be", [1, 1]>
•<"not", [1]>
•<"or", [1]>
•<"to", [1, 1]>
Reduce


•Shuffle による中間データを
• 集約して答えを出力
•<"be", [1, 1]>
•<"not", [1]>
•<"or", [1]>
•<"to", [1, 1]>
•<"be", 2>
•<"not", 1>
•<"or", 1>
•<"to", 2>
複数の関数の
入出力を経て
最終的な答えを出力
ところで
MySQL で
MapReduce
  したい!!!
それ
  MySQL で
  も1
     カチ
        ャカ
MapReduce  チャ
              ...っ
                   ター
                     ン!
                        で

    したい!!!
モチベーション
•
プログラミングモデルとしての
MapReduce を使いたい

•   GROUP BY では難しい集計

•MySQL を入出力にしたい
• LL でサクッとやりたい
モチベーション
•
プログラミングモデルとしての
MapReduce を使いたい

•   GROUP BY では難しい集計

•MySQL を入出力にしたい
            PHP である必要はあまり無い


• LL でサクッとやりたい
モチベーション
 •
もち プログラミングモデルとしての
  ろん
     1カ
  MapReduce を使いたい
        チャ
           カチ
              ャ...
 • GROUP BY では難しい集計ッタ
                      ーン
                          !で
 • MySQL を入出力にしたい
              PHP である必要はあまり無い


 • LL でサクッとやりたい
というわけで作りました
MyMR
   https://github.com/yuya-takeyama/mymr


•MySQL を入出力とする

• PHP で Map/Reduce を書く

• カチャカチャ...ッターン!で実行
実行までの流れ

•入出力に使う MySQL テーブルの用意
•PHP で Map 関数を書く
•PHP で Reduce 関数を書く
•mymr コマンドを実行する
実行までの流れ

•入出力に使う MySQL テーブルの用意
•PHP で Map 関数を書く
•PHP で Reduce 関数を書く
•mymr コマンドを実行する
           この辺がカチャカチャカチャ...
実行までの流れ

•入出力に使う MySQL テーブルの用意
•PHP で Map 関数を書く
•PHP で Reduce 関数を書く
•mymr コマンドを実行する
            この辺が...ッターン!
に よ る
 yM R
M
    文章中の
単語の数を数える例
 (word count)
use MyMRBuilder;
                      Map/Reduce の定義
$builder = new Builder;

$builder->setInputTable('root@localhost/db/texts');
$builder->setOutputTable('root@localhost/db/word_counts');

$builder->setMapper(function ($record, $emitter) {
    $words = preg_split('/s+/u', $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }
});

$builder->setReducer(function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
});

return $builder;
use MyMRBuilder;
                      Map/Reduce の定義
$builder = new Builder;

$builder->setInputTable('root@localhost/db/texts');
$builder->setOutputTable('root@localhost/db/word_counts');

$builder->setMapper(function ($record, $emitter) {
    $words = preg_split('/s+/u', $record['text']);
    foreach ($words as $word) {       入出力テーブルの指定
        $emitter->emit($word, 1);
    }
});

$builder->setReducer(function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
});

return $builder;
use MyMRBuilder;
                      Map/Reduce の定義
$builder = new Builder;

$builder->setInputTable('root@localhost/db/texts');
$builder->setOutputTable('root@localhost/db/word_counts');

$builder->setMapper(function ($record, $emitter) {
    $words = preg_split('/s+/u', $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }                                    この辺が Map
});

$builder->setReducer(function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
});

return $builder;
use MyMRBuilder;
                      Map/Reduce の定義
$builder = new Builder;

$builder->setInputTable('root@localhost/db/texts');
$builder->setOutputTable('root@localhost/db/word_counts');

$builder->setMapper(function ($record, $emitter) {
    $words = preg_split('/s+/u', $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }
});

$builder->setReducer(function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);      この辺が Reduce
});

return $builder;
入力


•to be or not to be
Map

function ($record, $emitter) {
    $words = preg_split('/s+/u',
                        $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }
}
Map       レコードを
                       連想配列として受け取る

function ($record, $emitter) {
    $words = preg_split('/s+/u',
                        $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }
}
Map

function ($record, $emitter) {
    $words = preg_split('/s+/u',
                        $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }
}                         text カラム内の
                       文字列をスペースで分割
Map

function ($record, $emitter) {
    $words = preg_split('/s+/u',
                        $record['text']);
    foreach ($words as $word) {
        $emitter->emit($word, 1);
    }
}
                    Key/Value のペアとして
                    中間テーブルに INSERT
+----+--------------------+

Map    | id | text               |
       +----+--------------------+
       | 1 | to be or not to be |
       +----+--------------------+
      ↓ レコードを連想配列として Map へ ↓
        +----+---------+-------+
        | id | key     | value |
        +----+---------+-------+
        | 1 | to       | 1     |
        | 2 | be       | 1     |
        | 3 | or       | 1     |
        | 4 | not      | 1     |
        | 5 | to       | 1     |
        | 6 | be       | 1     |
        +----+---------+-------+
+----+--------------------+

Map          | id | text               |
             +----+--------------------+
             | 1 | to be or not to be |
             +----+--------------------+
          ↓ レコードを連想配列として Map へ ↓
               +----+---------+-------+
               | id | key     | value |
               +----+---------+-------+
               | 1 | to       | 1     |
               | 2 | be       | 1     |
               | 3 | or       | 1     |
               | 4 | not
 value には JSON で入れるので         | 1     |
               | 5 | to
     構造化データも使用可能              | 1     |
               | 6 | be       | 1     |
               +----+---------+-------+
+----+---------+-------+
                            | id | key     | value |

Shuffle                      +----+---------+-------+
                            | 1 | to
                            | 2 | be
                                           | 1
                                           | 1
                                                   |
                                                   |
                            | 3 | or       | 1     |
                            | 4 | not      | 1     |
                            | 5 | to       | 1     |
                            | 6 | be       | 1     |
                            +----+---------+-------+

                          ↓ キーで GROUP BY して ↓
SELECT                    ↓ 値は GROUP_CONCAT ↓
  `key`,                      +---------+--------+
  GROUP_CONCAT(`value`)       | key     | values |
FROM                          +---------+--------+
  `中間テーブル`                    | be      | 1,1    |
                              | not     | 1      |
GROUP BY                      | or      | 1      |
  `key`                       | to      | 1,1    |
                              +---------+--------+
Reduce
function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
}
Reduce
                 Key      Value の配列

function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
}
Reduce
                         Value を全て足す
function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
}
Reduce
function ($key, $values) {
    $sum = 0;
    foreach ($values as $count) {
        $sum += $count;
    }
    return array('count' => $sum);
}
                     返り値の連想配列を
                    レコードとして INSERT
+---------+--------+
            | key     | values |
Reduce      +---------+--------+
            | be      | 1,1    |
            | not     | 1      |
            | or      | 1      |
            | to      | 1,1    |
            +---------+--------+
         ↓ キーと値の配列を Reduce へ ↓
           +----+---------+-------+
           | id | key     | count |
           +----+---------+-------+
           | 1 | be       |     2 |
           | 2 | not      |     1 |
           | 3 | or       |     1 |
           | 4 | to       |     2 |
           +----+---------+-------+
+---------+--------+
                 | key     | values |
Reduce           +---------+--------+
                 | be      | 1,1    |
                 | not     | 1      |
                 | or      | 1      |
 実際にはデリミタとして改行を使用| to      | 1,1    |
                 +---------+--------+
   改行区切りの JSON になる

             ↓ キーと値の配列を Reduce へ ↓
                +----+---------+-------+
                | id | key     | count |
                +----+---------+-------+
                | 1 | be       |     2 |
                | 2 | not      |     1 |
                | 3 | or       |     1 |
                | 4 | to       |     2 |
                +----+---------+-------+
今後の目標


•非同期 INSERT による並列化
• Hadoop へのシームレスな
移行方法の提供
今後の野望

•V8 エンジンとかで

•ストレージエンジン API を
• カジュアルに叩いて

•MapReduce したい
今後の野望
もち
 • ろん エンジンとかで
   V8 1
        カチ
          ャカ
 •          チャ
   ストレージエンジン API を
               ...ッ
                    ター
 • カジュアルに叩いて          ン!
                         で
 •MapReduce したい
ご清聴
 ありがとう
ございました

More Related Content

More from Yuya Takeyama

5分でわかる? 関数型 PHP の潮流
5分でわかる? 関数型 PHP の潮流5分でわかる? 関数型 PHP の潮流
5分でわかる? 関数型 PHP の潮流
Yuya Takeyama
 
Good Parts of PHP and the UNIX Philosophy
Good Parts of PHP and the UNIX PhilosophyGood Parts of PHP and the UNIX Philosophy
Good Parts of PHP and the UNIX Philosophy
Yuya Takeyama
 
Reactor Pattern and React
Reactor Pattern and ReactReactor Pattern and React
Reactor Pattern and ReactYuya Takeyama
 
PHPUnit でテスト駆動開発を始めよう
PHPUnit でテスト駆動開発を始めようPHPUnit でテスト駆動開発を始めよう
PHPUnit でテスト駆動開発を始めようYuya Takeyama
 
HashTable と HashDos
HashTable と HashDosHashTable と HashDos
HashTable と HashDosYuya Takeyama
 
Proposal for xSpep BDD Framework for PHP
Proposal for xSpep BDD Framework for PHPProposal for xSpep BDD Framework for PHP
Proposal for xSpep BDD Framework for PHP
Yuya Takeyama
 
Building Development Environment with php-build and phpenv
Building Development Environment with php-build and phpenvBuilding Development Environment with php-build and phpenv
Building Development Environment with php-build and phpenv
Yuya Takeyama
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
Yuya Takeyama
 
LIMIT 付きで UPDATE を行うと何故怒られるか
LIMIT 付きで UPDATE を行うと何故怒られるかLIMIT 付きで UPDATE を行うと何故怒られるか
LIMIT 付きで UPDATE を行うと何故怒られるか
Yuya Takeyama
 
GOOS #1
GOOS #1GOOS #1
GOOS #1
Yuya Takeyama
 
Ruby 同好会宣言
Ruby 同好会宣言Ruby 同好会宣言
Ruby 同好会宣言
Yuya Takeyama
 
第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)
第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)
第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)
Yuya Takeyama
 

More from Yuya Takeyama (13)

5分でわかる? 関数型 PHP の潮流
5分でわかる? 関数型 PHP の潮流5分でわかる? 関数型 PHP の潮流
5分でわかる? 関数型 PHP の潮流
 
Good Parts of PHP and the UNIX Philosophy
Good Parts of PHP and the UNIX PhilosophyGood Parts of PHP and the UNIX Philosophy
Good Parts of PHP and the UNIX Philosophy
 
Reactor Pattern and React
Reactor Pattern and ReactReactor Pattern and React
Reactor Pattern and React
 
PHPUnit でテスト駆動開発を始めよう
PHPUnit でテスト駆動開発を始めようPHPUnit でテスト駆動開発を始めよう
PHPUnit でテスト駆動開発を始めよう
 
HashTable と HashDos
HashTable と HashDosHashTable と HashDos
HashTable と HashDos
 
Proposal for xSpep BDD Framework for PHP
Proposal for xSpep BDD Framework for PHPProposal for xSpep BDD Framework for PHP
Proposal for xSpep BDD Framework for PHP
 
Building Development Environment with php-build and phpenv
Building Development Environment with php-build and phpenvBuilding Development Environment with php-build and phpenv
Building Development Environment with php-build and phpenv
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
 
Making DSL with []
Making DSL with []Making DSL with []
Making DSL with []
 
LIMIT 付きで UPDATE を行うと何故怒られるか
LIMIT 付きで UPDATE を行うと何故怒られるかLIMIT 付きで UPDATE を行うと何故怒られるか
LIMIT 付きで UPDATE を行うと何故怒られるか
 
GOOS #1
GOOS #1GOOS #1
GOOS #1
 
Ruby 同好会宣言
Ruby 同好会宣言Ruby 同好会宣言
Ruby 同好会宣言
 
第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)
第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)
第一回 社内勉強会 PHP Application Security Checklist に学ぶ PHP セキュリティ (Excerpt)
 

Recently uploaded

Generating Automatic Feedback on UI Mockups with Large Language Models
Generating Automatic Feedback on UI Mockups with Large Language ModelsGenerating Automatic Feedback on UI Mockups with Large Language Models
Generating Automatic Feedback on UI Mockups with Large Language Models
harmonylab
 
ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---
ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---
ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---
Matsushita Laboratory
 
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアルLoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
CRI Japan, Inc.
 
遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化
遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化
遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化
t m
 
JSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさ
JSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさJSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさ
JSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさ
0207sukipio
 
キンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援します
キンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援しますキンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援します
キンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援します
Takayuki Nakayama
 
論文紹介:Deep Learning-Based Human Pose Estimation: A Survey
論文紹介:Deep Learning-Based Human Pose Estimation: A Survey論文紹介:Deep Learning-Based Human Pose Estimation: A Survey
論文紹介:Deep Learning-Based Human Pose Estimation: A Survey
Toru Tamaki
 
論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...
論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...
論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...
Toru Tamaki
 
This is the company presentation material of RIZAP Technologies, Inc.
This is the company presentation material of RIZAP Technologies, Inc.This is the company presentation material of RIZAP Technologies, Inc.
This is the company presentation material of RIZAP Technologies, Inc.
chiefujita1
 

Recently uploaded (9)

Generating Automatic Feedback on UI Mockups with Large Language Models
Generating Automatic Feedback on UI Mockups with Large Language ModelsGenerating Automatic Feedback on UI Mockups with Large Language Models
Generating Automatic Feedback on UI Mockups with Large Language Models
 
ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---
ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---
ReonHata_便利の副作用に気づかせるための発想支援手法の評価---行為の増減の提示による気づきへの影響---
 
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアルLoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
 
遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化
遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化
遺伝的アルゴリズムと知識蒸留による大規模言語モデル(LLM)の学習とハイパーパラメータ最適化
 
JSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさ
JSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさJSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさ
JSAI_類似画像マッチングによる器への印象付与手法の妥当性検証_ver.3_高橋りさ
 
キンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援します
キンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援しますキンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援します
キンドリル ネットワークアセスメントサービスご紹介 今のネットワーク環境は大丈夫? 調査〜対策までご支援します
 
論文紹介:Deep Learning-Based Human Pose Estimation: A Survey
論文紹介:Deep Learning-Based Human Pose Estimation: A Survey論文紹介:Deep Learning-Based Human Pose Estimation: A Survey
論文紹介:Deep Learning-Based Human Pose Estimation: A Survey
 
論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...
論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...
論文紹介:When Visual Prompt Tuning Meets Source-Free Domain Adaptive Semantic Seg...
 
This is the company presentation material of RIZAP Technologies, Inc.
This is the company presentation material of RIZAP Technologies, Inc.This is the company presentation material of RIZAP Technologies, Inc.
This is the company presentation material of RIZAP Technologies, Inc.
 

PHP と MySQL で 1 カチャカチャカチャ...ッターン! MapReduce (@ニコニコ超会議)

  • 1. PHP と MySQL で 1-Click MapReduce (@ニコニコ超会議) @yuya_takeyama
  • 4. PHP と MySQL で 1-Click 1カチャカチャカチャ...ッターン! MapReduce (@ニコニコ超会議) @yuya_takeyama
  • 5. N番煎じでお送りします • MyMR on GitHub https://github.com/yuya-takeyama/mymr • PHP と MySQL でカジュアルに MapReduce する http://blog.yuyat.jp/archives/1706 • もっとカジュアルに PHP と MySQL で MapReduce する http://blog.yuyat.jp/archives/1853 • PHP と MySQL でカジュアルに MapReduce する (スライド・Long Version) http://www.slideshare.net/taketyan/php-mysql-mapreduce • PHP と MySQL でカジュアルに MapReduce する (スライド・Short Version) http://www.slideshare.net/taketyan/php-mysql- mapreduce-short-version
  • 8. 入力 処理の流れ ↓ Map ↓ Shuffle ↓ Reduce ↓ 出力
  • 10. •to be or not to be
  • 11. •<"to", 1> •<"be", 1> •<"or", 1> •<"not", 1> •<"to", 1> •<"be", 1>
  • 12. Shuffle •Map による Key/Value を • Key ごとにまとめて出力
  • 13. •<"to", 1> •<"be", 1> •<"or", 1> •<"not", 1> •<"to", 1> •<"be", 1>
  • 14. •<"be", [1, 1]> •<"not", [1]> •<"or", [1]> •<"to", [1, 1]>
  • 16. •<"be", [1, 1]> •<"not", [1]> •<"or", [1]> •<"to", [1, 1]>
  • 20. MySQL で MapReduce したい!!!
  • 21. それ MySQL で も1 カチ ャカ MapReduce チャ ...っ ター ン! で したい!!!
  • 22. モチベーション • プログラミングモデルとしての MapReduce を使いたい • GROUP BY では難しい集計 •MySQL を入出力にしたい • LL でサクッとやりたい
  • 23. モチベーション • プログラミングモデルとしての MapReduce を使いたい • GROUP BY では難しい集計 •MySQL を入出力にしたい PHP である必要はあまり無い • LL でサクッとやりたい
  • 24. モチベーション • もち プログラミングモデルとしての ろん 1カ MapReduce を使いたい チャ カチ ャ... • GROUP BY では難しい集計ッタ ーン !で • MySQL を入出力にしたい PHP である必要はあまり無い • LL でサクッとやりたい
  • 26. MyMR https://github.com/yuya-takeyama/mymr •MySQL を入出力とする • PHP で Map/Reduce を書く • カチャカチャ...ッターン!で実行
  • 27. 実行までの流れ •入出力に使う MySQL テーブルの用意 •PHP で Map 関数を書く •PHP で Reduce 関数を書く •mymr コマンドを実行する
  • 28. 実行までの流れ •入出力に使う MySQL テーブルの用意 •PHP で Map 関数を書く •PHP で Reduce 関数を書く •mymr コマンドを実行する この辺がカチャカチャカチャ...
  • 29. 実行までの流れ •入出力に使う MySQL テーブルの用意 •PHP で Map 関数を書く •PHP で Reduce 関数を書く •mymr コマンドを実行する この辺が...ッターン!
  • 30. に よ る yM R M 文章中の 単語の数を数える例 (word count)
  • 31. use MyMRBuilder; Map/Reduce の定義 $builder = new Builder; $builder->setInputTable('root@localhost/db/texts'); $builder->setOutputTable('root@localhost/db/word_counts'); $builder->setMapper(function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } }); $builder->setReducer(function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); }); return $builder;
  • 32. use MyMRBuilder; Map/Reduce の定義 $builder = new Builder; $builder->setInputTable('root@localhost/db/texts'); $builder->setOutputTable('root@localhost/db/word_counts'); $builder->setMapper(function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) { 入出力テーブルの指定         $emitter->emit($word, 1);     } }); $builder->setReducer(function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); }); return $builder;
  • 33. use MyMRBuilder; Map/Reduce の定義 $builder = new Builder; $builder->setInputTable('root@localhost/db/texts'); $builder->setOutputTable('root@localhost/db/word_counts'); $builder->setMapper(function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } この辺が Map }); $builder->setReducer(function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); }); return $builder;
  • 34. use MyMRBuilder; Map/Reduce の定義 $builder = new Builder; $builder->setInputTable('root@localhost/db/texts'); $builder->setOutputTable('root@localhost/db/word_counts'); $builder->setMapper(function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } }); $builder->setReducer(function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); この辺が Reduce }); return $builder;
  • 35. 入力 •to be or not to be
  • 36. Map function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } }
  • 37. Map レコードを 連想配列として受け取る function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } }
  • 38. Map function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } } text カラム内の 文字列をスペースで分割
  • 39. Map function ($record, $emitter) {     $words = preg_split('/s+/u', $record['text']);     foreach ($words as $word) {         $emitter->emit($word, 1);     } } Key/Value のペアとして 中間テーブルに INSERT
  • 40. +----+--------------------+ Map | id | text | +----+--------------------+ | 1 | to be or not to be | +----+--------------------+ ↓ レコードを連想配列として Map へ ↓ +----+---------+-------+ | id | key | value | +----+---------+-------+ | 1 | to | 1 | | 2 | be | 1 | | 3 | or | 1 | | 4 | not | 1 | | 5 | to | 1 | | 6 | be | 1 | +----+---------+-------+
  • 41. +----+--------------------+ Map | id | text | +----+--------------------+ | 1 | to be or not to be | +----+--------------------+ ↓ レコードを連想配列として Map へ ↓ +----+---------+-------+ | id | key | value | +----+---------+-------+ | 1 | to | 1 | | 2 | be | 1 | | 3 | or | 1 | | 4 | not value には JSON で入れるので | 1 | | 5 | to 構造化データも使用可能 | 1 | | 6 | be | 1 | +----+---------+-------+
  • 42. +----+---------+-------+ | id | key | value | Shuffle +----+---------+-------+ | 1 | to | 2 | be | 1 | 1 | | | 3 | or | 1 | | 4 | not | 1 | | 5 | to | 1 | | 6 | be | 1 | +----+---------+-------+ ↓ キーで GROUP BY して ↓ SELECT ↓ 値は GROUP_CONCAT ↓ `key`, +---------+--------+ GROUP_CONCAT(`value`) | key | values | FROM +---------+--------+ `中間テーブル` | be | 1,1 | | not | 1 | GROUP BY | or | 1 | `key` | to | 1,1 | +---------+--------+
  • 43. Reduce function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); }
  • 44. Reduce Key Value の配列 function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); }
  • 45. Reduce Value を全て足す function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); }
  • 46. Reduce function ($key, $values) {     $sum = 0;     foreach ($values as $count) {         $sum += $count;     }     return array('count' => $sum); } 返り値の連想配列を レコードとして INSERT
  • 47. +---------+--------+ | key | values | Reduce +---------+--------+ | be | 1,1 | | not | 1 | | or | 1 | | to | 1,1 | +---------+--------+ ↓ キーと値の配列を Reduce へ ↓ +----+---------+-------+ | id | key | count | +----+---------+-------+ | 1 | be | 2 | | 2 | not | 1 | | 3 | or | 1 | | 4 | to | 2 | +----+---------+-------+
  • 48. +---------+--------+ | key | values | Reduce +---------+--------+ | be | 1,1 | | not | 1 | | or | 1 | 実際にはデリミタとして改行を使用| to | 1,1 | +---------+--------+ 改行区切りの JSON になる ↓ キーと値の配列を Reduce へ ↓ +----+---------+-------+ | id | key | count | +----+---------+-------+ | 1 | be | 2 | | 2 | not | 1 | | 3 | or | 1 | | 4 | to | 2 | +----+---------+-------+
  • 49. 今後の目標 •非同期 INSERT による並列化 • Hadoop へのシームレスな 移行方法の提供
  • 50. 今後の野望 •V8 エンジンとかで •ストレージエンジン API を • カジュアルに叩いて •MapReduce したい
  • 51. 今後の野望 もち • ろん エンジンとかで V8 1 カチ ャカ • チャ ストレージエンジン API を ...ッ ター • カジュアルに叩いて ン! で •MapReduce したい