通过 Java 和 Amazon CLI 使用 CreateDataset API - Amazon IoT Analytics
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon IoT Analytics 不再向新客户提供。的现有客户 Amazon IoT Analytics 可以继续照常使用该服务。了解更多

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

通过 Java 和 Amazon CLI 使用 CreateDataset API

创建数据集。数据集存储通过应用 queryAction(SQL 查询)或 containerAction(执行容器化应用程序)从数据存储中检索的数据。此操作创建数据集的骨架。该数据集可以通过调用 CreateDatasetContent 手动填充,也可以根据您指定的 trigger 自动填充。有关更多信息,请参阅 CreateDatasetCreateDatasetContent

示例 1 - 创建 SQL 数据集 (java)

CreateDatasetRequest request = new CreateDatasetRequest(); request.setDatasetName(dataSetName); DatasetAction action = new DatasetAction(); //Create Action action.setActionName("SQLAction1"); action.setQueryAction(new SqlQueryDatasetAction().withSqlQuery("select * from DataStoreName")); // Add Action to Actions List List<DatasetAction> actions = new ArrayList<DatasetAction>(); actions.add(action); //Create Trigger DatasetTrigger trigger = new DatasetTrigger(); trigger.setSchedule(new Schedule().withExpression("cron(0 12 * * ? *)")); //Add Trigger to Triggers List List<DatasetTrigger> triggers = new ArrayList<DatasetTrigger>(); triggers.add(trigger); // Add Triggers and Actions to CreateDatasetRequest object request.setActions(actions); request.setTriggers(triggers); // Add RetentionPeriod to CreateDatasetRequest object request.setRetentionPeriod(new RetentionPeriod().withNumberOfDays(10)); final CreateDatasetResult result = iot.createDataset(request);

成功时的输出:

{DatasetName: <datatsetName>, DatasetArn: <datatsetARN>, RetentionPeriod: {unlimited: true} or {numberOfDays: 10, unlimited: false}}

示例 2 - 创建具有增量时段的 SQL 数据集 (Java)

CreateDatasetRequest request = new CreateDatasetRequest(); request.setDatasetName(dataSetName); DatasetAction action = new DatasetAction(); //Create Filter for DeltaTime QueryFilter deltaTimeFilter = new QueryFilter(); deltaTimeFilter.withDeltaTime( new DeltaTime() .withOffsetSeconds(-1 * EstimatedDataDelayInSeconds) .withTimeExpression("from_unixtime(timestamp)")); //Create Action action.setActionName("SQLActionWithDeltaTime"); action.setQueryAction(new SqlQueryDatasetAction() .withSqlQuery("SELECT * from DataStoreName") .withFilters(deltaTimeFilter)); // Add Action to Actions List List<DatasetAction> actions = new ArrayList<DatasetAction>(); actions.add(action); //Create Trigger DatasetTrigger trigger = new DatasetTrigger(); trigger.setSchedule(new Schedule().withExpression("cron(0 12 * * ? *)")); //Add Trigger to Triggers List List<DatasetTrigger> triggers = new ArrayList<DatasetTrigger>(); triggers.add(trigger); // Add Triggers and Actions to CreateDatasetRequest object request.setActions(actions); request.setTriggers(triggers); // Add RetentionPeriod to CreateDatasetRequest object request.setRetentionPeriod(new RetentionPeriod().withNumberOfDays(10)); final CreateDatasetResult result = iot.createDataset(request);

成功时的输出:

{DatasetName: <datatsetName>, DatasetArn: <datatsetARN>, RetentionPeriod: {unlimited: true} or {numberOfDays: 10, unlimited: false}}

示例 3 - 创建自带计划触发器的容器数据集 (Java)

CreateDatasetRequest request = new CreateDatasetRequest(); request.setDatasetName(dataSetName); DatasetAction action = new DatasetAction(); //Create Action action.setActionName("ContainerActionDataset"); action.setContainerAction(new ContainerDatasetAction() .withImage(ImageURI) .withExecutionRoleArn(ExecutionRoleArn) .withResourceConfiguration( new ResourceConfiguration() .withComputeType(new ComputeType().withAcu(1)) .withVolumeSizeInGB(1)) .withVariables(new Variable() .withName("VariableName") .withStringValue("VariableValue")); // Add Action to Actions List List<DatasetAction> actions = new ArrayList<DatasetAction>(); actions.add(action); //Create Trigger DatasetTrigger trigger = new DatasetTrigger(); trigger.setSchedule(new Schedule().withExpression("cron(0 12 * * ? *)")); //Add Trigger to Triggers List List<DatasetTrigger> triggers = new ArrayList<DatasetTrigger>(); triggers.add(trigger); // Add Triggers and Actions to CreateDatasetRequest object request.setActions(actions); request.setTriggers(triggers); // Add RetentionPeriod to CreateDatasetRequest object request.setRetentionPeriod(new RetentionPeriod().withNumberOfDays(10)); final CreateDatasetResult result = iot.createDataset(request);

