More Related Content
Similar to 20140903groonga発表資料 (20)
More from Hironobu Saitoh (14)
20140903groonga発表資料
- 5. Twitter Steraming API
• HTTPで接続すると永遠にツイートデータが流れ
てくる
• 流れてくるのは全ツイートの1%程度のサンプル
• 接続するエンドポイントによってとれる情報が違
う
• 今回は日本(lang=ja)を指定して取得してみた。
- 8. • github.com/gc37/20140904-groonga
使用例
<?php
require_once 'groonga.php';
$groonga = new Groonga('localhost', 10041);
$cond = [
'table' => 'TableName',
'match_columns' => 'test_col',
'query' => 'hoge',
'limit' => 10,
'offset' => 0
];
$r = $groonga->execute('select', $cond);
// ヒット数
var_dump($r->count);
// 列情報
var_dumP($r->headers);
// 実行結果
var_dumP($r->results);
example.php
- 9. • インスタンスを作る
• 実行するコマンドのパラメータを配列にする
• コマンド名を渡してexecute()メソッドを叩く
• いじょう
- 10. 実行結果
int(161) <=== ヒット数
array(10) { <=== 検索結果
[0]=>
array(7) {
["_id"]=>
int(90032)
["created_at"]=>
float(1408890604)
["date"]=>
string(10) "2014-08-24"
["name"]=>
string(11) "hiroshi5s38"
["screen_name"]=>
string(36) "レンタルサーバーの選び方"
["source"]=>
string(11) "sakura_blog"
["text"]=>
string(106) "『サーバーの悩み』レンタルサーバー比較- サービスの比…|http://t.co/j1AtQfUXOL"
}
• ヒット数、列情報、実行結果がオブジェクトで返る
• 実行結果が連想配列になる
- 11. 実装
• github.com/gc37/20140904-groonga
/**
* Groongaサーバにリクエストを送信して、結果をJSONで返す
*
* @param string $cmd コマンド名
* @param array $params コマンドに渡すパラメータ配列
* @return array
*/
private function sendRequest($cmd, $params = [])
{
$url = sprintf('http://%s:%d/d/%s', $this->host, $this->port, $cmd);
$url .= '?' . http_build_query($params);
// リクエストを送信
$curl = $this->initializeCurl();
curl_setopt($curl, CURLOPT_URL, $url);
$body = curl_exec($curl);
if( ! $body) {
throw new RuntimeException('HTTP Request fail.');
}
// レスポンスをデコードする
$json = json_decode($body, true);
if( ! $json) {
throw new RuntimeException('Incorrect datatype for JSON.');
}
// この要素が0未満の場合はコマンド実行失敗で、[0][3]にメッセージが入るっぽい。
if($json[0][0] < 0) {
throw new RuntimeException($json[0][3]);
}
return $json;
}
- 13. データ取得スクリプト概要
• twapi_streaming.php
• Phirehoseライブラリを使わせていただいた
(https://github.com/fennb/phirehose)
• StreamingAPIからのJSONデータをパースして
Groonga用のJSONオブジェクトを作る
• 自作GroongaクラスとPhirehoseクラスの合わせ技
- 14. Phirehoseライブラリの使い方
<?php
require_once 'phirehose/lib/Phirehose.php';
require_once 'phirehose/lib/OauthPhirehose.php';
// OAuth
$access_token = '************************************************';
$access_token_secret = '******************************************';
class TestStream extends OauthPhirehose
{
public function enqueueStatus($status)
{
// ここに実装を書くだけ
// $status が1ツイートのJSONデータになっている
//
// また今回の場合、自動的に以下のエンドポイントが選択される
// https://stream.twitter.com/1.1/statuses/filter.json
}
}
$s = new TestStream($access_token, $access_token_secret);
$s->setLang('ja');
$s->consume();
- 15. Groongaへの保存
<?php
• twapi_streaming.php
// Groongaにデータを保存する
// $dataはツイートオブジェクト
$tweet = [
'_key' => $data->id_str,
'text' => $data->text,
'source' => $data->source,
'name' => $data->user->name,
'screen_name' => $data->user->screen_name,
'created_at' => $time
];
if($data->geo != null) {
$geo = $data->geo->coordinates[0] . 'x' . $data->geo->coordinates[1];
$tweet['geo'] = $geo;
}
$data = [
'table' => 'TwSource',
'values' =>json_encode([ $tweet ])
];
$r = $this->groonga->execute('load', $data);
- 18. <?php
$g = new Groonga('localhost', 10041);
// 既存テーブル削除
try {
$params = [
'name' => 'TwIndex'
];
$g->execute('table_remove', $params);
} catch(Exception $e) {}
// インデックステーブルを作成
$params = [
'name' => 'TwIndex',
'flags' => 'TABLE_PAT_KEY|KEY_NORMALIZE',
'key_type' => 'ShortText',
'default_tokenizer' => 'TokenMecab',
];
$g->execute('table_create', $params);
// インデックスカラムを作成
$params = [
'table' => 'TwIndex',
'name' => 'text',
'flags' => 'COLUMN_INDEX|WITH_POSITION',
'type' => 'TwSource',
'source' => 'text'
];
$g->execute('column_create', $params);
- 20. データ分析スクリプト概要
• select1.php, select2.php
• 簡単な分析をする
• 対象は8/24〜9/2までのツイートデータ4,380,001
件
• (ただし8/29、8/29、8/30の一部は事故で取得失敗・・・)
• キーワードの出現頻度を見てみましょう
- 22. [hiro@MBP]# ./select_burst.php PSN障害
2014-08-24: 15
2014-08-25: 16
2014-08-26: 12
2014-08-27: 2
2014-08-28: 0
2014-08-29: 0
2014-08-30: 0
2014-08-31: 0
2014-09-01: 0
[hiro@MBP]# ./select_burst.php ランドクルーザー
2014-08-24: 0
2014-08-25: 84
2014-08-26: 20
2014-08-27: 5
2014-08-28: 4
2014-08-29: 0
2014-08-30: 0
2014-08-31: 1
2014-09-01: 5
[hiro@MBP]# ./select_burst.php ヨンア
2014-08-24: 0
2014-08-25: 2
2014-08-26: 209
2014-08-27: 29
2014-08-28: 14
2014-08-29: 0
2014-08-30: 0
2014-08-31: 0
2014-09-01: 2
[hiro@MBP]# ./select_burst.php 芝幸太郎
2014-08-24: 0
2014-08-25: 0
2014-08-26: 135
2014-08-27: 10
2014-08-28: 1
2014-08-29: 0
2014-08-30: 0
2014-08-31: 0
2014-09-01: 0
25日
26日
27日
- 23. 時事ワード
• デング熱
• 急性の熱性感染症。戦後初の国内感染を確認
• ↑このニュースが出たのが27日
[hiro@MBP]# ./select_burst.php デング熱
2014-08-24: 0
2014-08-25: 0
2014-08-26: 0
2014-08-27: 332
2014-08-28: 756
2014-08-29: 0
2014-08-30: 0
2014-08-31: 38
2014-09-01: 837
(29日、30日、31日はデータ取得失敗)