Search in sources :

Example 6 with AmazonPersonalize

use of com.amazonaws.services.personalize.AmazonPersonalize in project knime-cloud by knime.

the class AmazonPersonalizeCreateSolutionVersionNodeDialog method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    if (specs[0] == null) {
        throw new NotConfigurableException("No connection information available");
    }
    final CloudConnectionInformation connectionInformation = (CloudConnectionInformation) ((ConnectionInformationPortObjectSpec) specs[0]).getConnectionInformation();
    // Check if the port object has connection information
    if (connectionInformation == null) {
        throw new NotConfigurableException("No connection information available");
    }
    // List all existing dataset groups and solutions
    try (final AmazonPersonalizeConnection personalizeConnection = new AmazonPersonalizeConnection(connectionInformation)) {
        final AmazonPersonalize personalizeClient = personalizeConnection.getClient();
        final List<DatasetGroupSummary> listAllDatasetGroups = AmazonPersonalizeUtils.listAllDatasetGroups(personalizeClient);
        if (listAllDatasetGroups.size() == 0) {
            throw new NotConfigurableException("No existing dataset group found. Upload datasets first.");
        }
        final DefaultComboBoxModel<NameArnPair> comboBoxModel = new DefaultComboBoxModel<NameArnPair>(listAllDatasetGroups.stream().map(e -> NameArnPair.of(e.getName(), e.getDatasetGroupArn())).toArray(NameArnPair[]::new));
        m_comboBoxDatasetGroupList.setModel(comboBoxModel);
        final DefaultComboBoxModel<NameArnPair> comboBoxModel2 = new DefaultComboBoxModel<NameArnPair>(AmazonPersonalizeUtils.listAllSolutions(personalizeClient).stream().map(e -> NameArnPair.of(e.getName(), e.getSolutionArn())).toArray(NameArnPair[]::new));
        m_comboBoxExistingSolutions.setModel(comboBoxModel2);
        final DefaultComboBoxModel<NameArnPair> comboBoxModelUserPersonalization = new DefaultComboBoxModel<>();
        final DefaultComboBoxModel<NameArnPair> comboBoxModelPersonalizedRanking = new DefaultComboBoxModel<>();
        final DefaultComboBoxModel<NameArnPair> comboBoxModelRelatedItems = new DefaultComboBoxModel<>();
        for (RecipeSummary rs : AmazonPersonalizeUtils.listAllRecipes(personalizeClient)) {
            final Recipe recipe = personalizeClient.describeRecipe(new DescribeRecipeRequest().withRecipeArn(rs.getRecipeArn())).getRecipe();
            final NameArnPair nameArnPair = NameArnPair.of(recipe.getName(), recipe.getRecipeArn());
            switch(RecipeType.ofRecipeType(recipe)) {
                case USER_PERSONALIZATION:
                    comboBoxModelUserPersonalization.addElement(nameArnPair);
                    break;
                case PERSONALIZED_RANKING:
                    comboBoxModelPersonalizedRanking.addElement(nameArnPair);
                    break;
                case RELATED_ITEMS:
                    comboBoxModelRelatedItems.addElement(nameArnPair);
                    break;
            }
        }
        m_comboBoxUserPersonalizationRecipeList.setModel(comboBoxModelUserPersonalization);
        m_comboBoxPersonalizedRankingRecipeList.setModel(comboBoxModelPersonalizedRanking);
        m_comboBoxRelatedItemsRecipeList.setModel(comboBoxModelRelatedItems);
    } catch (Exception e) {
        throw new NotConfigurableException(e.getMessage());
    }
    // Loading
    final AmazonPersonalizeCreateSolutionVersionNodeSettings nodeSettings = new AmazonPersonalizeCreateSolutionVersionNodeSettings();
    nodeSettings.loadSettingsForDialog(settings);
    m_radioButtonCreateNewSolution.setSelected(nodeSettings.isCreateNewSolution());
    m_radioButtonUseExistingSolution.setSelected(!nodeSettings.isCreateNewSolution());
    final NameArnPair datasetGroup = nodeSettings.getDatasetGroup();
    if (datasetGroup == null) {
        m_comboBoxDatasetGroupList.setSelectedIndex(0);
    } else {
        m_comboBoxDatasetGroupList.setSelectedItem(datasetGroup);
    }
    final RecipeSelection recipeSelection = nodeSettings.getRecipeSelection();
    m_radioButtonPredefinedRecipe.setSelected(recipeSelection == RecipeSelection.PREDEFINED);
    m_radioButtonUserDefinedRecipe.setSelected(recipeSelection == RecipeSelection.USER_DEFINED);
    m_radioButtonAutoML.setSelected(recipeSelection == RecipeSelection.AUTOML);
    final RecipeType predefinedRecipeType = nodeSettings.getPredefinedRecipeType();
    final NameArnPair predefinedRecipe = nodeSettings.getPredefinedRecipe();
    if (predefinedRecipeType == RecipeType.USER_PERSONALIZATION) {
        m_radioButtonUserPersonalization.setSelected(true);
        if (predefinedRecipe != null) {
            m_comboBoxUserPersonalizationRecipeList.setSelectedItem(predefinedRecipe);
        }
    }
    if (predefinedRecipeType == RecipeType.PERSONALIZED_RANKING) {
        m_radioButtonPersonalizedRanking.setSelected(true);
        m_comboBoxPersonalizedRankingRecipeList.setSelectedItem(predefinedRecipe);
    }
    if (predefinedRecipeType == RecipeType.RELATED_ITEMS) {
        m_radioButtonRelatedItems.setSelected(true);
        m_comboBoxRelatedItemsRecipeList.setSelectedItem(predefinedRecipe);
    }
    m_textFieldUserDefinedRecipeArn.setText(nodeSettings.getUserDefinedRecipeArn());
    m_checkBoxHyperParamOpt.setSelected(nodeSettings.isHyperparameterOpt());
    m_textFieldPrefixSolutionName.setText(nodeSettings.getSolutionName());
    m_checkBoxOutputSolutionVersionARNAsVar.setSelected(nodeSettings.isOutputSolutionVersionArnAsVar());
    final NameArnPair existingSolution = nodeSettings.getExistingSolution();
    if (existingSolution == null && m_comboBoxExistingSolutions.getModel().getSize() > 0) {
        m_comboBoxExistingSolutions.setSelectedIndex(0);
    } else {
        m_comboBoxExistingSolutions.setSelectedItem(existingSolution);
    }
    enablePanelComponents(m_createNewSolutionPanel, !m_radioButtonUseExistingSolution.isSelected());
    enablePanelComponents(m_useExistingSolutionPanel, m_radioButtonUseExistingSolution.isSelected());
    enableComponents();
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) NameArnPair(org.knime.cloud.aws.mlservices.utils.personalize.NameArnPair) DescribeRecipeRequest(com.amazonaws.services.personalize.model.DescribeRecipeRequest) Recipe(com.amazonaws.services.personalize.model.Recipe) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) DatasetGroupSummary(com.amazonaws.services.personalize.model.DatasetGroupSummary) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NotConfigurableException(org.knime.core.node.NotConfigurableException) RecipeType(org.knime.cloud.aws.mlservices.utils.personalize.RecipeType) CloudConnectionInformation(org.knime.cloud.core.util.port.CloudConnectionInformation) RecipeSummary(com.amazonaws.services.personalize.model.RecipeSummary)

