Table & SQL Connectors

    This page describes how to register table sources and table sinks in Flink using the natively supported connectors. After a source or sink has been registered, it can be accessed by Table API & SQL statements.

    If you want to implement your own custom table source or sink, have a look at the user-defined sources & sinks page.

    Flink natively support various connectors. The following tables list all available connectors.

    Flink supports using SQL statements to register tables. One can define the table name, the table schema, and the table options for connecting to an external system.

    See the .

    The following code shows a full example of how to connect to Kafka for reading and writing JSON records.

    SQL

    The desired connection properties are converted into string-based key-value pairs. Factories will create configured table sources, table sinks, and corresponding formats from the key-value pairs based on factory identifiers (kafka and json in this example). All factories that can be found via Java’s are taken into account when searching for exactly one matching factory for each component.

    If no factory can be found or multiple factories match for the given properties, an exception will be thrown with additional information about considered factories and supported properties.

    Flink uses Java’s Service Provider Interfaces (SPI) to load the table connector/format factories by their identifiers. Since the SPI resource file named org.apache.flink.table.factories.Factory for every table connector/format is under the same directory META-INF/services, these resource files will override each other when build the uber-jar of the project which uses more than one table connector/format, which will cause Flink to fail to load table connector/format factories.

    1. <modelVersion>4.0.0</modelVersion>
    2. <groupId>org.example</groupId>
    3. <artifactId>myProject</artifactId>
    4. <version>1.0-SNAPSHOT</version>
    5. <dependencies>
    6. <!-- other project dependencies ...-->
    7. <dependency>
    8. <groupId>org.apache.flink</groupId>
    9. <artifactId>flink-sql-connector-hive-3.1.2_2.11</artifactId>
    10. <version>1.14.4</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>org.apache.flink</groupId>
    14. <version>1.14.4</version>
    15. </dependency>
    16. </dependencies>
    17. <build>
    18. <plugins>
    19. <plugin>
    20. <groupId>org.apache.maven.plugins</groupId>
    21. <artifactId>maven-shade-plugin</artifactId>
    22. <executions>
    23. <execution>
    24. <id>shade</id>
    25. <phase>package</phase>
    26. <goals>
    27. <goal>shade</goal>
    28. </goals>
    29. <configuration>
    30. <transformers combine.children="append">
    31. <!-- The service transformer is needed to merge META-INF/services files -->
    32. <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
    33. <!-- ... -->
    34. </transformers>
    35. </configuration>
    36. </execution>
    37. </executions>
    38. </plugins>
    39. </build>

    After configured the , the table connector/format resource files under the directory META-INF/services would be merged rather than overwritten each other when build the uber-jar of above project.

    The body clause of a SQL CREATE TABLE statement defines the names and types of physical columns, constraints and watermarks. Flink doesn’t hold the data, thus the schema definition only declares how to map physical columns from an external system to Flink’s representation. The mapping may not be mapped by names, it depends on the implementation of formats and connectors. For example, a MySQL database table is mapped by field names (not case sensitive), and a CSV filesystem is mapped by field order (field names can be arbitrary). This will be explained in every connector.

    The following example shows a simple schema without time attributes and one-to-one field mapping of input/output to table columns.

    SQL

    Some connectors and formats expose additional metadata fields that can be accessed in metadata columns next to the physical payload columns. See the for more information about metadata columns.

    Primary key constraints tell that a column or a set of columns of a table are unique and they do not contain nulls. Primary key uniquely identifies a row in a table.

    The primary key of a source table is a metadata information for optimization. The primary key of a sink table is usually used by the sink implementation for upserting.

    SQL standard specifies that a constraint can either be ENFORCED or NOT ENFORCED. This controls if the constraint checks are performed on the incoming/outgoing data. Flink does not own the data the only mode we want to support is the NOT ENFORCED mode. Its up to the user to ensure that the query enforces key integrity.

    SQL

    1. CREATE TABLE MyTable (
    2. MyField1 INT,
    3. MyField2 STRING,
    4. MyField3 BOOLEAN,
    5. PRIMARY KEY (MyField1, MyField2) NOT ENFORCED -- defines a primary key on columns
    6. ) WITH (
    7. ...
    8. )

    Time attributes are essential when working with unbounded streaming tables. Therefore both proctime and rowtime attributes can be defined as part of the schema.

    Proctime Attributes

    In order to declare a proctime attribute in the schema, you can use to declare a computed column which is generated from PROCTIME() builtin function. The computed column is a virtual column which is not stored in the physical data.

    SQL

    Rowtime Attributes

    In order to control the event-time behavior for tables, Flink provides predefined timestamp extractors and watermark strategies.

    Please refer to for more information about defining time attributes in DDL.

    The following timestamp extractors are supported:

    DDL

    1. -- use the existing TIMESTAMP(3) field in schema as the rowtime attribute
    2. CREATE TABLE MyTable (
    3. ts_field TIMESTAMP(3),
    4. WATERMARK FOR ts_field AS ...
    5. ) WITH (
    6. ...
    7. )
    8. -- use system functions or UDFs or expressions to extract the expected TIMESTAMP(3) rowtime field
    9. CREATE TABLE MyTable (
    10. log_ts STRING,
    11. ts_field AS TO_TIMESTAMP(log_ts),
    12. WATERMARK FOR ts_field AS ...
    13. ) WITH (
    14. ...

    The following watermark strategies are supported:

    DDL

    Make sure to always declare both timestamps and watermarks. Watermarks are required for triggering time-based operations.

    Please see the Data Types page about how to declare a type in SQL.