use of org.apache.flink.table.api.TableColumn.MetadataColumn in project flink by apache.
the class DescriptorProperties method putTableSchema.
/**
* Adds a table schema under the given key.
*/
public void putTableSchema(String key, TableSchema schema) {
checkNotNull(key);
checkNotNull(schema);
final String[] fieldNames = schema.getFieldNames();
final DataType[] fieldTypes = schema.getFieldDataTypes();
final String[] fieldExpressions = schema.getTableColumns().stream().map(column -> {
if (column instanceof ComputedColumn) {
return ((ComputedColumn) column).getExpression();
}
return null;
}).toArray(String[]::new);
final String[] fieldMetadata = schema.getTableColumns().stream().map(column -> {
if (column instanceof MetadataColumn) {
return ((MetadataColumn) column).getMetadataAlias().orElse(column.getName());
}
return null;
}).toArray(String[]::new);
final String[] fieldVirtual = schema.getTableColumns().stream().map(column -> {
if (column instanceof MetadataColumn) {
return Boolean.toString(((MetadataColumn) column).isVirtual());
}
return null;
}).toArray(String[]::new);
final List<List<String>> values = new ArrayList<>();
for (int i = 0; i < schema.getFieldCount(); i++) {
values.add(Arrays.asList(fieldNames[i], fieldTypes[i].getLogicalType().asSerializableString(), fieldExpressions[i], fieldMetadata[i], fieldVirtual[i]));
}
putIndexedOptionalProperties(key, Arrays.asList(NAME, DATA_TYPE, EXPR, METADATA, VIRTUAL), values);
if (!schema.getWatermarkSpecs().isEmpty()) {
final List<List<String>> watermarkValues = new ArrayList<>();
for (WatermarkSpec spec : schema.getWatermarkSpecs()) {
watermarkValues.add(Arrays.asList(spec.getRowtimeAttribute(), spec.getWatermarkExpr(), spec.getWatermarkExprOutputType().getLogicalType().asSerializableString()));
}
putIndexedFixedProperties(key + '.' + WATERMARK, Arrays.asList(WATERMARK_ROWTIME, WATERMARK_STRATEGY_EXPR, WATERMARK_STRATEGY_DATA_TYPE), watermarkValues);
}
schema.getPrimaryKey().ifPresent(pk -> {
putString(key + '.' + PRIMARY_KEY_NAME, pk.getName());
putString(key + '.' + PRIMARY_KEY_COLUMNS, String.join(",", pk.getColumns()));
});
}
use of org.apache.flink.table.api.TableColumn.MetadataColumn in project flink by apache.
the class TableSchema method toSchema.
/**
* Helps to migrate to the new {@link Schema} class.
*/
public Schema toSchema() {
final Schema.Builder builder = Schema.newBuilder();
columns.forEach(column -> {
if (column instanceof PhysicalColumn) {
final PhysicalColumn c = (PhysicalColumn) column;
builder.column(c.getName(), c.getType());
} else if (column instanceof MetadataColumn) {
final MetadataColumn c = (MetadataColumn) column;
builder.columnByMetadata(c.getName(), c.getType(), c.getMetadataAlias().orElse(null), c.isVirtual());
} else if (column instanceof ComputedColumn) {
final ComputedColumn c = (ComputedColumn) column;
builder.columnByExpression(c.getName(), c.getExpression());
} else {
throw new IllegalArgumentException("Unsupported column type: " + column);
}
});
watermarkSpecs.forEach(spec -> builder.watermark(spec.getRowtimeAttribute(), spec.getWatermarkExpr()));
if (primaryKey != null) {
builder.primaryKeyNamed(primaryKey.getName(), primaryKey.getColumns());
}
return builder.build();
}
Aggregations