section> Computing
  • Auto Scaling
  • Bare Metal Server
  • Dedicated Host
  • Elastic Cloud Server
  • FunctionGraph
  • Image Management Service
Network
  • Direct Connect
  • Domain Name Service
  • Elastic IP
  • Elastic Load Balancing
  • Enterprise Router
  • NAT Gateway
  • Private Link Access Service
  • Secure Mail Gateway
  • Virtual Private Cloud
  • Virtual Private Network
  • VPC Endpoint
Storage
  • Cloud Backup and Recovery
  • Cloud Server Backup Service
  • Elastic Volume Service
  • Object Storage Service
  • Scalable File Service
  • Storage Disaster Recovery Service
  • Volume Backup Service
Application
  • API Gateway (APIG)
  • Application Operations Management
  • Application Performance Management
  • Distributed Message Service (for Kafka)
  • Simple Message Notification
Data Analysis
  • Cloud Search Service
  • Data Lake Insight
  • Data Warehouse Service
  • DataArts Studio
  • MapReduce Service
  • ModelArts
  • Optical Character Recognition
Container
  • Application Service Mesh
  • Cloud Container Engine
  • Cloud Container Instance
  • Software Repository for Containers
Databases
  • Data Replication Service
  • Distributed Cache Service
  • Distributed Database Middleware
  • Document Database Service
  • GeminiDB
  • Relational Database Service
  • TaurusDB
Management & Deployment
  • Cloud Create
  • Cloud Eye
  • Cloud Trace Service
  • Config
  • Log Tank Service
  • Resource Formation Service
  • Tag Management Service
Security Services
  • Anti-DDoS
  • Cloud Firewall
  • Database Security Service
  • Dedicated Web Application Firewall
  • Host Security Service
  • Identity and Access Management
  • Key Management Service
  • Web Application Firewall
Other
  • Enterprise Dashboard
  • Marketplace
  • Price Calculator
  • Status Dashboard
APIs
  • REST API
  • API Usage Guidelines
  • Endpoints
Development and Automation
  • SDKs
  • Drivers and Tools
  • Terraform
  • Ansible
  • Cloud Create
Architecture Center
  • Best Practices
  • Blueprints
IaaSComputingAuto ScalingBare Metal ServerDedicated HostElastic Cloud ServerFunctionGraphImage Management ServiceNetworkDirect ConnectDomain Name ServiceElastic IPElastic Load BalancingEnterprise RouterNAT GatewayPrivate Link Access ServiceSecure Mail GatewayVirtual Private CloudVirtual Private NetworkVPC EndpointStorageCloud Backup and RecoveryCloud Server Backup ServiceElastic Volume ServiceObject Storage ServiceScalable File ServiceStorage Disaster Recovery ServiceVolume Backup ServicePaaSApplicationAPI Gateway (APIG)Application Operations ManagementApplication Performance ManagementDistributed Message Service (for Kafka)Simple Message NotificationData AnalysisCloud Search ServiceData Lake InsightData Warehouse ServiceDataArts StudioMapReduce ServiceModelArtsOptical Character RecognitionContainerApplication Service MeshCloud Container EngineCloud Container InstanceSoftware Repository for ContainersDatabasesData Replication ServiceDistributed Cache ServiceDistributed Database MiddlewareDocument Database ServiceGeminiDBRelational Database ServiceTaurusDBManagementManagement & DeploymentCloud CreateCloud EyeCloud Trace ServiceConfigLog Tank ServiceResource Formation ServiceTag Management ServiceSecuritySecurity ServicesAnti-DDoSCloud FirewallDatabase Security ServiceDedicated Web Application FirewallHost Security ServiceIdentity and Access ManagementKey Management ServiceWeb Application FirewallOtherOtherEnterprise DashboardMarketplacePrice CalculatorStatus Dashboard

MapReduce Service

  • Overview
    • What Is MRS?
    • Application Scenarios
    • Components
      • List of MRS Component Versions
      • Alluxio
      • CarbonData
      • ClickHouse
      • CDL
      • DBService
      • Apache Doris
      • Flink
        • Flink Basic Principles
        • Flink HA Solution
        • Relationship with Other Components
        • Flink Enhanced Open Source Features
      • Flume
      • Guardian
      • HBase
      • HDFS
      • HetuEngine
      • Hive
      • Hudi
      • Hue
      • Impala
      • IoTDB
      • JobGateway
      • Kafka
      • KafkaManager
      • KrbServer and LdapServer
      • Kudu
      • Loader
      • Manager
      • MapReduce
      • MemArtsCC
      • Oozie
      • OpenTSDB
      • Presto
      • Ranger
      • Spark
      • Spark2x
      • Storm
      • Tez
      • Yarn
      • ZooKeeper
    • Functions
    • Constraints
    • Related Services
  • Preparing a User
  • MRS Quick Start
  • Configuring a Cluster
  • Managing Clusters
  • Using an MRS Client
  • Configuring a Cluster with Storage and Compute Decoupled
  • Accessing Web Pages of Open Source Components Managed in MRS Clusters
  • Accessing Manager
  • MRS Manager Operation Guide (Applicable to 3.x)
  • MRS Manager Operation Guide (Applicable to 2.x and Earlier Versions)
  • Security Description
  • High-Risk Operations
  • Backup and Restoration
  • Data Backup and Restoration
  • Appendix
  • FAQ
  • Change History
  • User Guide
  • Overview
  • Components
  • Flink
  • Flink Enhanced Open Source Features
  • Flink CEP in SQL

Flink CEP in SQL¶

Flink CEP in SQL¶

Flink allows users to represent complex event processing (CEP) query results in SQL for pattern matching and evaluate event streams on Flink engines.

SQL Query Syntax¶

CEP SQL is implemented through the MATCH_RECOGNIZE SQL syntax. The MATCH_RECOGNIZE clause is supported by Oracle SQL since Oracle Database 12c and is used to indicate event pattern matching in SQL. Apache Calcite also supports the MATCH_RECOGNIZE clause.

Flink uses Calcite to analyze SQL query results. Therefore, this operation complies with the Apache Calcite syntax.

MATCH_RECOGNIZE (
      [ PARTITION BY expression [, expression ]* ]
      [ ORDER BY orderItem [, orderItem ]* ]
      [ MEASURES measureColumn [, measureColumn ]* ]
      [ ONE ROW PER MATCH | ALL ROWS PER MATCH ]
      [ AFTER MATCH
            ( SKIP TO NEXT ROW
            | SKIP PAST LAST ROW
            | SKIP TO FIRST variable
            | SKIP TO LAST variable
            | SKIP TO variable )
      ]
      PATTERN ( pattern )
      [ WITHIN intervalLiteral ]
      [ SUBSET subsetItem [, subsetItem ]* ]
      DEFINE variable AS condition [, variable AS condition ]*
      )

The syntax elements of the MATCH_RECOGNIZE clause are defined as follows:

(Optional) -PARTITION BY: defines partition columns. This clause is optional. If this parameter is not defined, the parallelism 1 is used.

(Optional) -ORDER BY: defines the sequence of events in a data flow. The ORDER BY clause is optional. If it is ignored, non-deterministic sorting is used. Since the order of events is important in pattern matching, this clause should be specified in most cases.

(Optional) -MEASURES: specifies the attribute value of the successfully matched event.

(Optional) -ONE ROW PER MATCH | ALL ROWS PER MATCH: defines how to output the result. ONE ROW PER MATCH indicates that only one row is output for each matching. ALL ROWS PER MATCH indicates that one row is output for each matching event.

(Optional) -AFTER MATCH: specifies the start position for processing after the next pattern is successfully matched.

-PATTERN: defines the matching pattern as a regular expression. The following operators can be used in the PATTERN clause: join operators, quantifier operators (*, +, ?, {n}, {n,}, {n,m}, and {,m}), branch operators (vertical bar |), and differential operators ('{- -}').

(Optional) -WITHIN: outputs a pattern clause match only when the match occurs within the specified time.

(Optional) -SUBSET: combines one or more associated variables defined in the DEFINE clause.

-DEFINE: specifies the Boolean condition, which defines the variables used in the PATTERN clause.

In addition, the MATCH_RECOGNIZE clause supports the following functions:

-MATCH_NUMBER(): Used in the MEASURES clause to allocate the same number to each row that is successfully matched.

-CLASSIFIER(): Used in the MEASURES clause to indicate the mapping between matched rows and variables.

-FIRST() and LAST(): Used in the MEASURES clause to return the value of the expression evaluated in the first or last row of the row set mapped to the schema variable.

-NEXT() and PREV(): Used in the DEFINE clause to evaluate an expression using the previous or next row in a partition.

-RUNNING and FINAL keywords: Used to determine the semantics required for aggregation. RUNNING can be used in the MEASURES and DEFINE clauses, whereas FINAL can be used only in the MEASURES clause.

  • Aggregate functions (COUNT, SUM, AVG, MAX, MIN): Used in the MEASURES and DEFINE clauses.

Query Example¶

The following query finds the V-shaped pattern in the stock price data flow.

SELECT *
    FROM MyTable
    MATCH_RECOGNIZE (
      ORDER BY rowtime
      MEASURES
        STRT.name as s_name,
        LAST(DOWN.name) as down_name,
        LAST(UP.name) as up_name
      ONE ROW PER MATCH
      PATTERN (STRT DOWN+ UP+)
      DEFINE
        DOWN AS DOWN.v < PREV(DOWN.v),
        UP AS UP.v > PREV(UP.v)
    )

In the following query, the aggregate function AVG is used in the MEASURES clause of SUBSET E consisting of variables related to A and C.

SELECT *
    FROM Ticker
    MATCH_RECOGNIZE (
      MEASURES
        AVG(E.price) AS avgPrice
      ONE ROW PER MATCH
      AFTER MATCH SKIP PAST LAST ROW
      PATTERN (A B+ C)
      SUBSET E = (A,C)
      DEFINE
        A AS A.price < 30,
        B AS B.price < 20,
        C AS C.price < 30
    )
  • Prev
  • Next
last updated: 2025-09-12 13:13 UTC - commit: 24eadb5c5fab74f43c2223ebac5503c93aeae657
Edit pageReport Documentation Bug
Page Contents
  • Flink CEP in SQL
    • Flink CEP in SQL
    • SQL Query Syntax
    • Query Example
© T-Systems International GmbH
  • Contact
  • Data privacy
  • Disclaimer of Liabilities
  • Imprint