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

通过Java和使用CreateDatasetAPI AWS CLI

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

示例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)

Delta窗口是一系列用户定义的、非重叠的和连续的时间间隔。Delta窗口允许您创建数据集内容,并对自上次分析以来已到达数据仓库的新数据执行分析。通过设置 deltaTimefilters 部分 queryAction (创建数据集)。通常,您需要通过设置时间间隔触发器自动创建数据集内容(triggers:schedule:expression)。基本上,这使您能够过滤在特定时间窗口内到达的消息,因此以前时间窗口的消息中包含的数据不会被计数两次。

在此示例中,我们使用自上次以来收到的数据,每15分钟创建一个自动创建新数据集内容的新数据集。我们指定 3 分钟(180 秒)的 deltaTime 偏差,以允许到达指定数据存储的消息延迟 3 分钟。因此,如果数据集内容在上午10:30创建,则使用的数据(包含在数据集内容中)将是时间戳在上午10:12到上午10:27之间的数据(即上午10:#0-15分钟-3分钟-10:30AM-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>", }