

要获得与亚马逊 Timestream 类似的功能 LiveAnalytics，可以考虑适用于 InfluxDB 的亚马逊 Timestream。适用于 InfluxDB 的 Amazon Timestream 提供简化的数据摄取和个位数毫秒级的查询响应时间，以实现实时分析。点击[此处](https://docs.aws.amazon.com//timestream/latest/developerguide/timestream-for-influxdb.html)了解更多信息。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 访问 Amazon Timestream 以使用 LiveAnalytics AWS CLI
<a name="Tools.CLI"></a>

 您可以使用 AWS Command Line Interface (AWS CLI) 从命令行控制多项 AWS 服务，并通过脚本自动执行这些服务。您可以将 AWS CLI 用于即席操作。您还可以使用它在实用程序脚本中嵌入用于 LiveAnalytics 操作的 Amazon Timestream。

 必须先设置编程访问权限，然后才能将 Timestream AWS CLI 与 Timestream 配合使用。 LiveAnalytics有关更多信息，请参阅 [授予编程式访问权限](accessing.md#programmatic-access)。

有关中用于 LiveAnalytics 查询 Timestream API 的所有可用命令的完整列表 AWS CLI，请参阅[AWS CLI 命令参考](https://docs.aws.amazon.com/cli/latest/reference/timestream-query/index.html)。

有关中可用于 Timestream for W LiveAnalytics rite API 的所有命令的完整列表 AWS CLI，请参阅[AWS CLI 命令参考](https://docs.aws.amazon.com/cli/latest/reference/timestream-write/index.html)。

**Topics**
+ [正在下载和配置 AWS CLI](#Tools.CLI.DownloadingAndRunning)
+ [使用 with Tim AWS CLI estream LiveAnalytics](#Tools.CLI.UsingWithQLDB)

## 正在下载和配置 AWS CLI
<a name="Tools.CLI.DownloadingAndRunning"></a>

它们可以在 Windows、macOS 或 Linux 上 AWS CLI 运行。要下载、安装和配置，请按照以下步骤：

1.  AWS CLI 在 [http://aws.amazon.com/cli](https://aws.amazon.com/cli) 下载。

1. 按照《*AWS Command Line Interface 用户指南*[》中有关安装 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/installing.html) 和[配置 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) 的说明进行操作。

## 使用 with Tim AWS CLI estream LiveAnalytics
<a name="Tools.CLI.UsingWithQLDB"></a>

命令行格式由用于 LiveAnalytics 操作名称的 Amazon Timestream 和该操作的参数组成。除了 AWS CLI JSON 之外，还支持参数值的简写语法。

 `help`用于在 Timestream 中列出所有可用的命令。 LiveAnalytics例如：

```
aws timestream-write help
```

```
aws timestream-query help
```

 您还可以使用 `help` 来描述特定命令并了解有关其用法的详细信息：

```
aws timestream-write create-database help
```

 例如，创建数据库：

```
aws timestream-write create-database --database-name myFirstDatabase
```

 创建启用磁性存储写入的表：

```
aws timestream-write create-table \
--database-name metricsdb \
--table-name metrics \
--magnetic-store-write-properties "{\"EnableMagneticStoreWrites\": true}"
```

使用单度量记录写入数据：

```
aws timestream-write write-records \
--database-name metricsdb \
--table-name metrics \
--common-attributes "{\"Dimensions\":[{\"Name\":\"asset_id\", \"Value\":\"100\"}], \"Time\":\"1631051324000\",\"TimeUnit\":\"MILLISECONDS\"}" \
--records "[{\"MeasureName\":\"temperature\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"30\"},{\"MeasureName\":\"windspeed\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"7\"},{\"MeasureName\":\"humidity\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"15\"},{\"MeasureName\":\"brightness\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"17\"}]"
```

使用多度量记录写入数据：

```
# wide model helper method to create Multi-measure records
function ingest_multi_measure_records {
  epoch=`date +%s`
  epoch+=$i

  # multi-measure records
  aws timestream-write write-records \
  --database-name $src_db_wide \
  --table-name $src_tbl_wide \
  --common-attributes "{\"Dimensions\":[{\"Name\":\"device_id\", \
              \"Value\":\"12345678\"},\
            {\"Name\":\"device_type\", \"Value\":\"iPhone\"}, \
            {\"Name\":\"os_version\", \"Value\":\"14.8\"}, \
            {\"Name\":\"region\", \"Value\":\"us-east-1\"} ], \
            \"Time\":\"$epoch\",\"TimeUnit\":\"MILLISECONDS\"}" \
--records "[{\"MeasureName\":\"video_metrics\", \"MeasureValueType\":\"MULTI\", \
  \"MeasureValues\": \
  [{\"Name\":\"video_startup_time\",\"Value\":\"0\",\"Type\":\"BIGINT\"}, \
  {\"Name\":\"rebuffering_ratio\",\"Value\":\"0.5\",\"Type\":\"DOUBLE\"}, \
  {\"Name\":\"video_playback_failures\",\"Value\":\"0\",\"Type\":\"BIGINT\"}, \
  {\"Name\":\"average_frame_rate\",\"Value\":\"0.5\",\"Type\":\"DOUBLE\"}]}]" \
--endpoint-url $ingest_endpoint \
  --region  $region
}

# create 5 records
for i in {100..105};
  do ingest_multi_measure_records $i;
done
```

如何查询表：

```
aws timestream-query query \
--query-string "SELECT time, device_id, device_type, os_version, 
region, video_startup_time, rebuffering_ratio, video_playback_failures, \
average_frame_rate \
FROM metricsdb.metrics \
where time >= ago (15m)"
```

创建计划查询：

```
aws timestream-query create-scheduled-query \
  --name scheduled_query_name \
  --query-string "select bin(time, 1m) as time, \
          avg(measure_value::double) as avg_cpu, min(measure_value::double) as min_cpu, region \
          from $src_db.$src_tbl where measure_name = 'cpu' \
          and time BETWEEN @scheduled_runtime - (interval '5' minute)  AND @scheduled_runtime \
          group by region, bin(time, 1m)" \
  --schedule-configuration "{\"ScheduleExpression\":\"$cron_exp\"}" \
  --notification-configuration "{\"SnsConfiguration\":{\"TopicArn\":\"$sns_topic_arn\"}}" \
  --scheduled-query-execution-role-arn "arn:aws:iam::452360119086:role/TimestreamSQExecutionRole" \
  --target-configuration "{\"TimestreamConfiguration\":{\
          \"DatabaseName\": \"$dest_db\",\
          \"TableName\": \"$dest_tbl\",\
          \"TimeColumn\":\"time\",\
          \"DimensionMappings\":[{\
            \"Name\": \"region\", \"DimensionValueType\": \"VARCHAR\"
          }],\
          \"MultiMeasureMappings\":{\
            \"TargetMultiMeasureName\": \"mma_name\",
            \"MultiMeasureAttributeMappings\":[{\
              \"SourceColumn\": \"avg_cpu\", \"MeasureValueType\": \"DOUBLE\", \"TargetMultiMeasureAttributeName\": \"target_avg_cpu\"
            },\
            { \
              \"SourceColumn\": \"min_cpu\", \"MeasureValueType\": \"DOUBLE\", \"TargetMultiMeasureAttributeName\": \"target_min_cpu\"
            }] \
          }\
          }}" \
  --error-report-configuration "{\"S3Configuration\": {\
        \"BucketName\": \"$s3_err_bucket\",\
        \"ObjectKeyPrefix\": \"scherrors\",\
        \"EncryptionOption\": \"SSE_S3\"\
        }\
      }"
```