Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masayuki5160/a6bd8c46a286203e42e7 to your computer and use it in GitHub Desktop.
Save masayuki5160/a6bd8c46a286203e42e7 to your computer and use it in GitHub Desktop.
AWS Kinesisを使うまで
{
"require":{
"aws/aws-sdk-php":"2.*"
}
}
<?php
require './vendor/autoload.php';
use Aws\Kinesis\KinesisClient;
use Aws\Common\Enum\Region;
$kinesis = KinesisClient::factory(array(
'key' => 'IAMで取得したキー名',
'secret' => 'IAMで取得したシークレットキー名',
'region' => Region::TOKYO // streamを作成したリージョン
));
$shard_iterator_info = $kinesis->getShardIterator(array(
'StreamName' => 'masayuki',
'ShardId' => 'shardId-000000000000',
'ShardIteratorType' => 'TRIM_HORIZON',
));
$shard_iterator = null;
while (true) {
if (is_null($shard_iterator)) {
$shard_iterator = $shard_iterator_info['ShardIterator'];
} else {
$shard_iterator = $records_info['NextShardIterator'];
}
$records_info = $kinesis->getRecords(array(
'ShardIterator' => $shard_iterator,
'Limit' => 100
));
$records = $records_info['Records'];
if (!empty($records)) {
// Recordsは必ずしも取れるわけではない
var_dump($records);
}
sleep(1);
}
AWS SDKインストール(EC2上で実施)
[参考]AWS SDK編~for PHP~
http://recipe.kc-cloud.jp/archives/6045
# sudo yum update
# sudo yum install php
# curl -sS https://getcomposer.org/installer | php
# vim composer.json
# php composer.phar install
<?php
require './vendor/autoload.php';
use Aws\Kinesis\KinesisClient;
use Aws\Common\Enum\Region;
$kinesis = KinesisClient::factory(array(
'key' => 'IAMで取得したキー名',
'secret' => 'IAMで取得したシークレットキー名',
'region' => Region::TOKYO // streamを作成したリージョン
));
while (true) {
$sample_data = date('YmdHis');
//=============================//
// Kinesiへのデータ送信処理
//=============================//
$result = $kinesis->putRecord(array(
'StreamName' => 'masayuki',// stream名を設定
'Data' => $sample_data,
'PartitionKey' => 'kinesis-sample'
));
echo "put data:$sample_data" . PHP_EOL;
sleep(1);
}
@masayuki5160
Copy link
Author

[参考サイト]
AWS SDK編~for PHP~
http://recipe.kc-cloud.jp/archives/6045

awsのs3を操作する為のaccess keyとsecret keyを取得する(IAM)
http://joppot.info/2014/06/14/1621

AWS SDK for PHP 2を利用してAmazon Kinesisを操作してみた
http://f-retu.hatenablog.com/entry/2014/02/23/231434

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment