use of com.amazonaws.services.personalize.model.CreateSchemaRequest 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();
}
Aggregations