Example 7 with AmazonPersonalize

use of com.amazonaws.services.personalize.AmazonPersonalize in project knime-cloud by knime.

the class AbstractAmazonPersonalizePredictNodeDialog method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    final ConnectionInformationPortObjectSpec object = (ConnectionInformationPortObjectSpec) specs[0];
    final CloudConnectionInformation connectionInformation = (CloudConnectionInformation) object.getConnectionInformation();
    // Check if the port object has connection information
    if (connectionInformation == null) {
        throw new NotConfigurableException("No connection information available");
    }
    // List all existing campaigns
    try (final AmazonPersonalizeConnection personalizeConnection = new AmazonPersonalizeConnection(connectionInformation)) {
        final AmazonPersonalize personalizeClient = personalizeConnection.getClient();
        // Filter only the campaigns that have a solution with the proper recipe type
        final DefaultComboBoxModel<NameArnPair> comboBoxModel = new DefaultComboBoxModel<NameArnPair>(AmazonPersonalizeUtils.listAllCampaigns(personalizeClient).stream().filter(e -> {
            final String recipeType = personalizeClient.describeRecipe(new DescribeRecipeRequest().withRecipeArn(personalizeClient.describeSolutionVersion(new DescribeSolutionVersionRequest().withSolutionVersionArn(personalizeClient.describeCampaign(new DescribeCampaignRequest().withCampaignArn(e.getCampaignArn())).getCampaign().getSolutionVersionArn())).getSolutionVersion().getRecipeArn())).getRecipe().getRecipeType();
            return recipeType.equals(getRecipeType().getType());
        }).map(e -> new NameArnPair(e.getName(), e.getCampaignArn())).toArray(NameArnPair[]::new));
        m_comboBoxCampaigns.setModel(comboBoxModel);
        if (comboBoxModel.getSize() == 0) {
            throw new NotConfigurableException("No campaign of type '" + getRecipeType().toString() + "' found. You can create one using the 'Amazon Personalize Create Campaign' node.");
        }
    } catch (Exception e) {
        throw new NotConfigurableException(e.getMessage());
    }
    // Loading
    final DataTableSpec spec = (DataTableSpec) specs[1];
    m_settings.loadSettingsForDialog(settings);
    final NameArnPair campaign = m_settings.getCampaign();
    if (campaign != null) {
        m_comboBoxCampaigns.setSelectedItem(campaign);
    } else {
        m_comboBoxCampaigns.setSelectedItem(m_comboBoxCampaigns.getItemAt(0));
    }
    m_colSelectionUserID.update(spec, m_settings.getUserIDCol());
    m_colSelectionItemID.update(spec, m_settings.getItemIDCol());
    m_radioButtonFail.setSelected(m_settings.getMissingValueHandling() == MissingValueHandling.FAIL);
    m_radioButtonIgnore.setSelected(m_settings.getMissingValueHandling() == MissingValueHandling.IGNORE);
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ConnectionInformationPortObjectSpec(org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformationPortObjectSpec) NodeDialogPane(org.knime.core.node.NodeDialogPane) CloudConnectionInformation(org.knime.cloud.core.util.port.CloudConnectionInformation) NotConfigurableException(org.knime.core.node.NotConfigurableException) AmazonPersonalizeUtils(org.knime.cloud.aws.mlservices.utils.personalize.AmazonPersonalizeUtils) NodeUtils(org.knime.base.filehandling.NodeUtils) ColumnSelectionPanel(org.knime.core.node.util.ColumnSelectionPanel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) JComboBox(javax.swing.JComboBox) AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) DescribeRecipeRequest(com.amazonaws.services.personalize.model.DescribeRecipeRequest) StringValue(org.knime.core.data.StringValue) NameArnPair(org.knime.cloud.aws.mlservices.utils.personalize.NameArnPair) ButtonGroup(javax.swing.ButtonGroup) DescribeSolutionVersionRequest(com.amazonaws.services.personalize.model.DescribeSolutionVersionRequest) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) BorderFactory(javax.swing.BorderFactory) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) GridBagConstraints(java.awt.GridBagConstraints) JRadioButton(javax.swing.JRadioButton) RecipeType(org.knime.cloud.aws.mlservices.utils.personalize.RecipeType) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DataValueColumnFilter(org.knime.core.node.util.DataValueColumnFilter) JLabel(javax.swing.JLabel) GridBagLayout(java.awt.GridBagLayout) DescribeCampaignRequest(com.amazonaws.services.personalize.model.DescribeCampaignRequest) JPanel(javax.swing.JPanel) WordUtils(org.apache.commons.lang.WordUtils) AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) NameArnPair(org.knime.cloud.aws.mlservices.utils.personalize.NameArnPair) DescribeCampaignRequest(com.amazonaws.services.personalize.model.DescribeCampaignRequest) DescribeRecipeRequest(com.amazonaws.services.personalize.model.DescribeRecipeRequest) DataTableSpec(org.knime.core.data.DataTableSpec) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NotConfigurableException(org.knime.core.node.NotConfigurableException) ConnectionInformationPortObjectSpec(org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformationPortObjectSpec) DescribeSolutionVersionRequest(com.amazonaws.services.personalize.model.DescribeSolutionVersionRequest) CloudConnectionInformation(org.knime.cloud.core.util.port.CloudConnectionInformation)

