Search in sources :

Example 6 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class StreamSQLTestProgram method main.

public static void main(String[] args) throws Exception {
    ParameterTool params = ParameterTool.fromArgs(args);
    String outputPath = params.getRequired("outputPath");
    final StreamExecutionEnvironment sEnv = StreamExecutionEnvironment.getExecutionEnvironment();
    sEnv.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, Time.of(10, TimeUnit.SECONDS)));
    sEnv.enableCheckpointing(4000);
    sEnv.getConfig().setAutoWatermarkInterval(1000);
    final StreamTableEnvironment tEnv = StreamTableEnvironment.create(sEnv);
    ((TableEnvironmentInternal) tEnv).registerTableSourceInternal("table1", new GeneratorTableSource(10, 100, 60, 0));
    ((TableEnvironmentInternal) tEnv).registerTableSourceInternal("table2", new GeneratorTableSource(5, 0.2f, 60, 5));
    int overWindowSizeSeconds = 1;
    int tumbleWindowSizeSeconds = 10;
    String overQuery = String.format("SELECT " + "  key, " + "  rowtime, " + "  COUNT(*) OVER (PARTITION BY key ORDER BY rowtime RANGE BETWEEN INTERVAL '%d' SECOND PRECEDING AND CURRENT ROW) AS cnt " + "FROM table1", overWindowSizeSeconds);
    String tumbleQuery = String.format("SELECT " + "  key, " + "  CASE SUM(cnt) / COUNT(*) WHEN 101 THEN 1 ELSE 99 END AS correct, " + "  TUMBLE_START(rowtime, INTERVAL '%d' SECOND) AS wStart, " + "  TUMBLE_ROWTIME(rowtime, INTERVAL '%d' SECOND) AS rowtime " + "FROM (%s) " + "WHERE rowtime > TIMESTAMP '1970-01-01 00:00:01' " + "GROUP BY key, TUMBLE(rowtime, INTERVAL '%d' SECOND)", tumbleWindowSizeSeconds, tumbleWindowSizeSeconds, overQuery, tumbleWindowSizeSeconds);
    String joinQuery = String.format("SELECT " + "  t1.key, " + "  t2.rowtime AS rowtime, " + "  t2.correct," + "  t2.wStart " + "FROM table2 t1, (%s) t2 " + "WHERE " + "  t1.key = t2.key AND " + "  t1.rowtime BETWEEN t2.rowtime AND t2.rowtime + INTERVAL '%d' SECOND", tumbleQuery, tumbleWindowSizeSeconds);
    String finalAgg = String.format("SELECT " + "  SUM(correct) AS correct, " + "  TUMBLE_START(rowtime, INTERVAL '20' SECOND) AS rowtime " + "FROM (%s) " + "GROUP BY TUMBLE(rowtime, INTERVAL '20' SECOND)", joinQuery);
    // get Table for SQL query
    Table result = tEnv.sqlQuery(finalAgg);
    // convert Table into append-only DataStream
    DataStream<Row> resultStream = tEnv.toAppendStream(result, Types.ROW(Types.INT, Types.SQL_TIMESTAMP));
    final StreamingFileSink<Row> sink = StreamingFileSink.forRowFormat(new Path(outputPath), (Encoder<Row>) (element, stream) -> {
        PrintStream out = new PrintStream(stream);
        out.println(element.toString());
    }).withBucketAssigner(new KeyBucketAssigner()).withRollingPolicy(OnCheckpointRollingPolicy.build()).build();
    resultStream.map(new KillMapper()).setParallelism(1).addSink(sink).setParallelism(1);
    sEnv.execute();
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) Path(org.apache.flink.core.fs.Path) PrintStream(java.io.PrintStream) Table(org.apache.flink.table.api.Table) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) Encoder(org.apache.flink.api.common.serialization.Encoder) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Row(org.apache.flink.types.Row)

Example 7 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class TpcdsTestProgram method main.

public static void main(String[] args) throws Exception {
    ParameterTool params = ParameterTool.fromArgs(args);
    String sourceTablePath = params.getRequired("sourceTablePath");
    String queryPath = params.getRequired("queryPath");
    String sinkTablePath = params.getRequired("sinkTablePath");
    Boolean useTableStats = params.getBoolean("useTableStats");
    TableEnvironment tableEnvironment = prepareTableEnv(sourceTablePath, useTableStats);
    // execute TPC-DS queries
    for (String queryId : TPCDS_QUERIES) {
        System.out.println("[INFO]Run TPC-DS query " + queryId + " ...");
        String queryName = QUERY_PREFIX + queryId + QUERY_SUFFIX;
        String queryFilePath = queryPath + FILE_SEPARATOR + queryName;
        String queryString = loadFile2String(queryFilePath);
        Table resultTable = tableEnvironment.sqlQuery(queryString);
        // register sink table
        String sinkTableName = QUERY_PREFIX + queryId + "_sinkTable";
        ((TableEnvironmentInternal) tableEnvironment).registerTableSinkInternal(sinkTableName, new CsvTableSink(sinkTablePath + FILE_SEPARATOR + queryId + RESULT_SUFFIX, COL_DELIMITER, 1, FileSystem.WriteMode.OVERWRITE, resultTable.getSchema().getFieldNames(), resultTable.getSchema().getFieldDataTypes()));
        TableResult tableResult = resultTable.executeInsert(sinkTableName);
        // wait job finish
        tableResult.getJobClient().get().getJobExecutionResult().get();
        System.out.println("[INFO]Run TPC-DS query " + queryId + " success.");
    }
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) ConnectorCatalogTable(org.apache.flink.table.catalog.ConnectorCatalogTable) Table(org.apache.flink.table.api.Table) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) TableResult(org.apache.flink.table.api.TableResult) CsvTableSink(org.apache.flink.table.sinks.CsvTableSink) TableEnvironment(org.apache.flink.table.api.TableEnvironment)

