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

org.apache.flink.api.java.operators.KeyFunctions 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.api.java.operators;

import org.apache.flink.annotation.Internal;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.operators.Keys.SelectorFunctionKeys;
import org.apache.flink.api.common.operators.UnaryOperatorInformation;
import org.apache.flink.api.common.operators.Union;
import org.apache.flink.api.common.operators.base.MapOperatorBase;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.operators.translation.KeyExtractingMapper;
import org.apache.flink.api.java.operators.translation.KeyRemovingMapper;
import org.apache.flink.api.java.operators.translation.TwoKeyExtractingMapper;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.api.java.typeutils.TupleTypeInfo;

/**
 * This class holds static utilities to append functions that extract and
 * prune keys.
 */
@Internal
public class KeyFunctions {

	@SuppressWarnings("unchecked")
	public static  org.apache.flink.api.common.operators.Operator> appendKeyExtractor(
			org.apache.flink.api.common.operators.Operator input,
			SelectorFunctionKeys key) {

		if (input instanceof Union) {
			// if input is a union, we apply the key extractors recursively to all inputs
			org.apache.flink.api.common.operators.Operator firstInput = ((Union) input).getFirstInput();
			org.apache.flink.api.common.operators.Operator secondInput = ((Union) input).getSecondInput();

			org.apache.flink.api.common.operators.Operator> firstInputWithKey =
					appendKeyExtractor(firstInput, key);
			org.apache.flink.api.common.operators.Operator> secondInputWithKey =
					appendKeyExtractor(secondInput, key);

			return new Union(firstInputWithKey, secondInputWithKey, input.getName());
		}

		TypeInformation inputType = key.getInputType();
		TypeInformation> typeInfoWithKey = createTypeWithKey(key);
		KeyExtractingMapper extractor = new KeyExtractingMapper(key.getKeyExtractor());

		MapOperatorBase, MapFunction>> mapper =
				new MapOperatorBase, MapFunction>>(
						extractor,
						new UnaryOperatorInformation(inputType, typeInfoWithKey),
						"Key Extractor"
				);

		mapper.setInput(input);
		mapper.setParallelism(input.getParallelism());

		return mapper;
	}

	@SuppressWarnings("unchecked")
	public static  org.apache.flink.api.common.operators.Operator> appendKeyExtractor(
			org.apache.flink.api.common.operators.Operator input,
			SelectorFunctionKeys key1,
			SelectorFunctionKeys key2) {

		if (input instanceof Union) {
			// if input is a union, we apply the key extractors recursively to all inputs
			org.apache.flink.api.common.operators.Operator firstInput = ((Union) input).getFirstInput();
			org.apache.flink.api.common.operators.Operator secondInput = ((Union) input).getSecondInput();

			org.apache.flink.api.common.operators.Operator> firstInputWithKey =
					appendKeyExtractor(firstInput, key1, key2);
			org.apache.flink.api.common.operators.Operator> secondInputWithKey =
					appendKeyExtractor(secondInput, key1, key2);

			return new Union(firstInputWithKey, secondInputWithKey, input.getName());
		}

		TypeInformation inputType = key1.getInputType();
		TypeInformation> typeInfoWithKey = createTypeWithKey(key1, key2);
		TwoKeyExtractingMapper extractor =
				new TwoKeyExtractingMapper<>(key1.getKeyExtractor(), key2.getKeyExtractor());

		MapOperatorBase, MapFunction>> mapper =
				new MapOperatorBase, MapFunction>>(
						extractor,
						new UnaryOperatorInformation<>(inputType, typeInfoWithKey),
						"Key Extractor"
				);

		mapper.setInput(input);
		mapper.setParallelism(input.getParallelism());

		return mapper;
	}

	public static  org.apache.flink.api.common.operators.SingleInputOperator appendKeyRemover(
			org.apache.flink.api.common.operators.Operator> inputWithKey,
			SelectorFunctionKeys key) {

		TypeInformation inputType = key.getInputType();
		TypeInformation> typeInfoWithKey = createTypeWithKey(key);

		MapOperatorBase, T, MapFunction, T>> mapper =
				new MapOperatorBase, T, MapFunction, T>>(
						new KeyRemovingMapper(),
						new UnaryOperatorInformation<>(typeInfoWithKey, inputType),
						"Key Remover"
				);
		mapper.setInput(inputWithKey);
		mapper.setParallelism(inputWithKey.getParallelism());

		return mapper;
	}

	public static  TypeInformation> createTypeWithKey(
			SelectorFunctionKeys key) {
		return new TupleTypeInfo<>(key.getKeyType(), key.getInputType());
	}

	public static  TypeInformation> createTypeWithKey(
			SelectorFunctionKeys key1,
			SelectorFunctionKeys key2) {
		return new TupleTypeInfo<>(key1.getKeyType(), key2.getKeyType(), key1.getInputType());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy