How to setup amazon timestream in php?

1.1k views Asked by At

I have found the documentation for it here. I have PHP SDK installed. Now when I go through the documents there is not so much in detail about the PHP one. I have the following questions:

  1. Here how can I specify the $client
$result = $client->createDatabase([
    'DatabaseName' => '<string>', // REQUIRED
    'KmsKeyId' => '<string>',
    'Tags' => [
        [
            'Key' => '<string>', // REQUIRED
            'Value' => '<string>', // REQUIRED
        ],
        // ...
    ],
]);
  1. Is there any good documents or videos regarding the timestream in PHP from where I can get some help.
3

There are 3 answers

0
psykx On

There are two client classes. One for writing and one for reading.

TimestreamWriteClient

https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.TimestreamWrite.TimestreamWriteClient.html

and

TimestreamQueryClient

https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.TimestreamQuery.TimestreamQueryClient.html

You can use the function createTimestreamQuery and createTimestreamWrite on the $sdk object to instantiate those classes.

0
Guvener Gokce On

A sample Timestream client and query below.

// Create client
$client = new \Aws\TimestreamQuery\TimestreamQueryClient([    
    'version' => 'latest',
    'region' => AWS_REGION,  /* eg: eu-west-1 */
    'endpoint' => AWS_TIMESTREAM_ENDPOINT, /* eg: https://query-cell3.timestream.eu-west-1.amazonaws.com */
    'credentials' => new \Aws\Credentials\Credentials(AWS_KEY, AWS_SECRET)
]);

// Perform a basic query with the client
$client->query([
    'QueryString' => 'select * from "db_timestream"."tbl_usage_logs"', 
    'ValidateOnly' => true,
]);

If you receive endpoint warning, such as "The endpoint required for this service is currently unable to be retrieved"

You can find endpoint using AWS CLI command,

aws timestream-query describe-endpoints --region eu-west-1

Sample response:

{
    "Endpoints": [
        {
            "Address": "query-cell3.timestream.eu-west-1.amazonaws.com",
            "CachePeriodInMinutes": 1440
        }
    ]
}

One can create TimestreamWriteClient and write records in a similar way.

0
Alan Cleaver On

The documentation seems sparse and a bit misleading, to me anyhow. This is how I got it going for a write client (assuming SDK is installed).

//Create the client
$client = new \Aws\TimestreamWrite\TimestreamWriteClient([    
    'version' => 'latest',
    'region' => 'eu-west-1',
    'credentials' => new \Aws\Credentials\Credentials('***KEY***', '***SECRET***')
    ]);

Note that the 'endpoint' is not specified, as I've seen in some examples. There seems to be some misleading documentation of what the endpoint should be for any given region. The SDK does some magic and creates a suitable endpoint; providing a specific endpoint didn't work for me.

$result = $client->writeRecords(
    [
        'DatabaseName' => 'testDB',
        'TableName' => 'history',
        'Records' =>
            [
                [
                    'Dimensions' => [
                        [
                            'DimensionValueType' => 'VARCHAR',
                            'Name' => 'Server', 
                            'Value' => 'VM01', 
                         ],    
                     ],
                     'MeasureName' => 'CPU_utilization',
                     'MeasureValue' => '1.21',
                     'MeasureValueType' => 'DOUBLE',
                     'Time' => strval(time()),
                     'TimeUnit'  =>  'SECONDS',    
                 ]
             ]
         ]
     );

This seems to be the minimum set of things needed to write a record to Timestream successfully. The code above writes one record, with one dimension, in this case, a 'Name' of a 'Server', recording its CPU utilization at time().

Note:

  • Time is required, although the documentation suggested it is optional.
  • Time has to be a String.