WeiZhong94 commented on a change in pull request #11448: [FLINK-16666][python][table] Support new Python dependency configuration options in flink-java, flink-streaming-java and flink-table. URL: https://github.com/apache/flink/pull/11448#discussion_r396257250
########## File path: flink-java/src/main/java/org/apache/flink/api/java/utils/PythonDependencyManager.java ########## @@ -0,0 +1,282 @@ +/* + * 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.utils; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.cache.DistributedCache; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.configuration.ConfigOptions; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.PythonOptions; +import org.apache.flink.configuration.ReadableConfig; +import org.apache.flink.util.Preconditions; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.apache.flink.configuration.PythonOptions.PYTHON_CLIENT_EXECUTABLE; +import static org.apache.flink.configuration.PythonOptions.PYTHON_EXECUTABLE; + +/** + * Utility class for Python dependency management. The dependencies will be registered at the distributed + * cache. + */ +@Internal +public class PythonDependencyManager { + + public static final String PYTHON_FILE_PREFIX = "python_file"; + public static final String PYTHON_REQUIREMENTS_FILE_PREFIX = "python_requirements_file"; + public static final String PYTHON_REQUIREMENTS_CACHE_PREFIX = "python_requirements_cache"; + public static final String PYTHON_ARCHIVE_PREFIX = "python_archive"; + public static final String CLUSTER = "cluster"; + public static final String FILE = "file"; + public static final String CACHE = "cache"; + public static final String FILE_DELIMITER = ","; + public static final String PARAM_DELIMITER = "#"; + + // Metadata of Python dependencies. + + public static final ConfigOption<Map<String, String>> PYTHON_FILES = + ConfigOptions.key("python.internal.files-key-map").mapType().noDefaultValue(); + public static final ConfigOption<Map<String, String>> PYTHON_REQUIREMENTS_FILE = + ConfigOptions.key("python.internal.requirements-file-key").mapType().noDefaultValue(); + public static final ConfigOption<Map<String, String>> PYTHON_ARCHIVES = + ConfigOptions.key("python.internal.archives-key-map").mapType().noDefaultValue(); + + private final Configuration config; + private final List<Tuple2<String, DistributedCache.DistributedCacheEntry>> cachedFiles; + private int counter = -1; + + public PythonDependencyManager( + Configuration config, + List<Tuple2<String, DistributedCache.DistributedCacheEntry>> cachedFiles) { + this.config = Preconditions.checkNotNull(config); + this.cachedFiles = cachedFiles; + } + + /** + * Adds a Python dependency which could be .py files, Python packages(.zip, .egg etc.) or + * local directories. The dependencies will be added to the PYTHONPATH of the Python UDF worker + * and the local Py4J python client. + * + * @param filePath The path of the Python dependency. + */ + public void addPythonFile(String filePath) { + Preconditions.checkNotNull(filePath); + if (fileAlreadyRegistered(filePath)) { Review comment: This case is expected because the `configure()` method will be called when the `translate()` method of the Table API planners is called. As the `translate()` can be called many times in interactive programing, a python file will be registered multiple times. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services