Java Code Examples for org.apache.flink.api.common.typeutils.CompositeType#getFieldNames()

The following examples show how to use org.apache.flink.api.common.typeutils.CompositeType#getFieldNames() . 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 Project: Flink-CEPplus   File: TableSchema.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a table schema from a {@link TypeInformation} instance. If the type information is
 * a {@link CompositeType}, the field names and types for the composite type are used to
 * construct the {@link TableSchema} instance. Otherwise, a table schema with a single field
 * is created. The field name is "f0" and the field type the provided type.
 *
 * @param typeInfo The {@link TypeInformation} from which the table schema is generated.
 * @return The table schema that was generated from the given {@link TypeInformation}.
 */
public static TableSchema fromTypeInfo(TypeInformation<?> typeInfo) {
	if (typeInfo instanceof CompositeType<?>) {
		final CompositeType<?> compositeType = (CompositeType<?>) typeInfo;
		// get field names and types from composite type
		final String[] fieldNames = compositeType.getFieldNames();
		final TypeInformation<?>[] fieldTypes = new TypeInformation[fieldNames.length];
		for (int i = 0; i < fieldTypes.length; i++) {
			fieldTypes[i] = compositeType.getTypeAt(i);
		}
		return new TableSchema(fieldNames, fieldTypes);
	} else {
		// create table schema with a single field named "f0" of the given type.
		return new TableSchema(
			new String[]{ATOMIC_TYPE_FIELD_NAME},
			new TypeInformation<?>[]{typeInfo});
	}
}
 
Example 2
Source Project: Flink-CEPplus   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
private static <T> String getSerializerTree(TypeInformation<T> ti, int indent) {
	String ret = "";
	if (ti instanceof CompositeType) {
		ret += StringUtils.repeat(' ', indent) + ti.getClass().getSimpleName() + "\n";
		CompositeType<T> cti = (CompositeType<T>) ti;
		String[] fieldNames = cti.getFieldNames();
		for (int i = 0; i < cti.getArity(); i++) {
			TypeInformation<?> fieldType = cti.getTypeAt(i);
			ret += StringUtils.repeat(' ', indent + 2) + fieldNames[i] + ":" + getSerializerTree(fieldType, indent);
		}
	} else {
		if (ti instanceof GenericTypeInfo) {
			ret += StringUtils.repeat(' ', indent) + "GenericTypeInfo (" + ti.getTypeClass().getSimpleName() + ")\n";
			ret += getGenericTypeTree(ti.getTypeClass(), indent + 4);
		} else {
			ret += StringUtils.repeat(' ', indent) + ti.toString() + "\n";
		}
	}
	return ret;
}
 
Example 3
Source Project: flink   File: TableSchema.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a table schema from a {@link TypeInformation} instance. If the type information is
 * a {@link CompositeType}, the field names and types for the composite type are used to
 * construct the {@link TableSchema} instance. Otherwise, a table schema with a single field
 * is created. The field name is "f0" and the field type the provided type.
 *
 * @param typeInfo The {@link TypeInformation} from which the table schema is generated.
 * @return The table schema that was generated from the given {@link TypeInformation}.
 *
 * @deprecated This method will be removed soon. Use {@link DataTypes} to declare types.
 */
@Deprecated
public static TableSchema fromTypeInfo(TypeInformation<?> typeInfo) {
	if (typeInfo instanceof CompositeType<?>) {
		final CompositeType<?> compositeType = (CompositeType<?>) typeInfo;
		// get field names and types from composite type
		final String[] fieldNames = compositeType.getFieldNames();
		final TypeInformation<?>[] fieldTypes = new TypeInformation[fieldNames.length];
		for (int i = 0; i < fieldTypes.length; i++) {
			fieldTypes[i] = compositeType.getTypeAt(i);
		}
		return new TableSchema(fieldNames, fieldTypes);
	} else {
		// create table schema with a single field named "f0" of the given type.
		return new TableSchema(
			new String[]{ATOMIC_TYPE_FIELD_NAME},
			new TypeInformation<?>[]{typeInfo});
	}
}
 
Example 4
Source Project: flink   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
private static <T> String getSerializerTree(TypeInformation<T> ti, int indent) {
	String ret = "";
	if (ti instanceof CompositeType) {
		ret += StringUtils.repeat(' ', indent) + ti.getClass().getSimpleName() + "\n";
		CompositeType<T> cti = (CompositeType<T>) ti;
		String[] fieldNames = cti.getFieldNames();
		for (int i = 0; i < cti.getArity(); i++) {
			TypeInformation<?> fieldType = cti.getTypeAt(i);
			ret += StringUtils.repeat(' ', indent + 2) + fieldNames[i] + ":" + getSerializerTree(fieldType, indent);
		}
	} else {
		if (ti instanceof GenericTypeInfo) {
			ret += StringUtils.repeat(' ', indent) + "GenericTypeInfo (" + ti.getTypeClass().getSimpleName() + ")\n";
			ret += getGenericTypeTree(ti.getTypeClass(), indent + 4);
		} else {
			ret += StringUtils.repeat(' ', indent) + ti.toString() + "\n";
		}
	}
	return ret;
}
 
Example 5
Source Project: flink   File: TableSchema.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a table schema from a {@link TypeInformation} instance. If the type information is
 * a {@link CompositeType}, the field names and types for the composite type are used to
 * construct the {@link TableSchema} instance. Otherwise, a table schema with a single field
 * is created. The field name is "f0" and the field type the provided type.
 *
 * @param typeInfo The {@link TypeInformation} from which the table schema is generated.
 * @return The table schema that was generated from the given {@link TypeInformation}.
 *
 * @deprecated This method will be removed soon. Use {@link DataTypes} to declare types.
 */
@Deprecated
public static TableSchema fromTypeInfo(TypeInformation<?> typeInfo) {
	if (typeInfo instanceof CompositeType<?>) {
		final CompositeType<?> compositeType = (CompositeType<?>) typeInfo;
		// get field names and types from composite type
		final String[] fieldNames = compositeType.getFieldNames();
		final TypeInformation<?>[] fieldTypes = new TypeInformation[fieldNames.length];
		for (int i = 0; i < fieldTypes.length; i++) {
			fieldTypes[i] = compositeType.getTypeAt(i);
		}
		return new TableSchema(fieldNames, fieldTypes);
	} else {
		// create table schema with a single field named "f0" of the given type.
		return new TableSchema(
			new String[]{ATOMIC_TYPE_FIELD_NAME},
			new TypeInformation<?>[]{typeInfo});
	}
}
 
Example 6
Source Project: flink   File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
private static <T> String getSerializerTree(TypeInformation<T> ti, int indent) {
	String ret = "";
	if (ti instanceof CompositeType) {
		ret += StringUtils.repeat(' ', indent) + ti.getClass().getSimpleName() + "\n";
		CompositeType<T> cti = (CompositeType<T>) ti;
		String[] fieldNames = cti.getFieldNames();
		for (int i = 0; i < cti.getArity(); i++) {
			TypeInformation<?> fieldType = cti.getTypeAt(i);
			ret += StringUtils.repeat(' ', indent + 2) + fieldNames[i] + ":" + getSerializerTree(fieldType, indent);
		}
	} else {
		if (ti instanceof GenericTypeInfo) {
			ret += StringUtils.repeat(' ', indent) + "GenericTypeInfo (" + ti.getTypeClass().getSimpleName() + ")\n";
			ret += getGenericTypeTree(ti.getTypeClass(), indent + 4);
		} else {
			ret += StringUtils.repeat(' ', indent) + ti.toString() + "\n";
		}
	}
	return ret;
}
 
Example 7
Source Project: flink   File: DataTypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static TableSchema expandLegacyCompositeType(DataType dataType) {
	// legacy composite type
	CompositeType<?> compositeType = (CompositeType<?>) ((LegacyTypeInformationType<?>) dataType.getLogicalType())
		.getTypeInformation();

	String[] fieldNames = compositeType.getFieldNames();
	TypeInformation<?>[] fieldTypes = Arrays.stream(fieldNames)
		.map(compositeType::getTypeAt)
		.toArray(TypeInformation[]::new);

	return new TableSchema(fieldNames, fieldTypes);
}
 
Example 8
Source Project: Flink-CEPplus   File: Keys.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create int-based (non-nested) field position keys on a tuple type.
 */
public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) {

	if (!type.isTupleType() || !(type instanceof CompositeType)) {
		throw new InvalidProgramException("Specifying keys via field positions is only valid " +
				"for tuple data types. Type: " + type);
	}
	if (type.getArity() == 0) {
		throw new InvalidProgramException("Tuple size must be greater than 0. Size: " + type.getArity());
	}
	if (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) {
		throw new IllegalArgumentException("The grouping fields must not be empty.");
	}

	this.keyFields = new ArrayList<>();

	if (keyPositions == null || keyPositions.length == 0) {
		// use all tuple fields as key fields
		keyPositions = createIncrIntArray(type.getArity());
	} else {
		rangeCheckFields(keyPositions, type.getArity() - 1);
	}

	checkArgument(keyPositions.length > 0, "Grouping fields can not be empty at this point");

	// extract key field types
	CompositeType<T> cType = (CompositeType<T>)type;
	this.keyFields = new ArrayList<>(type.getTotalFields());

	// for each key position, find all (nested) field types
	String[] fieldNames = cType.getFieldNames();
	this.originalKeyTypes = new TypeInformation<?>[keyPositions.length];
	ArrayList<FlatFieldDescriptor> tmpList = new ArrayList<>();
	for (int i = 0; i < keyPositions.length; i++) {
		int keyPos = keyPositions[i];
		tmpList.clear();
		// get all flat fields
		this.originalKeyTypes[i] = cType.getTypeAt(keyPos);
		cType.getFlatFields(fieldNames[keyPos], 0, tmpList);
		// check if fields are of key type
		for(FlatFieldDescriptor ffd : tmpList) {
			if(!ffd.getType().isKeyType()) {
				throw new InvalidProgramException("This type (" + ffd.getType() + ") cannot be used as key.");
			}
		}
		this.keyFields.addAll(tmpList);
	}
}
 