Example 8 with AmazonPersonalize

use of com.amazonaws.services.personalize.AmazonPersonalize in project knime-cloud by knime.

the class AbstractAmazonPersonalizeDataUploadNodeDialog method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    // Check if a port object is available
    if (specs[0] == null) {
        throw new NotConfigurableException("No connection information available.");
    }
    final ConnectionInformationPortObjectSpec object = (ConnectionInformationPortObjectSpec) specs[0];
    m_connectionInformation = (CloudConnectionInformation) object.getConnectionInformation();
    // Check if the port object has connection information
    if (m_connectionInformation == null) {
        throw new NotConfigurableException("No connection information available.");
    }
    m_fileChooserTarget.setConnectionInformation(m_connectionInformation);
    // Try to list all available roles
    try {
        final DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<String>(AmazonPersonalizeUtils.listAllRoles(m_connectionInformation).stream().map(e -> e.getArn()).toArray(String[]::new));
        m_comboBoxRoleList.setModel(comboBoxModel);
        m_labelNoListRolePermissions.setVisible(false);
        m_radioButtonAvailableRole.setEnabled(true);
    } catch (Exception e) {
        if (!(e instanceof AmazonIdentityManagementException)) {
            throw new NotConfigurableException(e.getMessage());
        }
        // AmazonIdentityManagementException happens if the user does not have permissions to list roles
        m_radioButtonAvailableRole.setEnabled(false);
    }
    // List all existing dataset groups
    try (final AmazonPersonalizeConnection personalizeConnection = new AmazonPersonalizeConnection(m_connectionInformation)) {
        final AmazonPersonalize personalize = personalizeConnection.getClient();
        final DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<String>(AmazonPersonalizeUtils.listAllDatasetGroups(personalize).stream().map(e -> e.getName()).toArray(String[]::new));
        m_comboBoxDatasetGroupList.setModel(comboBoxModel);
    } catch (Exception e) {
        throw new NotConfigurableException(e.getMessage());
    }
    // Load settings
    m_spec = (DataTableSpec) specs[1];
    m_settings.loadSettingsForDialog(settings, m_spec);
    m_fileChooserTarget.setSelection(m_settings.getTarget());
    // IAM service role
    final String iamServiceRoleArn = m_settings.getIamServiceRoleArn();
    boolean customServiceRole = ((DefaultComboBoxModel<String>) m_comboBoxRoleList.getModel()).getIndexOf(iamServiceRoleArn) < 0;
    m_buttonGroupRoleSelection.setSelected(m_radioButtonCustomRole.getModel(), customServiceRole);
    m_buttonGroupRoleSelection.setSelected(m_radioButtonAvailableRole.getModel(), !customServiceRole);
    if (customServiceRole) {
        m_textFieldCustomRole.setText(iamServiceRoleArn);
    } else {
        m_comboBoxRoleList.setSelectedItem(iamServiceRoleArn);
    }
    // Dataset group
    final String selectedDatasetGroup = m_settings.getSelectedDatasetGroup();
    boolean createNewDatasetGroup = ((DefaultComboBoxModel<String>) m_comboBoxDatasetGroupList.getModel()).getIndexOf(selectedDatasetGroup) < 0;
    m_buttonGroupDatasetGroup.setSelected(m_radioButtonUploadToNewDatasetGroup.getModel(), createNewDatasetGroup);
    m_buttonGroupDatasetGroup.setSelected(m_radioButtonUploadToExistingDatasetGroup.getModel(), !createNewDatasetGroup);
    if (createNewDatasetGroup) {
        m_textFieldDatasetGroupName.setText(selectedDatasetGroup);
    } else {
        m_comboBoxDatasetGroupList.setSelectedItem(selectedDatasetGroup);
    }
    final OverwritePolicy overwritePolicy = OverwritePolicy.valueOf(m_settings.getOverwriteDatasetPolicy());
    m_buttonGroupOverwriteDataset.setSelected(m_radioButtonOverwriteExistingDataset.getModel(), overwritePolicy == OverwritePolicy.OVERWRITE);
    m_buttonGroupOverwriteDataset.setSelected(m_radioButtonAbortOnExistingDataset.getModel(), overwritePolicy == OverwritePolicy.ABORT);
    m_textFieldDatasetName.setText(m_settings.getDatasetName());
    m_textFieldImportJobNamePrefix.setText(m_settings.getPrefixImportJobName());
    m_textFieldSchemaNamePrefix.setText(m_settings.getPrefixSchemaName());
    DataColumnSpecFilterConfiguration filterConfig = m_settings.getFilterConfig();
    m_columnFilterPanel.loadConfiguration(filterConfig, m_spec);
    enableComponents();
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) ConnectionInformationPortObjectSpec(org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformationPortObjectSpec) AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) DataColumnSpecFilterConfiguration(org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration) AmazonIdentityManagementException(com.amazonaws.services.identitymanagement.model.AmazonIdentityManagementException) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NotConfigurableException(org.knime.core.node.NotConfigurableException) AmazonIdentityManagementException(com.amazonaws.services.identitymanagement.model.AmazonIdentityManagementException)