Example 8 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class BatchSQLTestProgram method main.

public static void main(String[] args) throws Exception {
    ParameterTool params = ParameterTool.fromArgs(args);
    String outputPath = params.getRequired("outputPath");
    String sqlStatement = params.getRequired("sqlStatement");
    TableEnvironment tEnv = TableEnvironment.create(EnvironmentSettings.inBatchMode());
    ((TableEnvironmentInternal) tEnv).registerTableSourceInternal("table1", new GeneratorTableSource(10, 100, 60, 0));
    ((TableEnvironmentInternal) tEnv).registerTableSourceInternal("table2", new GeneratorTableSource(5, 0.2f, 60, 5));
    ((TableEnvironmentInternal) tEnv).registerTableSinkInternal("sinkTable", new CsvTableSink(outputPath).configure(new String[] { "f0", "f1" }, new TypeInformation[] { Types.INT, Types.SQL_TIMESTAMP }));
    TableResult result = tEnv.executeSql(sqlStatement);
    // wait job finish
    result.getJobClient().get().getJobExecutionResult().get();
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) TableResult(org.apache.flink.table.api.TableResult) CsvTableSink(org.apache.flink.table.sinks.CsvTableSink) TableEnvironment(org.apache.flink.table.api.TableEnvironment) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation)

Example 9 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class CassandraConnectorITCase method testCassandraTableSink.

@Test
public void testCassandraTableSink() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(4);
    StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    DataStreamSource<Row> source = env.fromCollection(rowCollection);
    tEnv.createTemporaryView("testFlinkTable", source);
    ((TableEnvironmentInternal) tEnv).registerTableSinkInternal("cassandraTable", new CassandraAppendTableSink(builderForWriting, injectTableName(INSERT_DATA_QUERY)).configure(new String[] { "f0", "f1", "f2" }, new TypeInformation[] { Types.STRING, Types.INT, Types.INT }));
    tEnv.sqlQuery("select * from testFlinkTable").executeInsert("cassandraTable").await();
    ResultSet rs = session.execute(injectTableName(SELECT_DATA_QUERY));
    // validate that all input was correctly written to Cassandra
    List<Row> input = new ArrayList<>(rowCollection);
    List<com.datastax.driver.core.Row> output = rs.all();
    for (com.datastax.driver.core.Row o : output) {
        Row cmp = new Row(3);
        cmp.setField(0, o.getString(0));
        cmp.setField(1, o.getInt(2));
        cmp.setField(2, o.getInt(1));
        Assert.assertTrue("Row " + cmp + " was written to Cassandra but not in input.", input.remove(cmp));
    }
    Assert.assertTrue("The input data was not completely written to Cassandra", input.isEmpty());
}
Also used : ArrayList(java.util.ArrayList) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) ResultSet(com.datastax.driver.core.ResultSet) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamTableEnvironment(org.apache.flink.table.api.bridge.java.StreamTableEnvironment) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 10 with TableEnvironmentInternal

use of org.apache.flink.table.api.internal.TableEnvironmentInternal in project flink by apache.

the class LocalExecutor method executeOperation.

@Override
public TableResultInternal executeOperation(String sessionId, Operation operation) throws SqlExecutionException {
    final ExecutionContext context = getExecutionContext(sessionId);
    final TableEnvironmentInternal tEnv = (TableEnvironmentInternal) context.getTableEnvironment();
    try {
        return context.wrapClassLoader(() -> tEnv.executeInternal(operation));
    } catch (Exception e) {
        throw new SqlExecutionException(MESSAGE_SQL_EXECUTION_ERROR, e);
    }
}
Also used : SqlExecutionException(org.apache.flink.table.client.gateway.SqlExecutionException) ExecutionContext(org.apache.flink.table.client.gateway.context.ExecutionContext) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) SqlExecutionException(org.apache.flink.table.client.gateway.SqlExecutionException)

Aggregations

TableEnvironmentInternal (org.apache.flink.table.api.internal.TableEnvironmentInternal)18 Parser (org.apache.flink.table.delegation.Parser)7 Test (org.junit.Test)5 TableEnvironment (org.apache.flink.table.api.TableEnvironment)4 CatalogTable (org.apache.flink.table.catalog.CatalogTable)4 ExecutionContext (org.apache.flink.table.client.gateway.context.ExecutionContext)4 IOException (java.io.IOException)3 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)3 Table (org.apache.flink.table.api.Table)3 TableException (org.apache.flink.table.api.TableException)3 TableResult (org.apache.flink.table.api.TableResult)3 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)3 HiveParser (org.apache.flink.table.planner.delegation.hive.HiveParser)3 Row (org.apache.flink.types.Row)3 ArrayList (java.util.ArrayList)2 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 StreamTableEnvironment (org.apache.flink.table.api.bridge.java.StreamTableEnvironment)2 SqlExecutionException (org.apache.flink.table.client.gateway.SqlExecutionException)2 CsvTableSink (org.apache.flink.table.sinks.CsvTableSink)2