Example 9
Source Project: flink   File: Keys.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create int-based (non-nested) field position keys on a tuple type.
 */
public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) {

	if (!type.isTupleType() || !(type instanceof CompositeType)) {
		throw new InvalidProgramException("Specifying keys via field positions is only valid " +
				"for tuple data types. Type: " + type);
	}
	if (type.getArity() == 0) {
		throw new InvalidProgramException("Tuple size must be greater than 0. Size: " + type.getArity());
	}
	if (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) {
		throw new IllegalArgumentException("The grouping fields must not be empty.");
	}

	this.keyFields = new ArrayList<>();

	if (keyPositions == null || keyPositions.length == 0) {
		// use all tuple fields as key fields
		keyPositions = createIncrIntArray(type.getArity());
	} else {
		rangeCheckFields(keyPositions, type.getArity() - 1);
	}

	checkArgument(keyPositions.length > 0, "Grouping fields can not be empty at this point");

	// extract key field types
	CompositeType<T> cType = (CompositeType<T>)type;
	this.keyFields = new ArrayList<>(type.getTotalFields());

	// for each key position, find all (nested) field types
	String[] fieldNames = cType.getFieldNames();
	this.originalKeyTypes = new TypeInformation<?>[keyPositions.length];
	ArrayList<FlatFieldDescriptor> tmpList = new ArrayList<>();
	for (int i = 0; i < keyPositions.length; i++) {
		int keyPos = keyPositions[i];
		tmpList.clear();
		// get all flat fields
		this.originalKeyTypes[i] = cType.getTypeAt(keyPos);
		cType.getFlatFields(fieldNames[keyPos], 0, tmpList);
		// check if fields are of key type
		for(FlatFieldDescriptor ffd : tmpList) {
			if(!ffd.getType().isKeyType()) {
				throw new InvalidProgramException("This type (" + ffd.getType() + ") cannot be used as key.");
			}
		}
		this.keyFields.addAll(tmpList);
	}
}
 
Example 10
Source Project: flink   File: Keys.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create int-based (non-nested) field position keys on a tuple type.
 */
public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) {

	if (!type.isTupleType() || !(type instanceof CompositeType)) {
		throw new InvalidProgramException("Specifying keys via field positions is only valid " +
				"for tuple data types. Type: " + type);
	}
	if (type.getArity() == 0) {
		throw new InvalidProgramException("Tuple size must be greater than 0. Size: " + type.getArity());
	}
	if (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) {
		throw new IllegalArgumentException("The grouping fields must not be empty.");
	}

	this.keyFields = new ArrayList<>();

	if (keyPositions == null || keyPositions.length == 0) {
		// use all tuple fields as key fields
		keyPositions = createIncrIntArray(type.getArity());
	} else {
		rangeCheckFields(keyPositions, type.getArity() - 1);
	}

	checkArgument(keyPositions.length > 0, "Grouping fields can not be empty at this point");

	// extract key field types
	CompositeType<T> cType = (CompositeType<T>)type;
	this.keyFields = new ArrayList<>(type.getTotalFields());

	// for each key position, find all (nested) field types
	String[] fieldNames = cType.getFieldNames();
	this.originalKeyTypes = new TypeInformation<?>[keyPositions.length];
	ArrayList<FlatFieldDescriptor> tmpList = new ArrayList<>();
	for (int i = 0; i < keyPositions.length; i++) {
		int keyPos = keyPositions[i];
		tmpList.clear();
		// get all flat fields
		this.originalKeyTypes[i] = cType.getTypeAt(keyPos);
		cType.getFlatFields(fieldNames[keyPos], 0, tmpList);
		// check if fields are of key type
		for(FlatFieldDescriptor ffd : tmpList) {
			if(!ffd.getType().isKeyType()) {
				throw new InvalidProgramException("This type (" + ffd.getType() + ") cannot be used as key.");
			}
		}
		this.keyFields.addAll(tmpList);
	}
}