Example 9 with AmazonPersonalize

use of com.amazonaws.services.personalize.AmazonPersonalize in project knime-cloud by knime.

the class AbstractAmazonPersonalizeDataUploadNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    // === Write table out as CSV ===  (TODO we may be able to write it directly to S3)
    // Filter included columns
    final BufferedDataTable filterTable = filterTable((BufferedDataTable) inObjects[TABLE_INPUT_PORT_IDX], exec);
    // Rename columns to fit the later created schema
    final BufferedDataTable adaptedTable = renameColumns(filterTable, exec);
    // Check if the input is valid (just a shallow check, there is no clear documentation by Amazon)
    exec.setMessage("Validating input");
    validateInputTableContent(adaptedTable);
    exec.setProgress(0.05);
    // Write the table as CSV to disc
    final URI sourceURI = writeCSV(adaptedTable, exec.createSubExecutionContext(0.1));
    // === Upload CSV to S3 ===
    final CloudConnectionInformation cxnInfo = ((AmazonConnectionInformationPortObject) inObjects[0]).getConnectionInformation();
    final String uniqueFilePath = m_settings.getTarget() + "KNIME-tmp-" + StringUtils.lowerCase(getDatasetType()) + "-file-" + System.currentTimeMillis() + ".csv";
    final RemoteFile<Connection> target = writeToS3(exec.createSubExecutionContext(0.1), sourceURI, cxnInfo, uniqueFilePath);
    // === Import data from S3 to Amazon Personalize service ===
    try (final AmazonPersonalizeConnection personalizeConnection = new AmazonPersonalizeConnection(cxnInfo)) {
        final AmazonPersonalize personalizeClient = personalizeConnection.getClient();
        // Create the dataset group ARN or use existing one
        final String datasetGroupArn = createDatasetGroup(personalizeClient, exec.createSubExecutionContext(0.2));
        // Check if the respective dataset already exists and either delete it or abort
        checkAlreadyExistingDataset(personalizeClient, datasetGroupArn, exec.createSubExecutionContext(0.1));
        exec.setProgress(0.5);
        // Create the data set (container)
        exec.setMessage("Importing dataset from S3");
        final String schemaArn = createSchema(personalizeClient, adaptedTable.getDataTableSpec());
        final String datasetArn = personalizeClient.createDataset(new CreateDatasetRequest().withDatasetGroupArn(datasetGroupArn).withDatasetType(m_datasetType).withName(m_settings.getDatasetName()).withSchemaArn(schemaArn)).getDatasetArn();
        try {
            // Import the dataset from S3
            importDataFromS3(personalizeClient, "s3:/" + uniqueFilePath, datasetArn, exec);
        } catch (RuntimeException | InterruptedException e1) {
            try {
                deleteDataset(personalizeClient, datasetGroupArn, datasetArn);
            } catch (InterruptedException e) {
            // happens if user cancels node execution during deletion of dataset
            // do nothing, deletion will be further processed by amazon
            }
            throw e1;
        }
    } catch (RuntimeException e) {
        // TODO cancel import job, currently not supported but hopefully in future versions
        throw e;
    } finally {
        // Remove temporary created S3 file
        target.delete();
    }
    return null;
}
Also used : AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) CreateDatasetRequest(com.amazonaws.services.personalize.model.CreateDatasetRequest) Connection(org.knime.base.filehandling.remote.files.Connection) AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) URI(java.net.URI) AmazonConnectionInformationPortObject(org.knime.cloud.aws.util.AmazonConnectionInformationPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) CloudConnectionInformation(org.knime.cloud.core.util.port.CloudConnectionInformation)

Example 10 with AmazonPersonalize

use of com.amazonaws.services.personalize.AmazonPersonalize 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;
}
Also used : ConnectionMonitor(org.knime.base.filehandling.remote.files.ConnectionMonitor) Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) AmazonConnectionInformationPortObject(org.knime.cloud.aws.util.AmazonConnectionInformationPortObject) CSVWriter(org.knime.base.node.io.csvwriter.CSVWriter) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) URISyntaxException(java.net.URISyntaxException) ListDatasetGroupsResult(com.amazonaws.services.personalize.model.ListDatasetGroupsResult) DescribeDatasetGroupResult(com.amazonaws.services.personalize.model.DescribeDatasetGroupResult) RemoteFile(org.knime.base.filehandling.remote.files.RemoteFile) CreateDatasetGroupResult(com.amazonaws.services.personalize.model.CreateDatasetGroupResult) CreateDatasetImportJobRequest(com.amazonaws.services.personalize.model.CreateDatasetImportJobRequest) InvalidInputException(com.amazonaws.services.personalize.model.InvalidInputException) Status(org.knime.cloud.aws.mlservices.utils.personalize.AmazonPersonalizeUtils.Status) DataColumnSpec(org.knime.core.data.DataColumnSpec) Map(java.util.Map) FieldAssembler(org.apache.avro.SchemaBuilder.FieldAssembler) URI(java.net.URI) DeleteDatasetGroupRequest(com.amazonaws.services.personalize.model.DeleteDatasetGroupRequest) DescribeDatasetImportJobRequest(com.amazonaws.services.personalize.model.DescribeDatasetImportJobRequest) PortType(org.knime.core.node.port.PortType) FileWriterSettings(org.knime.base.node.io.csvwriter.FileWriterSettings) IntValue(org.knime.core.data.IntValue) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Schema(org.apache.avro.Schema) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) NodeModel(org.knime.core.node.NodeModel) Collectors(java.util.stream.Collectors) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) RemoteFileFactory(org.knime.base.filehandling.remote.files.RemoteFileFactory) Optional(java.util.Optional) DataSource(com.amazonaws.services.personalize.model.DataSource) DescribeDatasetImportJobResult(com.amazonaws.services.personalize.model.DescribeDatasetImportJobResult) PortObject(org.knime.core.node.port.PortObject) LongValue(org.knime.core.data.LongValue) DataTableSpec(org.knime.core.data.DataTableSpec) DatasetGroupSummary(com.amazonaws.services.personalize.model.DatasetGroupSummary) DescribeDatasetGroupRequest(com.amazonaws.services.personalize.model.DescribeDatasetGroupRequest) HashMap(java.util.HashMap) DatasetSummary(com.amazonaws.services.personalize.model.DatasetSummary) BufferedOutputStream(java.io.BufferedOutputStream) ExecutionContext(org.knime.core.node.ExecutionContext) CloudConnectionInformation(org.knime.cloud.core.util.port.CloudConnectionInformation) Connection(org.knime.base.filehandling.remote.files.Connection) AmazonPersonalizeUtils(org.knime.cloud.aws.mlservices.utils.personalize.AmazonPersonalizeUtils) CreateSchemaRequest(com.amazonaws.services.personalize.model.CreateSchemaRequest) OutputStreamWriter(java.io.OutputStreamWriter) AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) DataCell(org.knime.core.data.DataCell) Type(org.apache.avro.Schema.Type) StringValue(org.knime.core.data.StringValue) CreateDatasetGroupRequest(com.amazonaws.services.personalize.model.CreateDatasetGroupRequest) ConnectionInformation(org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformation) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) CreateDatasetRequest(com.amazonaws.services.personalize.model.CreateDatasetRequest) ListDatasetsResult(com.amazonaws.services.personalize.model.ListDatasetsResult) FileOutputStream(java.io.FileOutputStream) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) DatasetSchemaSummary(com.amazonaws.services.personalize.model.DatasetSchemaSummary) DeleteDatasetRequest(com.amazonaws.services.personalize.model.DeleteDatasetRequest) File(java.io.File) DataRow(org.knime.core.data.DataRow) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) ListDatasetGroupsRequest(com.amazonaws.services.personalize.model.ListDatasetGroupsRequest) ListDatasetsRequest(com.amazonaws.services.personalize.model.ListDatasetsRequest) StringUtils(com.amazonaws.util.StringUtils) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) FileUtil(org.knime.core.util.FileUtil) DescribeDatasetGroupResult(com.amazonaws.services.personalize.model.DescribeDatasetGroupResult) DatasetGroupSummary(com.amazonaws.services.personalize.model.DatasetGroupSummary) CreateDatasetGroupResult(com.amazonaws.services.personalize.model.CreateDatasetGroupResult) CreateDatasetGroupRequest(com.amazonaws.services.personalize.model.CreateDatasetGroupRequest) ListDatasetGroupsRequest(com.amazonaws.services.personalize.model.ListDatasetGroupsRequest) ListDatasetGroupsResult(com.amazonaws.services.personalize.model.ListDatasetGroupsResult) DescribeDatasetGroupRequest(com.amazonaws.services.personalize.model.DescribeDatasetGroupRequest) DeleteDatasetGroupRequest(com.amazonaws.services.personalize.model.DeleteDatasetGroupRequest)

Aggregations

AmazonPersonalize (com.amazonaws.services.personalize.AmazonPersonalize)12 AmazonPersonalizeConnection (org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection)12 CloudConnectionInformation (org.knime.cloud.core.util.port.CloudConnectionInformation)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)9 AmazonConnectionInformationPortObject (org.knime.cloud.aws.util.AmazonConnectionInformationPortObject)8 AmazonPersonalizeUtils (org.knime.cloud.aws.mlservices.utils.personalize.AmazonPersonalizeUtils)6 DataTableSpec (org.knime.core.data.DataTableSpec)6 BufferedDataTable (org.knime.core.node.BufferedDataTable)6 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)6 NodeSettingsWO (org.knime.core.node.NodeSettingsWO)6 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)6 CreateDatasetRequest (com.amazonaws.services.personalize.model.CreateDatasetRequest)5 File (java.io.File)5 IOException (java.io.IOException)5 CreateDatasetGroupRequest (com.amazonaws.services.personalize.model.CreateDatasetGroupRequest)4 CreateDatasetGroupResult (com.amazonaws.services.personalize.model.CreateDatasetGroupResult)4 CreateDatasetImportJobRequest (com.amazonaws.services.personalize.model.CreateDatasetImportJobRequest)4 CreateSchemaRequest (com.amazonaws.services.personalize.model.CreateSchemaRequest)4 DatasetGroupSummary (com.amazonaws.services.personalize.model.DatasetGroupSummary)4 URI (java.net.URI)4