use of com.datastax.driver.core.schemabuilder.Alter in project sdc by onap.
the class SdcSchemaBuilder method alterTable.
/**
* check if there are new columns that were added to definition but don't exist in DB
*
* @param session
* @param existingTablesMetadata
* @param tableDescription
* @param tableName
* @param columnDescription
*/
private static void alterTable(Session session, Map<String, List<String>> existingTablesMetadata, ITableDescription tableDescription, String tableName, Map<String, ImmutablePair<DataType, Boolean>> columnDescription) {
List<String> definedTableColumns = existingTablesMetadata.get(tableName);
// add column to casandra if was added to table definition
for (Map.Entry<String, ImmutablePair<DataType, Boolean>> column : columnDescription.entrySet()) {
String columnName = column.getKey();
if (!definedTableColumns.contains(columnName.toLowerCase())) {
log.info("Adding new column {} to the table {}", columnName, tableName);
Alter alter = SchemaBuilder.alterTable(tableDescription.getKeyspace(), tableDescription.getTableName());
SchemaStatement addColumn = alter.addColumn(columnName).type(column.getValue().getLeft());
log.trace("executing :{}", addColumn);
session.execute(addColumn);
}
}
}
use of com.datastax.driver.core.schemabuilder.Alter in project kafka-connect-scylladb by scylladb.
the class ScyllaDbSchemaBuilder method alter.
void alter(final ScyllaDbSchemaKey key, String tableName, SinkRecord record, TableMetadata.Table tableMetadata, TopicConfigs topicConfigs) {
Preconditions.checkNotNull(tableMetadata, "tableMetadata cannot be null.");
Preconditions.checkNotNull(record.valueSchema(), "valueSchema cannot be null.");
log.trace("alter() - tableMetadata = '{}' ", tableMetadata);
Map<String, DataType> addedColumns = new LinkedHashMap<>();
if (topicConfigs != null && topicConfigs.isScyllaColumnsMapped()) {
if (topicConfigs.getTablePartitionKeyMap().keySet().size() != tableMetadata.primaryKey().size()) {
throw new DataException(String.format("Cannot alter primary key of a ScyllaDb Table. Existing primary key: '%s', " + "Primary key mapped in 'topic.my_topic.my_ks.my_table.mapping' config: '%s", Joiner.on("', '").join(tableMetadata.primaryKey()), Joiner.on("', '").join(topicConfigs.getTablePartitionKeyMap().keySet())));
}
for (Map.Entry<String, TopicConfigs.KafkaScyllaColumnMapper> entry : topicConfigs.getTableColumnMap().entrySet()) {
String columnName = entry.getValue().getScyllaColumnName();
log.trace("alter for mapping() - Checking if table has '{}' column.", columnName);
final TableMetadata.Column columnMetadata = tableMetadata.columnMetadata(columnName);
if (null == columnMetadata) {
log.debug("alter for mapping() - Adding column '{}'", columnName);
final DataType dataType = dataType(entry.getValue().getKafkaRecordField().schema());
addedColumns.put(Metadata.quoteIfNecessary(columnName), dataType);
} else {
log.trace("alter for mapping() - Table already has '{}' column.", columnName);
}
}
} else {
for (final Field field : record.valueSchema().fields()) {
log.trace("alter() - Checking if table has '{}' column.", field.name());
final TableMetadata.Column columnMetadata = tableMetadata.columnMetadata(field.name());
if (null == columnMetadata) {
log.debug("alter() - Adding column '{}'", field.name());
DataType dataType = dataType(field.schema());
addedColumns.put(Metadata.quoteIfNecessary(field.name()), dataType);
} else {
log.trace("alter() - Table already has '{}' column.", field.name());
}
}
}
if (!addedColumns.isEmpty()) {
final Alter alterTable = SchemaBuilder.alterTable(this.config.keyspace, tableName);
if (!this.config.tableManageEnabled) {
List<String> requiredAlterStatements = addedColumns.entrySet().stream().map(e -> alterTable.addColumn(e.getKey()).type(e.getValue()).toString()).collect(Collectors.toList());
throw new DataException(String.format("Alter statement(s) needed. Missing column(s): '%s'\n%s;", Joiner.on("', '").join(addedColumns.keySet()), Joiner.on(';').join(requiredAlterStatements)));
} else {
String query = alterTable.withOptions().compressionOptions(config.tableCompressionAlgorithm).buildInternal();
this.session.executeQuery(query);
for (Map.Entry<String, DataType> e : addedColumns.entrySet()) {
final String columnName = e.getKey();
final DataType dataType = e.getValue();
final Statement alterStatement = alterTable.addColumn(columnName).type(dataType);
this.session.executeStatement(alterStatement);
}
this.session.onTableChanged(this.config.keyspace, tableName);
}
}
this.schemaLookup.put(key, DEFAULT);
}
Aggregations