All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.streaming.api.datastream.StreamProjection Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.streaming.api.datastream;

import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple1;
import org.apache.flink.api.java.tuple.Tuple10;
import org.apache.flink.api.java.tuple.Tuple11;
import org.apache.flink.api.java.tuple.Tuple12;
import org.apache.flink.api.java.tuple.Tuple13;
import org.apache.flink.api.java.tuple.Tuple14;
import org.apache.flink.api.java.tuple.Tuple15;
import org.apache.flink.api.java.tuple.Tuple16;
import org.apache.flink.api.java.tuple.Tuple17;
import org.apache.flink.api.java.tuple.Tuple18;
import org.apache.flink.api.java.tuple.Tuple19;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple20;
import org.apache.flink.api.java.tuple.Tuple21;
import org.apache.flink.api.java.tuple.Tuple22;
import org.apache.flink.api.java.tuple.Tuple23;
import org.apache.flink.api.java.tuple.Tuple24;
import org.apache.flink.api.java.tuple.Tuple25;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.api.java.tuple.Tuple4;
import org.apache.flink.api.java.tuple.Tuple5;
import org.apache.flink.api.java.tuple.Tuple6;
import org.apache.flink.api.java.tuple.Tuple7;
import org.apache.flink.api.java.tuple.Tuple8;
import org.apache.flink.api.java.tuple.Tuple9;
import org.apache.flink.api.java.typeutils.TupleTypeInfo;
import org.apache.flink.streaming.api.operators.StreamProject;

import com.google.common.base.Preconditions;

public class StreamProjection {

	private DataStream dataStream;
	private int[] fieldIndexes;

	protected StreamProjection(DataStream dataStream, int[] fieldIndexes) {
		if (!dataStream.getType().isTupleType()) {
			throw new RuntimeException("Only Tuple DataStreams can be projected");
		}
		if(fieldIndexes.length == 0) {
			throw new IllegalArgumentException("project() needs to select at least one (1) field.");
		} else if(fieldIndexes.length > Tuple.MAX_ARITY - 1) {
			throw new IllegalArgumentException(
					"project() may select only up to (" + (Tuple.MAX_ARITY - 1) + ") fields.");
		}

		int maxFieldIndex = (dataStream.getType()).getArity();
		for(int i = 0; i < fieldIndexes.length; i++) {
			Preconditions.checkElementIndex(fieldIndexes[i], maxFieldIndex);
		}

		this.dataStream = dataStream;
		this.fieldIndexes = fieldIndexes;
	}

	/**
	 * Chooses a projectTupleX according to the length of
	 * {@link org.apache.flink.streaming.api.datastream.StreamProjection#fieldIndexes}
	 *
	 * @return The projected DataStream.
	 * @see org.apache.flink.api.java.operators.ProjectOperator.Projection
	 */
	@SuppressWarnings("unchecked")
	public  SingleOutputStreamOperator projectTupleX() {
		SingleOutputStreamOperator projOperator = null;

		switch (fieldIndexes.length) {
			case 1: projOperator = (SingleOutputStreamOperator) projectTuple1(); break;
			case 2: projOperator = (SingleOutputStreamOperator) projectTuple2(); break;
			case 3: projOperator = (SingleOutputStreamOperator) projectTuple3(); break;
			case 4: projOperator = (SingleOutputStreamOperator) projectTuple4(); break;
			case 5: projOperator = (SingleOutputStreamOperator) projectTuple5(); break;
			case 6: projOperator = (SingleOutputStreamOperator) projectTuple6(); break;
			case 7: projOperator = (SingleOutputStreamOperator) projectTuple7(); break;
			case 8: projOperator = (SingleOutputStreamOperator) projectTuple8(); break;
			case 9: projOperator = (SingleOutputStreamOperator) projectTuple9(); break;
			case 10: projOperator = (SingleOutputStreamOperator) projectTuple10(); break;
			case 11: projOperator = (SingleOutputStreamOperator) projectTuple11(); break;
			case 12: projOperator = (SingleOutputStreamOperator) projectTuple12(); break;
			case 13: projOperator = (SingleOutputStreamOperator) projectTuple13(); break;
			case 14: projOperator = (SingleOutputStreamOperator) projectTuple14(); break;
			case 15: projOperator = (SingleOutputStreamOperator) projectTuple15(); break;
			case 16: projOperator = (SingleOutputStreamOperator) projectTuple16(); break;
			case 17: projOperator = (SingleOutputStreamOperator) projectTuple17(); break;
			case 18: projOperator = (SingleOutputStreamOperator) projectTuple18(); break;
			case 19: projOperator = (SingleOutputStreamOperator) projectTuple19(); break;
			case 20: projOperator = (SingleOutputStreamOperator) projectTuple20(); break;
			case 21: projOperator = (SingleOutputStreamOperator) projectTuple21(); break;
			case 22: projOperator = (SingleOutputStreamOperator) projectTuple22(); break;
			case 23: projOperator = (SingleOutputStreamOperator) projectTuple23(); break;
			case 24: projOperator = (SingleOutputStreamOperator) projectTuple24(); break;
			case 25: projOperator = (SingleOutputStreamOperator) projectTuple25(); break;
			default:
				throw new IllegalStateException("Excessive arity in tuple.");
		}

		return projOperator;
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple1() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(
				fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple2() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple3() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple4() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple5() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple6() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple7() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple8() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple9() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple10() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple11() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple12() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple13() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple14() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple15() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple16() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple17() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple18() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple19() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple20() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple21() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple22() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple23() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple24() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	/**
	 * Projects a {@link Tuple} {@link DataStream} to the previously selected fields.
	 *
	 * @return The projected DataStream.
	 * @see Tuple
	 * @see DataStream
	 */
	public  SingleOutputStreamOperator, ?> projectTuple25() {
		TypeInformation[] fTypes = extractFieldTypes(fieldIndexes, dataStream.getType());
		TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);

		return dataStream.transform("Projection", tType, new StreamProject>(fieldIndexes, tType.createSerializer(dataStream.getExecutionConfig())));
	}

	public static TypeInformation[] extractFieldTypes(int[] fields, TypeInformation inType) {

		TupleTypeInfo inTupleType = (TupleTypeInfo) inType;
		TypeInformation[] fieldTypes = new TypeInformation[fields.length];

		for (int i = 0; i < fields.length; i++) {
			fieldTypes[i] = inTupleType.getTypeAt(fields[i]);
		}

		return fieldTypes;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy