Search in sources :

Example 6 with PythonDependencyInfo

use of org.apache.flink.python.env.PythonDependencyInfo in project flink by apache.

the class ProcessPythonEnvironmentManagerTest method testPythonFiles.

@Test
public void testPythonFiles() throws Exception {
    // use LinkedHashMap to preserve the path order in environment variable
    Map<String, String> pythonFiles = new LinkedHashMap<>();
    pythonFiles.put(String.join(File.separator, tmpDir, "zip0"), "test_zip.zip");
    pythonFiles.put(String.join(File.separator, tmpDir, "file1"), "test_file1.py");
    pythonFiles.put(String.join(File.separator, tmpDir, "file2"), "test_file2.egg");
    pythonFiles.put(String.join(File.separator, tmpDir, "dir0"), "test_dir");
    PythonDependencyInfo dependencyInfo = new PythonDependencyInfo(pythonFiles, null, null, new HashMap<>(), "python");
    try (ProcessPythonEnvironmentManager environmentManager = createBasicPythonEnvironmentManager(dependencyInfo)) {
        environmentManager.open();
        String baseDir = environmentManager.getBaseDirectory();
        Map<String, String> environmentVariable = environmentManager.getPythonEnv();
        String[] expectedUserPythonPaths = new String[] { String.join(File.separator, baseDir, PYTHON_FILES_DIR, "zip0", "test_zip"), String.join(File.separator, baseDir, PYTHON_FILES_DIR, "file1"), String.join(File.separator, baseDir, PYTHON_FILES_DIR, "file2", "test_file2.egg"), String.join(File.separator, baseDir, PYTHON_FILES_DIR, "dir0", "test_dir") };
        String expectedPythonPath = String.join(File.pathSeparator, expectedUserPythonPaths);
        assertEquals(expectedPythonPath, environmentVariable.get("PYTHONPATH"));
        assertFileEquals(new File(String.join(File.separator, tmpDir, "file1")), new File(String.join(File.separator, baseDir, PYTHON_FILES_DIR, "file1", "test_file1.py")));
        assertFileEquals(new File(String.join(File.separator, tmpDir, "zipExpected0")), new File(String.join(File.separator, baseDir, PYTHON_FILES_DIR, "zip0", "test_zip")));
        assertFileEquals(new File(String.join(File.separator, tmpDir, "file2")), new File(String.join(File.separator, baseDir, PYTHON_FILES_DIR, "file2", "test_file2.egg")));
        assertFileEquals(new File(String.join(File.separator, tmpDir, "dir0")), new File(String.join(File.separator, baseDir, PYTHON_FILES_DIR, "dir0", "test_dir")));
    }
}
Also used : PythonDependencyInfo(org.apache.flink.python.env.PythonDependencyInfo) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 7 with PythonDependencyInfo

use of org.apache.flink.python.env.PythonDependencyInfo in project flink by apache.

the class ProcessPythonEnvironmentManagerTest method testCreateRetrievalToken.

@Test
public void testCreateRetrievalToken() throws Exception {
    PythonDependencyInfo dependencyInfo = new PythonDependencyInfo(new HashMap<>(), null, null, new HashMap<>(), "python");
    Map<String, String> sysEnv = new HashMap<>();
    sysEnv.put("FLINK_HOME", "/flink");
    try (ProcessPythonEnvironmentManager environmentManager = new ProcessPythonEnvironmentManager(dependencyInfo, new String[] { tmpDir }, sysEnv, new JobID())) {
        environmentManager.open();
        String retrievalToken = environmentManager.createRetrievalToken();
        File retrievalTokenFile = new File(retrievalToken);
        byte[] content = new byte[(int) retrievalTokenFile.length()];
        try (DataInputStream input = new DataInputStream(new FileInputStream(retrievalToken))) {
            input.readFully(content);
        }
        assertEquals("{\"manifest\": {}}", new String(content));
    }
}
Also used : PythonDependencyInfo(org.apache.flink.python.env.PythonDependencyInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataInputStream(java.io.DataInputStream) File(java.io.File) JobID(org.apache.flink.api.common.JobID) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 8 with PythonDependencyInfo

use of org.apache.flink.python.env.PythonDependencyInfo in project flink by apache.

the class ProcessPythonEnvironmentManagerTest method testOpenClose.

@Test
public void testOpenClose() throws Exception {
    PythonDependencyInfo dependencyInfo = new PythonDependencyInfo(new HashMap<>(), null, null, new HashMap<>(), "python");
    try (ProcessPythonEnvironmentManager environmentManager = createBasicPythonEnvironmentManager(dependencyInfo)) {
        environmentManager.open();
        environmentManager.createRetrievalToken();
        String tmpBase = environmentManager.getBaseDirectory();
        assertTrue(new File(tmpBase).isDirectory());
        environmentManager.close();
        assertFalse(new File(tmpBase).exists());
    }
}
Also used : PythonDependencyInfo(org.apache.flink.python.env.PythonDependencyInfo) File(java.io.File) Test(org.junit.Test)

Example 9 with PythonDependencyInfo

use of org.apache.flink.python.env.PythonDependencyInfo in project flink by apache.

the class AbstractExternalPythonFunctionOperator method createPythonEnvironmentManager.

@Override
protected ProcessPythonEnvironmentManager createPythonEnvironmentManager() {
    PythonDependencyInfo dependencyInfo = PythonDependencyInfo.create(pythonConfig, getRuntimeContext().getDistributedCache());
    PythonEnv pythonEnv = getPythonEnv();
    if (pythonEnv.getExecType() == PythonEnv.ExecType.PROCESS) {
        return new ProcessPythonEnvironmentManager(dependencyInfo, getContainingTask().getEnvironment().getTaskManagerInfo().getTmpDirectories(), new HashMap<>(System.getenv()), getRuntimeContext().getJobId());
    } else {
        throw new UnsupportedOperationException(String.format("Execution type '%s' is not supported.", pythonEnv.getExecType()));
    }
}
Also used : PythonDependencyInfo(org.apache.flink.python.env.PythonDependencyInfo) ProcessPythonEnvironmentManager(org.apache.flink.python.env.process.ProcessPythonEnvironmentManager) PythonEnv(org.apache.flink.table.functions.python.PythonEnv)

Aggregations

PythonDependencyInfo (org.apache.flink.python.env.PythonDependencyInfo)9 Test (org.junit.Test)7 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)4 HashMap (java.util.HashMap)3 JobID (org.apache.flink.api.common.JobID)3 ProcessPythonEnvironmentManager (org.apache.flink.python.env.process.ProcessPythonEnvironmentManager)2 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 PythonEnv (org.apache.flink.table.functions.python.PythonEnv)1