Java Code Examples for org.apache.flink.table.types.DataType#bridgedTo()

The following examples show how to use org.apache.flink.table.types.DataType#bridgedTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiveBatchSource.java    From Alink with Apache License 2.0 6 votes vote down vote up
@Override
public DataType getProducedDataType() {
    TableSchema fullSchema = getTableSchema();
    DataType type;
    if (projectedFields == null) {
        type = fullSchema.toRowDataType();
    } else {
        String[] fullNames = fullSchema.getFieldNames();
        DataType[] fullTypes = fullSchema.getFieldDataTypes();
        type = TableSchema.builder().fields(
            Arrays.stream(projectedFields).mapToObj(i -> fullNames[i]).toArray(String[]::new),
            Arrays.stream(projectedFields).mapToObj(i -> fullTypes[i]).toArray(DataType[]::new))
            .build().toRowDataType();
    }
    return type.bridgedTo(BaseRow.class);
}
 
Example 2
Source File: DataTypeTemplate.java    From flink with Apache License 2.0 6 votes vote down vote up
private static DataType extractDataType(
		DataTypeFactory typeFactory,
		@Nullable String typeName,
		@Nullable Class<?> conversionClass,
		DataTypeTemplate template) {
	// explicit data type
	if (typeName != null) {
		// RAW type
		if (typeName.equals(RAW_TYPE_NAME)) {
			return createRawType(typeFactory, template.rawSerializer, conversionClass);
		}
		// regular type that must be resolvable
		final DataType resolvedDataType = typeFactory.createDataType(typeName);
		if (conversionClass != null) {
			return resolvedDataType.bridgedTo(conversionClass);
		}
		return resolvedDataType;
	}
	// extracted data type
	else if (conversionClass != null) {
		return DataTypeExtractor.extractFromType(typeFactory, template, conversionClass);
	}
	throw ExtractionUtils.extractionError(
		"Data type hint does neither specify an explicit data type or conversion class " +
			"from which a data type could be extracted.");
}
 
Example 3
Source File: CsvTableSource.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a field with the field name and the data type. Required. This method can be
 * called multiple times. The call order of this method defines also the order of the fields
 * in a row.
 *
 * @param fieldName the field name
 * @param fieldType the data type of the field
 */
public Builder field(String fieldName, DataType fieldType) {
	if (schema.containsKey(fieldName)) {
		throw new IllegalArgumentException("Duplicate field name " + fieldName);
	}
	// CSV only support java.sql.Timestamp/Date/Time
	DataType type;
	switch (fieldType.getLogicalType().getTypeRoot()) {
		case TIMESTAMP_WITHOUT_TIME_ZONE:
			type = fieldType.bridgedTo(Timestamp.class);
			break;
		case TIME_WITHOUT_TIME_ZONE:
			type = fieldType.bridgedTo(Time.class);
			break;
		case DATE:
			type = fieldType.bridgedTo(Date.class);
			break;
		default:
			type = fieldType;
	}
	schema.put(fieldName, type);
	return this;
}
 
Example 4
Source File: DataTypeConversionClassTransformation.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataType transform(DataType dataType) {
	LogicalType logicalType = dataType.getLogicalType();
	Class<?> conversionClass = conversions.get(logicalType.getTypeRoot());
	if (conversionClass != null) {
		return dataType.bridgedTo(conversionClass);
	} else {
		return dataType;
	}
}
 
Example 5
Source File: DataTypeExtractor.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Use closest class for data type if possible. Even though a hint might have provided some data
 * type, in many cases, the conversion class can be enriched with the extraction type itself.
 */
private DataType closestBridging(DataType dataType, @Nullable Class<?> clazz) {
	// no context class or conversion class is already more specific than context class
	if (clazz == null || clazz.isAssignableFrom(dataType.getConversionClass())) {
		return dataType;
	}
	final LogicalType logicalType = dataType.getLogicalType();
	final boolean supportsConversion = logicalType.supportsInputConversion(clazz) ||
		logicalType.supportsOutputConversion(clazz);
	if (supportsConversion) {
		return dataType.bridgedTo(clazz);
	}
	return dataType;
}
 
Example 6
Source File: ArrayFieldReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private Class<?> getElementClass(LogicalType elementType) {
	DataType dataType = TypeConversions.fromLogicalToDataType(elementType);
	if (elementType instanceof TimestampType) {
		// the default conversion class is java.time.LocalDateTime
		dataType = dataType.bridgedTo(Timestamp.class);
	} else if (elementType instanceof DateType) {
		// the default conversion class is java.time.LocalDate
		dataType = dataType.bridgedTo(Date.class);
	} else if (elementType instanceof TimeType) {
		// the default conversion class is java.time.LocalTime
		dataType = dataType.bridgedTo(Time.class);
	}
	return dataType.getConversionClass();
}
 
Example 7
Source File: HiveTableSink.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public DataType getConsumedDataType() {
	DataType dataType = getTableSchema().toRowDataType();
	return isBounded ? dataType : dataType.bridgedTo(RowData.class);
}