Variable management

更新时间:
复制 MD 格式

Using variables helps you avoid the security risks associated with plaintext credentials, such as an AccessKey or a password. Reusing variables also avoids repetitive code and values, simplifying configuration management. Variables are supported in SQL jobs, JAR/Python deployments, logging configurations, and UI settings. This topic describes how to create variables and use them in different scenarios.

Background information

A variable consists of a name and a value. The name, which you define, serves as a placeholder for the actual data and typically reflects its meaning. The value is the actual data that the name represents. Flink supports two types of variables: deployment variables and namespace variables. The differences are as follows:

  • Deployment variable: Can be used only within a single deployment, such as in an ETL SQL job or as a main function argument in a JAR/Python deployment. The format for referencing a deployment variable is ${variable_name}. For more information, see Deployment variables.

  • Namespace variable: Can be used across an entire namespace. This includes use in Development (for data ingestion, ETL, and query scripts), as main function arguments in JAR/Python deployments, in deployment parameter configurations, logging configurations, and UI settings (such as creating a Catalog). The format for referencing a namespace variable is ${secret_values.variable_name}. To learn how to create and use them, see Namespace variables.

Usage notes

  • You cannot configure multiple values for a single variable name.

  • Avoid using keywords as variable names.

  • In SQL jobs, you can only use variables in a temporary table (CREATE TEMPORARY TABLE). Using a variable in a persistent table (CREATE TABLE) causes the table to malfunction.

Deployment variables

Note

Deployment variable names must be unique within a single deployment.

ETL (SQL job)

You can use variables during SQL job development, including as arguments for custom functions. You can also modify the variable values when you start the job.

  1. In the left-side navigation pane of the Realtime Compute for Apache Flink console, click Development > ETL.

  2. In the SQL editor for your target SQL job, create variables using the ${variable_name} format, as shown in the following example.

    • When used as a parameter in a WITH clause, enclose the variable in single quotation marks: '${variable_name}'.

    • When used in other locations (such as for SQL table or field names), enclose the variable in backticks: `${variable_name}`.

    create temporary table `${source_name}`(
        id varchar,
        name varchar
    ) with (
        'connector' = 'datagen'
    );
    create temporary table blackhole(
        id varchar,
        `${test_name}` varchar
    ) with (
        'connector' = '${blackhole}'
    );
    insert into blackhole select * from `${source_name}`;
  3. Flink automatically identifies deployment variables that use this format in the SQL job. You must provide values for these variables.

    • Method 1: The Job Variables panel appears in the SQL editor. Enter the values in this panel.

      In the Job Variables panel, configure the key-value pairs: source_name=datagen, test_name=name, and blackhole=blackhole.

    • Method 2: Click More Configurations on the right side. Enter the values in the Job Variables section.

      For example, configure the variable source_name as datagen, test_name as name, and blackhole as blackhole. You can then reference these values in your SQL code by using the ${variable_name} syntax.

  4. After the job is deployed, you can view its variables on the Deployment Details tab of the job details page under Deployments.

    In the job properties, the Job Variables row displays the configured variables (such as source_name, test_name, and blackhole) and their corresponding values.

  5. Click Start in the Actions column for the target job. You can modify the variable values in the job startup panel.

    Important

    Changes made to variable values in the job startup panel apply only to the current job run. They do not alter the values defined in the SQL job.

    In the job startup panel, you can select stateless start or stateful start mode. If you select stateful start, you can configure recovery options, such as Recover from latest state. The Job Variables section displays the variables and their default values, such as source_name set to datagen, test_name to name, and blackhole to blackhole. You can modify them as needed and then click Start.

JAR/Python deployment

You can use variables as arguments for the main function in JAR/Python deployments.

Method 1: Use namespace variables

  1. Create a new variable. For details, see Create a variable.

  2. In the Realtime Compute for Apache Flink console, navigate to O&M > Deployments and click Deploy Job.

  3. In the Entry Point Main Arguments field, enter the argument using the format --variable_in_code ${secret_values.variable_in_management}.

    If a variable name needs to include special characters, see How do I pass special characters as parameters when I use Entry Point Main Arguments?.

    For example, enter --akid ${secret_values.test1} and --aksecret ${secret_values.test2}, with each parameter on a new line.

    For details on other deployment parameters, see Deploy a job.

  4. Click Deploy.

  5. Click Start in the Actions column for the target deployment.

Method 2: Use deployment variables

  1. In the Realtime Compute for Apache Flink console, navigate to O&M > Deployments and click Deploy Job.

  2. In the Entry Point Main Arguments field, enter the argument using the format --variable_in_code ${deployment_variable_name}, and then provide the variable value in the Job Variables section below.

    If a variable name needs to include special characters, see How do I pass special characters as parameters when I use Entry Point Main Arguments?.

    For example, enter --akid ${test1} --aksecret ${test2}, then add the test1 (AccessKey ID) and test2 (AccessKey Secret) variables and enter the actual credentials as their default values.

    For details on other deployment parameters, see Deploy a job.

  3. Click Deploy.

  4. Click Start in the Actions column for the target deployment. You can modify the variable values in the job startup panel.

    Select a start mode: stateless start (without an initial state) or stateful start (from an existing state). You can also enable Configuration Autotuning.

Namespace variables

Create a variable

Note

Only members with editor or higher permissions, or members with the specific permission to create variables, can create variables.

  1. Log on to the Realtime Compute for Apache Flink console. Find the target workspace and click Console in the Actions column.

  2. In the left-side navigation pane, click Security > Variables, and click Add Variable.

    Parameter

    Description

    Variable Name

    The variable name must be unique within the namespace and cannot be changed after creation.

    Variable Value

    Variable values are case-sensitive. The value can be modified after creation. For details, see Edit or delete a variable.

    Type (Plaintext/Ciphertext)

    The type cannot be changed after creation.

    • Plaintext: The variable value is directly visible on the Variables page.

    • Ciphertext: The variable value is hidden.

    After the variable is created, you can view its name, type, value, and creation time in the list on the Variables page. You can also Edit or Delete the variable.

  3. Click OK.

Use a variable

After creating a variable, you can reference it using the format ${secret_values.variable_name}. Replace "variable_name" with the name of your variable.

Development

You can use namespace variables in the Development section of the Realtime Compute for Apache Flink console.

ETL (SQL)

You can use variables in the DDL of an SQL job by referencing them with the format ${secret_values.variable_name}, as shown in the following example.

  • When used as a parameter in a WITH clause, enclose the variable in single quotation marks: '${secret_values.variable_name}'.

  • When used in other locations (such as for SQL table or field names), enclose the variable in backticks: `${secret_values.variable_name}`.

create temporary table `${secret_values.source_name}`(
    id varchar,
    name varchar
) with (
    'connector' = 'datagen'
);
create temporary table blackhole(
    id varchar,
    `${secret_values.test_name}` varchar
) with (
    'connector' = '${secret_values.blackhole}'
);
insert into blackhole select * from `${secret_values.source_name}`;

Data ingestion

source:
  type: mysql
  name: Mysql Source
  hostname: localhost
  port: 3306
  username: test
  password: ${secret_values.mysqlpw}
  tables: app_db.\.*
  server-id: 5400-5404
···

Data query

USE CATALOG paimon;
CREATE DATABASE IF NOT EXISTS `${secret_values.test_name}`;
CREATE TABLE paimon.`${secret_values.test_name}`.ods_user_log
(
    item_id  int NOT NULL,
    `${secret_values.user}` varchar(50) NOT NULL,
    action  varchar(20)  NOT NULL,
    vtime   timestamp,
    ds      varchar(10) NOT NULL
)
PARTITIONED BY (ds);
SELECT * from paimon.`${secret_values.test_name}`.ods_user_log LIMIT 10;

JAR/Python deployment

You can use variables as arguments for the main function in JAR/Python deployments.

  1. In the development console, navigate to O&M > Deployments and click Deploy Job.

  2. In the Entry Point Main Arguments field, enter the argument using the following format.

    Example: --input ${secret_values.input}, where ${secret_values.variable_name} is the syntax for referencing a variable.

    For details on other deployment parameters, see Deploy a job.

  3. Click Deploy.

Runtime parameter configuration

You can use variables in job runtime parameters.

  1. Navigate to the O&M > Deployments page and click the target job.

  2. On the Deployment Details tab, click Edit in the upper-right corner of the Runtime Parameter Configuration section and configure the parameters. You can use variables as parameter values, as shown in the following example.

    In the Other Configurations text area, use the ${secret_values.xxx} format to reference variables. For example, to configure Jindo OSS access credentials, you can set parameters such as fs.oss.jindo.buckets, fs.oss.jindo.accessKeyId, and fs.oss.jindo.accessKeySecret.

  3. Click Save.

Logging configuration

  1. Go to the logging configuration page.

  2. Use variables in the logging configuration.

    For example, when configuring job logs to be sent to SLS, you can use variables for 'accessKeyId' and 'accessKeySecret'. For complete information about log output configuration, see Configure job log output.

    <Appender name="SLS" type="SLS">
      <Layout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+8} %-5p %-60c %x - %m%n" type="PatternLayout" charset="UTF-8"/>  
      <!-- The final effective log path is: ${baseUri}/logs/${namespace}/${deploymentId}/{jobId}/ -->
      <Property name="namespace">{{ namespace }}</Property> <!-- Do not modify this line -->
      <Property name="project">YOUR-SLS-PROJECT</Property>  
      <Property name="logStore">YOUR-SLS-LOGSTORE</Property> 
      <Property name="endpoint">YOUR-SLS-ENDPOINT</Property> 
      <Property name="accessKeyId">${secret_values.accessKeyId}</Property> 
      <Property name="accessKeySecret">${secret_values.accessKeySecret}</Property> 
      <Property name="topic">{{ namespace }}:{{ deploymentId }}:{{ jobId }}</Property>
      <Property name="deploymentName">{{ deploymentName }}</Property>
      <Property name="flushIntervalSeconds">10</Property>
      <Property name="flushIntervalEventCount">100</Property>
    </Appender>

UI

When creating a Catalog through the UI, you can use variables in the configuration items.

  1. In the left-side navigation pane of the Realtime Compute for Apache Flink console, click Catalogs.

  2. Use variables on the Create Catalog page. The following example shows how to use a variable named mysqlpassword for the password parameter in a MySQL Catalog.

    In the password field, enter ${secret_values.mysqlpassword} to reference the secret value.

Edit or delete a variable

Note

Use caution when editing or deleting variables. This action can cause referencing deployments to fail or require you to redeploy online jobs. Running jobs are not affected.

  1. Log on to the Realtime Compute for Apache Flink console. Find the target workspace and click Console in the Actions column.

  2. In the left-side navigation pane, click Security > Variables. Find the target variable and click Edit or Delete in the Actions column.

    • Edit

      You can change the value of a variable. Only members with editor or higher permissions can edit variables.

    • Delete

      Only members with editor or higher permissions, or members with the specific permission to delete variables, can delete variables.

Related documents