成功时的输出:

{DatasetName: <datatsetName>, DatasetArn: <datatsetARN>, RetentionPeriod: {unlimited: true} or {numberOfDays: 10, unlimited: false}}

示例 4 - 创建以 SQL 数据集作为触发器的容器数据集 (Java)

CreateDatasetRequest request = new CreateDatasetRequest(); request.setDatasetName(dataSetName); DatasetAction action = new DatasetAction(); //Create Action action.setActionName("ContainerActionDataset"); action.setContainerAction(new ContainerDatasetAction() .withImage(ImageURI) .withExecutionRoleArn(ExecutionRoleArn) .withResourceConfiguration( new ResourceConfiguration() .withComputeType(new ComputeType().withAcu(1)) .withVolumeSizeInGB(1)) .withVariables(new Variable() .withName("VariableName") .withStringValue("VariableValue")); // Add Action to Actions List List<DatasetAction> actions = new ArrayList<DatasetAction>(); actions.add(action); //Create Trigger DatasetTrigger trigger = new DatasetTrigger() .withDataset(new TriggeringDataset() .withName(TriggeringSQLDataSetName)); //Add Trigger to Triggers List List<DatasetTrigger> triggers = new ArrayList<DatasetTrigger>(); triggers.add(trigger); // Add Triggers and Actions to CreateDatasetRequest object request.setActions(actions); request.setTriggers(triggers); final CreateDatasetResult result = iot.createDataset(request);

成功时的输出:

{DatasetName: <datatsetName>, DatasetArn: <datatsetARN>}

示例 5 - 创建 SQL 数据集 (CLI)

aws iotanalytics --endpoint <EndPoint> --region <Region> create-dataset --dataset-name="<dataSetName>" --actions="[{\"actionName\":\"<ActionName>\", \"queryAction\":{\"sqlQuery\":\"<SQLQuery>\"}}]" --retentionPeriod numberOfDays=10

成功时的输出:

{ "datasetName": "<datasetName>", "datasetArn": "<datatsetARN>", "retentionPeriod": {unlimited: true} or {numberOfDays: 10, unlimited: false} }

示例 6 - 创建具有增量时段的 SQL 数据集 (CLI)

增量时段是一系列用户定义的不重叠且连续的时间间隔。通过使用增量时段,您可以使用在上次分析后到达数据存储的新数据创建数据集内容,并对新数据执行分析。您可在数据集 queryActionfilters 部分中,通过设置 deltaTime 来创建增量时段 (CreateDataset)。通常,您还希望设置时间间隔触发器以自动创建数据集内容 (triggers:schedule:expression)。基本上,这使您可以筛选在特定时间窗口中到达的消息,这样来自以前时间窗口的消息中包含的数据不会重复计数。

在该示例中,我们创建一个新数据集,它仅使用在上次传送后到达的数据每隔 15 分钟自动创建新的数据集内容。我们指定 3 分钟(180 秒)的 deltaTime 偏差,以允许到达指定数据存储的消息延迟 3 分钟。因此,如果数据集内容是在上午 10:30 创建的,则使用的数据(包含在数据集内容中)的时间戳在上午 10:12 到上午 10:27 之间(即上午 10:30 - 15 分钟 - 3 分钟到上午 10:30 - 3 分钟)。

aws iotanalytics --endpoint <EndPoint> --region <Region> create-dataset --cli-input-json file://delta-window.json

delta-window.json 文件包含以下内容。

{ "datasetName": "delta_window_example", "actions": [ { "actionName": "delta_window_action", "queryAction": { "sqlQuery": "SELECT temperature, humidity, timestamp FROM my_datastore", "filters": [ { "deltaTime": { "offsetSeconds": -180, "timeExpression": "from_unixtime(timestamp)" } } ] } } ], "triggers": [ { "schedule": { "expression": "cron(0/15 * * * ? *)" } } ] }

成功时的输出:

{ "datasetName": "<datasetName>", "datasetArn": "<datatsetARN>", }