use of com.amazonaws.services.personalize.model.CreateDatasetGroupResult in project knime-cloud by knime.
the class AbstractAmazonPersonalizeDataUploadNodeModel method createDatasetGroup.
// Creates a new dataset group if not already existing
private String createDatasetGroup(final AmazonPersonalize personalizeClient, final ExecutionContext exec) throws InterruptedException {
exec.setMessage("Creating dataset group");
final ListDatasetGroupsRequest listDatasetGroupsRequest = new ListDatasetGroupsRequest();
final ListDatasetGroupsResult listDatasetGroups = personalizeClient.listDatasetGroups(listDatasetGroupsRequest);
final String datasetGroupName = m_settings.getSelectedDatasetGroup();
final String datasetGroupArn;
final boolean existing = listDatasetGroups.getDatasetGroups().stream().anyMatch(e -> e.getName().equals(datasetGroupName));
if (!existing) {
// Create new dataset group
final CreateDatasetGroupResult createDatasetGroup = personalizeClient.createDatasetGroup(new CreateDatasetGroupRequest().withName(datasetGroupName));
datasetGroupArn = createDatasetGroup.getDatasetGroupArn();
} else {
final Optional<DatasetGroupSummary> dataGroupSummary = listDatasetGroups.getDatasetGroups().stream().filter(e -> e.getName().equals(datasetGroupName)).findFirst();
if (!dataGroupSummary.isPresent()) {
// should never happen
throw new IllegalStateException("Dataset group with name '" + datasetGroupName + "' not present.");
}
datasetGroupArn = dataGroupSummary.get().getDatasetGroupArn();
}
// Wait until dataset group is created and ACTIVE (even if the group already existed, make sure it's ACTIVE)
final DescribeDatasetGroupRequest describeDatasetGroupRequest = new DescribeDatasetGroupRequest();
describeDatasetGroupRequest.setDatasetGroupArn(datasetGroupArn);
AmazonPersonalizeUtils.waitUntilActive(() -> {
final DescribeDatasetGroupResult datasetGroupDescription = personalizeClient.describeDatasetGroup(describeDatasetGroupRequest);
final String status = datasetGroupDescription.getDatasetGroup().getStatus();
exec.setMessage("Creating dataset group (Status: " + status + ")");
if (status.equals(Status.CREATED_FAILED.getStatus())) {
if (!existing) {
// Delete the dataset group that we tried to create
personalizeClient.deleteDatasetGroup(new DeleteDatasetGroupRequest().withDatasetGroupArn(datasetGroupArn));
// Wait until the dataset group is deleted (should usually be very quick but you never know...)
try {
AmazonPersonalizeUtils.waitUntilActive(() -> {
return !personalizeClient.listDatasetGroups(listDatasetGroupsRequest).getDatasetGroups().stream().anyMatch(e -> e.getName().equals(datasetGroupName));
}, 50);
} catch (InterruptedException e1) {
// unlikely case
// do nothing, the deletion will be further processed by amazon
}
throw new IllegalStateException("Dataset group creation failed. Reason: " + datasetGroupDescription.getDatasetGroup().getFailureReason());
}
throw new IllegalStateException("The selected dataset group is in an invalid state: " + Status.CREATED_FAILED.getStatus() + ". Reason: " + datasetGroupDescription.getDatasetGroup().getFailureReason());
}
return status.equals(Status.ACTIVE.getStatus());
}, 500);
exec.setProgress(1);
return datasetGroupArn;
}
Aggregations