DataGen Connector
Overview
The DataGen connector is a built-in source connector of Flink. It generates random or sequential test data based on rules and does not depend on any external data source.
| Category | Details |
|---|---|
| Supported type | Source table |
| Running mode | Batch mode and stream mode |
| Data format | Not applicable |
| API type | SQL |
| Whether result table can be updated or deleted | N/A |
Application Scenarios
- Function verification: Quickly verifies the logic of Flink SQL jobs when no external data source is available.
- Performance stress testing: Simulates high-throughput scenarios by controlling the data generation rate and field size.
- Development and debugging: Works with the Print or BlackHole connector to build an end-to-end debugging link with no dependency.
Notes and Constraints
- If both number-of-rows and the start or end of the sequence generator are set, the generation stops when either of the conditions is met.
Syntax Structure
CREATE TABLE {table_name} (
{column_name} {column_type},
...
WATERMARK FOR {time_column} AS {watermark_expression}
) WITH (
'connector' = 'datagen',
'rows-per-second' = '{Generation rate}',
'number-of-rows' = '{Total number of rows}',
'fields.{field_name}.kind' = 'random / sequence',
'fields.{field_name}.min' = '{Minimum value}',
'fields.{field_name}.max' = '{Maximum value}',
'fields.{field_name}.length' = '{String/Set length}',
'fields.{field_name}.start' = '{Start value}',
'fields.{field_name}.end' = '{End value}'
...
);
{field_name} in the parameter must be replaced with the actual field name defined in the DDL. For example, if the field name is user_id, the parameter is written as fields.user_id.kind.
WITH Parameters
Common parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| connector | Yes | String | None | datagen | Specifies the connector type as DataGen. The value must be fixed at datagen. This is the unique identifier for Flink SQL to identify the connector implementation. An incorrect value will cause the table registration to fail. |
| rows-per-second | No | Long | 10000 | Positive integer | Controls the number of rows of data generated per second. This parameter takes effect only for the source. During Flink runtime, each parallel subtask independently sends data downstream at this rate. The actual total throughput is calculated as follows: Total throughput = This value × Parallelism. |
| number-of-rows | No | Long | None (unbounded stream) | Positive integer | Controls the total number of rows of data generated. After this parameter is set, the source automatically stops when the number of rows reaches the specified value, and the stream becomes a bounded stream. This parameter is independent of the end value of the sequence generator. The generation stops when either condition is met first. If this parameter is not set, the source continuously generates data and will not automatically stop. |
Generation policy parameters
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| fields.{field_name}.kind | No | String | random | random/sequence | Specifies the data generation policy for a specified field.
If not explicitly set, all fields use the random generator by default. Different fields in the same table can have different generation policies specified. |
Random generator parameters (kind = random)
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| fields.{field_name}.min | No | Consistent with the field type | See the default value table of types. | Numeric | Specifies the minimum value generated randomly. This parameter takes effect only for numeric types (TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, and DECIMAL) and does not take effect for STRING and BOOLEAN types. It is used as the lower bound when Flink generates random values. |
| fields.{field_name}.max | No | Consistent with the field type | See the default value table of types. | Numeric | Specifies the maximum value generated randomly. This parameter takes effect only for numeric types. The value of min must be less than that of max. Otherwise, the generated behavior is undefined. This parameter is used together with min to determine the value range of random values. |
| fields.{field_name}.length | No | Integer | 100 | Positive integer | Controls the length (number of characters) of a randomly generated string or the number of elements in a collection. This parameter is used to control the string or byte length for the STRING, VARCHAR, CHAR, BYTES, BINARY, and VARBINARY types, and to control the number of collection elements for the ARRAY, MAP, and MULTISET types. A larger value indicates that more memory is occupied by a single record. If the value is too large, the throughput and GC performance will be affected. |
| fields.{field_name}.max-past | No | Consistent with the field type | 0 | Valid duration, for example, 30 seconds or 5 minutes. | Generates a random past time that is no more than N away from the current time for the TIMESTAMP type. Only the TIMESTAMP type is supported. |
| fields.{field_name}.null-rate | No | Float | None | Greater than or equal to 0 and less than or equal to 1 | Controls the probability that this column is generated as null. |
| fields.{field_name}.var-len | No | BOOLEAN | false | true/false | Controls whether the length of VARCHAR, STRING, VARBINARY, or BYTES generated is variable. |
Default range of each type of random generator:
| Data Type | Default Minimum Value | Default Maximum Value | Configurable Parameter |
|---|---|---|---|
| BOOLEAN | - | - | - |
| TINYINT | -128 | 127 | min, max |
| SMALLINT | -32768 | 32767 | min, max |
| INT | -2147483648 | 2147483647 | min, max |
| BIGINT | -9223372036854775808 | 9223372036854775807 | min, max |
| FLOAT | Minimum positive value | Maximum positive value | min, max |
| DOUBLE | Minimum positive value | Maximum positive value | min, max |
| DECIMAL(p, s) | Determined by the precision | Determined by the precision | min, max |
| STRING / VARCHAR / CHAR | - | - | length |
| BYTES / BINARY / VARBINARY | - | - | length |
| DATE | 1970-01-01 | Current date | min, max |
| TIME | 00:00:00 | 23:59:59 | min, max |
| TIMESTAMP | 1970-01-01 00:00:00.000 | Current timestamp | min, max |
| TIMESTAMP_LTZ | 1970-01-01 00:00:00.000 | Current timestamp | min, max |
| ARRAY<T> | - | - | length (number of elements) |
| MAP<K, V> | - | - | length (number of key-value pairs) |
| MULTISET<T> | - | - | length (number of elements) |
| ROW<...> | - | - | Configured independently for each subfield |
Sequence generator parameters (kind = sequence)
| Parameter | Mandatory | Type | Default Value | Value Range | Description |
|---|---|---|---|---|---|
| fields.{field_name}.start | Yes | Consistent with the field type | None | Value range of the numeric type | Specifies the start value of the sequence. This parameter is required only when the sequence generator is used. Flink generates data starting from this value and increments it by 1 each time. |
| fields.{field_name}.end | Yes | Consistent with the field type | None | Value range of the numeric type | Specifies the end value of the sequence. This parameter is required only when the sequence generator is used. When the sequence reaches this value, the source stops sending data and becomes a bounded stream. In the case of multiple parallelism, the sequence range is evenly allocated to each subtask. |
- Data types supported by the sequence generator: TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL, CHAR, VARCHAR, and STRING.
- CHAR, VARCHAR, and STRING are used to convert integers into numeric strings of the STRING type.
- The sequence generator does not support data types such as BOOLEAN, DATE, and TIMESTAMP.
- The sequence generator always becomes a bounded stream after reaching the end value.
Usage Example
- Example 1: Quickly create a test table. All fields use the default random generator, and no external dependency is required.
-- Create a DataGen source table. CREATE TABLE datagen_source ( id BIGINT, data STRING, ts TIMESTAMP(3), WATERMARK FOR ts AS ts - INTERVAL '5' SECOND ) WITH ( 'connector' = 'datagen' ); -- Create the Print result table. CREATE TABLE print_sink ( id BIGINT, data STRING, ts TIMESTAMP(3) ) WITH ( 'connector' = 'print' ); -- Submit the job. INSERT INTO print_sink SELECT id, data, ts FROM datagen_source;
- Example 2: Simulate user behavior data. The ID field is incremented by sequence, and the generation range of other fields is controlled.
-- Create a DataGen source table. CREATE TABLE user_behavior_source ( user_id INT, user_name STRING, score DOUBLE, age INT, event_time TIMESTAMP(3), WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '500', 'fields.user_id.kind' = 'sequence', 'fields.user_id.start' = '1', 'fields.user_id.end' = '1000', 'fields.user_name.length' = '10', 'fields.score.min' = '0.0', 'fields.score.max' = '100.0', 'fields.age.min' = '18', 'fields.age.max' = '65' ); -- Create the Print result table. CREATE TABLE print_sink ( user_id INT, user_name STRING, score DOUBLE, age INT, event_time TIMESTAMP(3) ) WITH ( 'connector' = 'print', 'print-identifier' = 'user-behavior' ); -- Submit the job. INSERT INTO print_sink SELECT user_id, user_name, score, age, event_time FROM user_behavior_source;
- Example 3: Generate test data with a fixed number of rows. After the data is generated, the source automatically stops. This example is applicable to batch processing tests.
-- Create a DataGen source table (bounded stream). CREATE TABLE datagen_bounded_source ( id INT, data STRING ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '100', 'number-of-rows' = '500', 'fields.id.kind' = 'sequence', 'fields.id.start' = '1', 'fields.id.end' = '500' ); -- Create the Print result table. CREATE TABLE print_sink ( id INT, data STRING ) WITH ( 'connector' = 'print', 'print-identifier' = 'bounded-output' ); -- Submit the job. INSERT INTO print_sink SELECT id, data FROM datagen_bounded_source;
- Example 4: Generate test data that contains composite type fields such as ARRAY, MAP, and ROW.
-- Create a DataGen source table (including composite type fields). CREATE TABLE datagen_complex_source ( id INT, tags ARRAY<STRING>, properties MAP<STRING, STRING>, address ROW<city STRING, street STRING>, payload BYTES ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '100', 'fields.id.kind' = 'sequence', 'fields.id.start' = '1', 'fields.id.end' = '10000', 'fields.tags.length' = '3', 'fields.properties.length' = '5', 'fields.payload.length' = '50' ); -- Create the Print result table. CREATE TABLE print_sink ( id INT, tags ARRAY<STRING>, properties MAP<STRING, STRING>, address ROW<city STRING, street STRING>, payload BYTES ) WITH ( 'connector' = 'print', 'print-identifier' = 'complex-data' ); -- Submit the job. INSERT INTO print_sink SELECT id, tags, properties, address, payload FROM datagen_complex_source;
- Example 5: Simulate a high-load scenario and control the data volume and rate to perform performance stress testing.
-- Create a DataGen source table (high-throughput stress test configuration). CREATE TABLE datagen_pressure_source ( id BIGINT, payload STRING, event_time TIMESTAMP(3), WATERMARK FOR event_time AS event_time - INTERVAL '1' SECOND ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '100000', 'number-of-rows' = '10000000', 'fields.id.kind' = 'sequence', 'fields.id.start' = '1', 'fields.id.end' = '10000000', 'fields.payload.length' = '1024' ); -- Create the BlackHole result table. CREATE TABLE blackhole_sink ( id BIGINT, payload STRING, event_time TIMESTAMP(3) ) WITH ( 'connector' = 'blackhole' ); -- Submit the job to test the pure processing throughput. INSERT INTO blackhole_sink SELECT id, payload, event_time FROM datagen_pressure_source;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot