use of com.amazonaws.services.personalize.AmazonPersonalize in project knime-cloud by knime.
the class AbstractAmazonPersonalizeDataUploadNodeModel method createSchema.
private String createSchema(final AmazonPersonalize personalizeClient, final DataTableSpec spec) {
final StringBuilder schemaNameBuilder = new StringBuilder(getSchemaNamePrefix());
FieldAssembler<Schema> fieldAssembler = createFieldAssembler(SCHEMA_NAMESPACE);
for (final String colName : spec.getColumnNames()) {
if (!colName.startsWith(PREFIX_METADATA_FIELD)) {
continue;
}
final DataColumnSpec colSpec = spec.getColumnSpec(colName);
final boolean isCategorical;
final Type type;
if (colSpec.getType().isCompatible(StringValue.class)) {
isCategorical = true;
type = Type.STRING;
} else if (colSpec.getType().isCompatible(IntValue.class)) {
isCategorical = false;
type = Type.INT;
} else if (colSpec.getType().isCompatible(LongValue.class)) {
isCategorical = false;
type = Type.LONG;
} else {
isCategorical = false;
type = Type.DOUBLE;
}
schemaNameBuilder.append("-" + type);
// 'categorical' must be set for metadata
fieldAssembler = fieldAssembler.name(colName).prop("categorical", isCategorical).type(Schema.create(type)).noDefault();
}
final String schemaName = schemaNameBuilder.toString();
// check if the same schema has been created before
final List<DatasetSchemaSummary> existingSchemas = AmazonPersonalizeUtils.listAllSchemas(personalizeClient);
final Optional<DatasetSchemaSummary> schemaSummary = existingSchemas.stream().filter(e -> e.getName().equals(schemaName)).findAny();
// if so, use this one again
if (schemaSummary.isPresent()) {
return schemaSummary.get().getSchemaArn();
}
// otherwise create new one
final Schema schema = fieldAssembler.endRecord();
final CreateSchemaRequest createSchemaRequest = new CreateSchemaRequest().withName(schemaName).withSchema(schema.toString());
return personalizeClient.createSchema(createSchemaRequest).getSchemaArn();
}
use of com.amazonaws.services.personalize.AmazonPersonalize in project knime-cloud by knime.
the class AbstractAmazonPersonalizeDataUploadNodeModel method checkAlreadyExistingDataset.
private void checkAlreadyExistingDataset(final AmazonPersonalize personalizeClient, final String datasetGroupArn, final ExecutionContext exec) throws InterruptedException {
exec.setMessage("Checking already existing datasets");
final ListDatasetsResult listDatasets = personalizeClient.listDatasets(new ListDatasetsRequest().withDatasetGroupArn(datasetGroupArn));
final Optional<DatasetSummary> dataset = listDatasets.getDatasets().stream().filter(e -> e.getDatasetType().equals(m_datasetType)).findFirst();
if (dataset.isPresent()) {
if (m_settings.getOverwriteDatasetPolicy().equals(OverwritePolicy.ABORT.toString())) {
// Abort if dataset already exists
throw new IllegalStateException("A dataset of type '" + getDatasetType() + "' already exists. Either choose a different dataset group or select to overwrite the existing " + "dataset.");
} else {
// Delete the existing dataset
exec.setMessage("Deleting existing dataset");
deleteDataset(personalizeClient, datasetGroupArn, dataset.get().getDatasetArn());
}
}
exec.setProgress(1);
}
Aggregations