pcbodev/laravel-aws-timestream

通过API与AWS Timestream服务交互的库

1.0.1 2023-09-18 07:54 UTC

This package is not auto-updated.

Last update: 2024-09-30 12:52:16 UTC


README

PHPUnit Tests PHPStan

AWS Timestream是一个快速、可扩展和无服务器的时序数据库服务。本包是一个具有意见的查询timestream并将数据导入timestream的实现。

它提供了一个查询构建器类,其中包含常见的时序SQL函数。这受到了Laravel Eloquent ORM的启发。查看支持的查询函数NorbyBaru\AwsTimestream\Contract\QueryBuilderContract

它还提供了一个有效载荷构建器类,用于正确格式化您的数据以导入到timestream中。查看NorbyBaru\AwsTimestream\Contract\PayloadBuilderContract

信息

我们分支了norbybaru的存储库,因为他为timestream查询构建器建立了一个出色的基础,但我们需要一些额外的功能,因此在我们分支中实现了这些功能。

安装

composer require pcbodev/laravel-aws-timestream

配置

  • 发布配置
php artisan vendor:publish --provider="NorbyBaru\AwsTimestream\TimestreamServiceProvider" --tag="timestream-config"
  • 打开timestream.php配置文件,设置数据库名称和表
  • 使用以下环境变量设置AWS Timestream密钥和权限
AWS_TIMESTREAM_KEY=
AWS_TIMESTREAM_SECRET=
AWS_TIMESTREAM_PROFILE=

基本用法

查询Timestream

使用TimestreamBuilder::query()将提供所有可用函数的自动完成

  1. 使用TimestreamBuilder构建要传递给TimestreamReaderDto的查询,该对象可以由TimestreamService查询函数消费
<?php

use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\TimestreamBuilder;
use NorbyBaru\AwsTimestream\Dto\TimestreamReaderDto;

public function overview(TimestreamService $timestreamService)
{
    $queryBuilder = TimestreamBuilder::query()
        ->select('*')
        ->from("database-name", 'table-name')
        ->whereAgo('time', '24h', '>=')
        ->whereNotIn('measure_value::varchar', ['reviewer', 'open', 'closed'])
        ->orderBy('time', 'desc');
    
    TimestreamReaderDto::make($queryBuilder);

    // response from Aws timestream
    return $timestreamService->query($timestreamReader)
}
  1. 使用TimestreamReaderDto注入from查询,默认为database名称,并按需指定table名称。注意。在您的查询构建器上不需要添加->from()查询。
<?php

use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\TimestreamBuilder;
use NorbyBaru\AwsTimestream\Dto\TimestreamReaderDto;

public function overview(TimestreamService $timestreamService)
{
   $queryBuilder = TimestreamBuilder::query()
       ->select('*')
       ->whereAgo('time', '24h', '>=')
       ->whereNotIn('measure_value::varchar', ['reviewer', 'open', 'closed'])
       ->orderBy('time', 'desc');
   
   TimestreamReaderDto::make($queryBuilder, 'table-name');

   // response from Aws timestream
   return $timestreamService->query($timestreamReader)
}

Timestream导入

我们需要构建Timestream可以接受的用于导入的有效载荷。

  1. 使用TimestreamBuilder构建导入有效载荷
<?php

use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\Dto\TimestreamWriterDto;
use NorbyBaru\AwsTimestream\TimestreamBuilder;

public function ingest(TimestreamService $timestreamService)
{
    $metrics = [
        'measure_name' => 'cpu_usage',
        'measure_value' => 80,
        'time' => Carbon::now(),
        'dimensions' => [
            'mac_address' => 'randomstring',
            'ref' => 'refs',
        ],
    ];

    $payload = TimestreamBuilder::payload(
        $metrics['measure_name'],
        $metrics['measure_value'],
        $metrics['time'],
        'VARCHAR',
        $metrics['dimensions'],
    )->toArray();

    $timestreamWriter = TimestreamWriterDto::make($payload)->forTable('table-name');
    return $timestreamService->write($timestreamWriter);
}
  1. 使用通用属性批量导入数据以降低Timestream的导入成本
<?php

use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\Dto\TimestreamWriterDto;
use NorbyBaru\AwsTimestream\Support\TimestreamPayloadBuilder;

public function ingest(TimestreamService $timestreamService)
{
    $metrics = [
        [
            'measure_name' => 'cpu_usage',
            'measure_value' => 80,
            'time' => Carbon::now(),
            'dimensions' => [
                'ref' => 'ref_1',
            ],
        ],
        [
            'measure_name' => 'memory_usage',
            'measure_value' => 20,
            'time' => Carbon::now(),
            'dimensions' => [
                'ref' => 'ref_2',
            ],
        ]
    ];

    $commonAttributes['device_name'] = 'device_1';
    $commonAttributes['mac_address'] = 'randomstring';

    $payload = TimestreamBuilder::batchPayload($metrics);

    $common = TimestreamBuilder::commonAttributes($commonAttributes);

    $timestreamWriter = TimestreamWriterDto::make($payload, $common, 'table-name');
    return $timestreamService->write($timestreamWriter);
}

在线资源