このガイドは、 Amazon Redshift のためのニフティクラウド SDK for PHP クライアントにフォーカスしています。 このガイドでは、 ニフティクラウド SDK for PHPをダウンロードしインストールが既に終わっていると想定しています。 はじめる場合のより詳細な情報は インストール を参照して下さい。
利用可能なすべてのメソッドおよび入出力の記述についてのより詳細な情報を得るためには ニフティクラウド API リファレンス または Amazon Redshift クライアント API リファレンス を参照して下さい。
まず以下のような方法のいずれかで、クライアントオブジェクトを作成しなければなりません。
Aws\Redshift\RedshiftClient::factory() メソッドを利用することで、最も簡単に立ち上げ速やかに動かすことができます。その際にクレデンシャル (key and secret) を提供して下さい。
region パラメータも必須で、以下の値のいずれかをセットしなければなりません。: us-east-1, ap-northeast-1, ap-southeast-1, ap-southeast-2, us-west-2, eu-west-1
use Aws\Redshift\RedshiftClient;
$client = RedshiftClient::factory(array(
'key' => '<aws access key>',
'secret' => '<aws secret key>',
'region' => '<region name>'
));
先の例のようにしてアクセスキーをセットすることができます。また、もし、 AWS Identity and Access Management (IAM) roles for EC2 instances を利用しているか、環境変数 AWS_ACCESS_KEY_ID と AWS_SECRET_ACCESS_KEY からクレデンシャル情報を利用できるのであれば、これらの設定を省略することも可能です。
より確実に Amazon Redshift に接続する方法はサービスロケータを通す方法です。 設定ファイルの中でクレデンシャルと設定値を指定することができます。 これらの設定はすべてのクライアントで共有されるので一度だけ設定を行えばよくなります。
use Aws\Common\Aws;
// Create a service builder using a configuration file
$aws = Aws::factory('/path/to/my_config.json');
// Get the client from the builder by namespace
$client = $aws->get('Redshift');
The primary resource in Amazon Redshift is the cluster. To create a cluster you will use the CreateCluster
operation. There are several parameters you can send when creating a cluster, so please refer to the API docs to
determine which parameters to use. The following is basic example.
$client->createCluster(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
'ClusterType' => 'multi-node',
'MasterUsername' => 'yourusername',
'MasterUserPassword' => 'Y0urP@$$w0rd',
'NodeType' => 'dw.hs1.xlarge',
'NumberOfNodes' => 2,
));
After the CreateCluster operation is complete, the record for your cluster will exist, but it will still take some
time before the cluster is actually available for use. You can describe your cluster to check it's status.
$result = $client->describeClusters(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
));
$clusters = $result->get('Clusters');
$status = $clusters['ClusterStatus'];
If you would like your code to wait until the cluster is available, you can use the the ClusterAvailable waiter.
$client->waitUntilClusterAvailable(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
));
Warning
It can take over 20 minutes for a cluster to become available.
You can also take snapshots of your cluster with the CreateClusterSnapshot operation. Snapshots can take a little
time before they become available as well, so there is a corresponding SnapshotAvailable waiter.
$client->createClusterSnapshot(array(
'ClusterIdentifier' => 'your-unique-cluster-id',
'SnapshotIdentifier' => 'your-unique-snapshot-id',
));
$client->waitUntilSnapshotAvailable(array(
'SnapshotIdentifier' => 'your-unique-snapshot-id',
));
Amazon Redshift records events that take place with your clusters and account. These events are available for up to 14
days and can be retrieved via the DescribeEvents operation. Only 100 events can be returned at a time, so using the
SDK's iterators feature allows you to easily fetch and iterate over all the events in your query without having to
manually send repeated requests. The StartTime and EndTime parameters can take any PHP date string or DateTime
object.
$events = $client->getIterator('DescribeEvents', array(
'StartTime' => strtotime('-3 days'),
'EndTime' => strtotime('now'),
));
foreach ($events as $event) {
echo "{$event['Date']}: {$event['Message']}\n";
}