Search in sources :

Example 1 with Executor

use of org.apache.flink.table.client.gateway.Executor in project flink by apache.

the class LocalExecutorITCase method testCompleteStatement.

@Test
public void testCompleteStatement() {
    final Executor executor = createLocalExecutor();
    String sessionId = executor.openSession("test-session");
    assertEquals("test-session", sessionId);
    initSession(executor, sessionId, Collections.emptyMap());
    final List<String> expectedTableHints = Arrays.asList("default_catalog.default_database.TableNumber1", "default_catalog.default_database.TableSourceSink");
    assertEquals(expectedTableHints, executor.completeStatement(sessionId, "SELECT * FROM Ta", 16));
    final List<String> expectedClause = Collections.singletonList("WHERE");
    assertEquals(expectedClause, executor.completeStatement(sessionId, "SELECT * FROM TableNumber1 WH", 29));
    final List<String> expectedField = Arrays.asList("IntegerField1");
    assertEquals(expectedField, executor.completeStatement(sessionId, "SELECT * FROM TableNumber1 WHERE Inte", 37));
    executor.closeSession(sessionId);
}
Also used : Executor(org.apache.flink.table.client.gateway.Executor) Test(org.junit.Test)

Example 2 with Executor

use of org.apache.flink.table.client.gateway.Executor in project flink by apache.

the class LocalExecutorITCase method testBatchQueryExecution.

@Test(timeout = 90_000L)
public void testBatchQueryExecution() throws Exception {
    final URL url = getClass().getClassLoader().getResource("test-data.csv");
    Objects.requireNonNull(url);
    final Map<String, String> replaceVars = new HashMap<>();
    replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
    final Map<String, String> configMap = new HashMap<>();
    configMap.put(EXECUTION_RESULT_MODE.key(), ResultMode.TABLE.name());
    configMap.put(RUNTIME_MODE.key(), RuntimeExecutionMode.BATCH.name());
    final Executor executor = createLocalExecutor(Collections.singletonList(udfDependency), Configuration.fromMap(configMap));
    String sessionId = executor.openSession("test-session");
    assertEquals("test-session", sessionId);
    initSession(executor, sessionId, replaceVars);
    try {
        final ResultDescriptor desc = executeQuery(executor, sessionId, "SELECT *, 'ABC' FROM TestView1");
        assertTrue(desc.isMaterialized());
        final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId(), desc.getRowDataStringConverter());
        final List<String> expectedResults = new ArrayList<>();
        expectedResults.add("[47, ABC]");
        expectedResults.add("[27, ABC]");
        expectedResults.add("[37, ABC]");
        expectedResults.add("[37, ABC]");
        expectedResults.add("[47, ABC]");
        expectedResults.add("[57, ABC]");
        TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
    } finally {
        executor.closeSession(sessionId);
    }
}
Also used : Executor(org.apache.flink.table.client.gateway.Executor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResultDescriptor(org.apache.flink.table.client.gateway.ResultDescriptor) URL(java.net.URL) Test(org.junit.Test)

Example 3 with Executor

use of org.apache.flink.table.client.gateway.Executor in project flink by apache.

the class CliClientITCase method runSqlStatements.

/**
 * Returns printed results for each ran SQL statements.
 *
 * @param statements the SQL statements to run
 * @return the printed results on SQL Client
 */
private List<Result> runSqlStatements(List<String> statements) throws IOException {
    final String sqlContent = String.join("", statements);
    DefaultContext defaultContext = new DefaultContext(Collections.emptyList(), new Configuration(MINI_CLUSTER_RESOURCE.getClientConfiguration()).set(ExecutionConfigOptions.TABLE_EXEC_LEGACY_CAST_BEHAVIOUR, ExecutionConfigOptions.LegacyCastBehaviour.DISABLED), Collections.singletonList(new DefaultCLI()));
    final Executor executor = new LocalExecutor(defaultContext);
    InputStream inputStream = new ByteArrayInputStream(sqlContent.getBytes());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
    String sessionId = executor.openSession("test-session");
    try (Terminal terminal = new DumbTerminal(inputStream, outputStream);
        CliClient client = new CliClient(() -> terminal, sessionId, executor, historyPath, HideSqlStatement.INSTANCE)) {
        client.executeInInteractiveMode();
        String output = new String(outputStream.toByteArray());
        return normalizeOutput(output);
    }
}
Also used : LocalExecutor(org.apache.flink.table.client.gateway.local.LocalExecutor) Executor(org.apache.flink.table.client.gateway.Executor) Configuration(org.apache.flink.configuration.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultCLI(org.apache.flink.client.cli.DefaultCLI) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultContext(org.apache.flink.table.client.gateway.context.DefaultContext) Terminal(org.jline.terminal.Terminal) DumbTerminal(org.jline.terminal.impl.DumbTerminal) DumbTerminal(org.jline.terminal.impl.DumbTerminal) LocalExecutor(org.apache.flink.table.client.gateway.local.LocalExecutor)

Example 4 with Executor

use of org.apache.flink.table.client.gateway.Executor in project flink by apache.

the class LocalExecutorITCase method testBatchQueryExecutionMultipleTimes.

@Test(timeout = 90_000L)
public void testBatchQueryExecutionMultipleTimes() throws Exception {
    final URL url = getClass().getClassLoader().getResource("test-data.csv");
    Objects.requireNonNull(url);
    final Map<String, String> replaceVars = new HashMap<>();
    replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
    final Map<String, String> configMap = new HashMap<>();
    configMap.put(EXECUTION_RESULT_MODE.key(), ResultMode.TABLE.name());
    configMap.put(RUNTIME_MODE.key(), RuntimeExecutionMode.BATCH.name());
    final Executor executor = createLocalExecutor(Collections.singletonList(udfDependency), Configuration.fromMap(configMap));
    String sessionId = executor.openSession("test-session");
    assertEquals("test-session", sessionId);
    initSession(executor, sessionId, replaceVars);
    final List<String> expectedResults = new ArrayList<>();
    expectedResults.add("[47]");
    expectedResults.add("[27]");
    expectedResults.add("[37]");
    expectedResults.add("[37]");
    expectedResults.add("[47]");
    expectedResults.add("[57]");
    try {
        for (int i = 0; i < 3; i++) {
            final ResultDescriptor desc = executeQuery(executor, sessionId, "SELECT * FROM TestView1");
            assertTrue(desc.isMaterialized());
            final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId(), desc.getRowDataStringConverter());
            TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
        }
    } finally {
        executor.closeSession(sessionId);
    }
}
Also used : Executor(org.apache.flink.table.client.gateway.Executor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResultDescriptor(org.apache.flink.table.client.gateway.ResultDescriptor) URL(java.net.URL) Test(org.junit.Test)

Example 5 with Executor

use of org.apache.flink.table.client.gateway.Executor in project flink by apache.

the class SqlClient method start.

private void start() {
    if (isEmbedded) {
        // create local executor with default environment
        DefaultContext defaultContext = LocalContextUtils.buildDefaultContext(options);
        final Executor executor = new LocalExecutor(defaultContext);
        executor.start();
        // Open an new session
        String sessionId = executor.openSession(options.getSessionId());
        try {
            // add shutdown hook
            Runtime.getRuntime().addShutdownHook(new EmbeddedShutdownThread(sessionId, executor));
            // do the actual work
            openCli(sessionId, executor);
        } finally {
            executor.closeSession(sessionId);
        }
    } else {
        throw new SqlClientException("Gateway mode is not supported yet.");
    }
}
Also used : Executor(org.apache.flink.table.client.gateway.Executor) LocalExecutor(org.apache.flink.table.client.gateway.local.LocalExecutor) DefaultContext(org.apache.flink.table.client.gateway.context.DefaultContext) LocalExecutor(org.apache.flink.table.client.gateway.local.LocalExecutor)

Aggregations

Executor (org.apache.flink.table.client.gateway.Executor)6 URL (java.net.URL)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Configuration (org.apache.flink.configuration.Configuration)2 ResultDescriptor (org.apache.flink.table.client.gateway.ResultDescriptor)2 DefaultContext (org.apache.flink.table.client.gateway.context.DefaultContext)2 LocalExecutor (org.apache.flink.table.client.gateway.local.LocalExecutor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 CommandLine (org.apache.commons.cli.CommandLine)1 Options (org.apache.commons.cli.Options)1 CliArgsException (org.apache.flink.client.cli.CliArgsException)1 CliFrontendParser (org.apache.flink.client.cli.CliFrontendParser)1 CustomCommandLine (org.apache.flink.client.cli.CustomCommandLine)1