dp api
06/14/2023, 5:52 AMdef log_processing():
env_settings = <http://EnvironmentSettings.in|EnvironmentSettings.in>_streaming_mode()
t_env = TableEnvironment.create(env_settings)
t_env.get_config().set("pipeline.jars", "file:///flink-sql-connector-kafka-1.17.1.jar")
t_env.get_config().set("table.exec.source.idle-timeout", "1000")
source_ddl = """
CREATE TABLE restuarant_live_pending_orders(
rest_id VARCHAR,
status VARCHAR
) WITH (
'connector' = 'kafka',
'topic' = 'live_order_status',
'properties.bootstrap.servers' = 'localhost:9092',
'properties.group.id' = 'rest_group',
'scan.startup.mode' = 'specific-offsets',
'scan.startup.specific-offsets' = 'partition:0,offset:0',
'json.fail-on-missing-field' = 'false',
'json.ignore-parse-errors' = 'true',
'format' = 'json'
)
"""
t_env.execute_sql(source_ddl)
tbl = t_env.from_path('restuarant_live_pending_orders')
tbl.print_schema()
orders_sum = t_env.sql_query ("SELECT rest_id, SUM(CASE WHEN status = 'NEW' THEN 1 ELSE -1 END) AS status_count FROM %s GROUP BY rest_id" % tbl).execute()
orders_sum.print_schema()Dian Fu
06/15/2023, 2:24 AMfile:///flink-sql-connector-kafka-1.17.1.jar really exists?dp api
06/15/2023, 7:49 AM{"rest_id": "6487f4c6fc13ae161d9008f6", "status": "NEW"}
So, the dynamic table is 100 x 2 table with changing COUNT of 'status' on 100 rest_ids. I am attempting to print this out on console first using Table API/SQL queries. Since, it a basic query I am sure I am using it correctly (like this ("SELECT rest_id, SUM(CASE WHEN status = 'NEW' THEN 1 ELSE -1 END) AS status_count FROM %s GROUP BY rest_id" % tbl)) However, the output on console is printed with Updates/Inserts (see below) i.e. COUNT of Status is not being grouped by rest_id (as it keeps getting repeated)
8> -U[6487f4c6fc13ae161d9008f7, 3]
8> +U[6487f4c6fc13ae161d9008f7, 4]
7> -U[6487f4c6fc13ae161d9008e3, 3]
8> -U[6487f4c6fc13ae161d9008f7, 4]
7> +U[6487f4c6fc13ae161d9008e3, 4]
I believe I am missing something fundamental here. Do we have to mention a special sink (like upsert-kafka or some other connector) to output the dynamic table of 100 x 2 instead of print it w/o sink? if yes, which one to use?Dian Fu
06/15/2023, 12:30 PMHowever, the output on console is printed with Updates/Inserts (see below)This is as expected.
-U means retracting previous output
+U means an updated row
If you don’t want to see this, you can write the results to an output storage which supports upsert, e.g. MySQL, etc.
COUNT of Status is not being grouped by rest_id (as it keeps getting repeated)Actually the results are grouped, you can see that the count is increasing. There is no repeating. It’s retraction message. I guess you could refer to the following documentation to understand some fundamental concepts: https://nightlies.apache.org/flink/flink-docs-release-1.17/docs/dev/table/concepts/versioned_tables/ https://nightlies.apache.org/flink/flink-docs-release-1.17/docs/dev/table/concepts/dynamic_tables/ https://nightlies.apache.org/flink/flink-docs-release-1.17/docs/dev/python/table/intro_to_table_api/#write-sql-queries
Dian Fu
06/15/2023, 12:31 PMdp api
06/16/2023, 7:04 AMdef log_processing():
env_settings = EnvironmentSettings.in_streaming_mode()
t_env = TableEnvironment.create(env_settings)
t_env.get_config().set("pipeline.jars", "file:///Users/Raghav/Desktop/prototype-v0.0.1/flink-sql-connector-kafka-1.17.1.jar", "file:///Users/Raghav/Desktop/prototype-v0.0.1/mysql-connector-j-8.0.32.jar")
t_env.get_config().set("table.exec.source.idle-timeout", "1000")
source_ddl = """
CREATE TABLE restuarant_live_pending_orders(
rest_id VARCHAR,
status VARCHAR
) WITH (
'connector' = 'kafka',
'topic' = 'live_order_status',
'properties.bootstrap.servers' = 'localhost:9092',
'properties.group.id' = 'rest_group',
'scan.startup.mode' = 'specific-offsets',
'scan.startup.specific-offsets' = 'partition:0,offset:0',
'json.fail-on-missing-field' = 'false',
'json.ignore-parse-errors' = 'true',
'format' = 'json'
)
"""
t_env.execute_sql(source_ddl)
query = """
INSERT INTO pending_orders_table
SELECT rest_id, SUM(CASE WHEN status = 'NEW' THEN 1 WHEN status = 'PROCESSED' THEN -1 ELSE 0 END) AS pending_count
FROM restuarant_live_pending_orders
GROUP BY rest_id
"""
sink_mysql = """
CREATE TABLE pending_orders_table (
rest_id VARCHAR,
pending_count INT,
PRIMARY KEY (rest_id) NOT ENFORCED
) WITH (
'connector' = 'jdbc',
'url' = 'jdbc:<mysql://0.0.0.0:3306/flink>',
'table-name' = 'pending_orders_table',
'username' = <username>,
'password' = <password>
)
"""
t_env.execute_sql(sink_mysql)
t_env.execute_sql(query)
if __name__ == '__main__':
log_processing()
Thanks a ton for all your help!!Dian Fu
06/16/2023, 7:05 AMdp api
06/16/2023, 7:28 AMDian Fu
06/16/2023, 7:28 AMdp api
06/16/2023, 7:57 AMfrom pyflink.table import EnvironmentSettings, TableEnvironment
from pyflink.table.expressions import *
from pyflink.table.table import Table
def log_processing():
env_settings = EnvironmentSettings.in_streaming_mode()
t_env = TableEnvironment.create(env_settings)
t_env.get_config().set("pipeline.jars", "file:///Users/Raghav/Desktop/prototype-v0.0.1/flink-sql-connector-kafka-1.17.1.jar;file:///Users/Raghav/Desktop/prototype-v0.0.1/mysql-connector-j-8.0.32.jar;file:///Users/Raghav/Desktop/prototype-v0.0.1/flink-connector-jdbc-3.1.0-1.17.jar")
t_env.get_config().set("table.exec.source.idle-timeout", "1000")
source_ddl = """
CREATE TABLE restuarant_live_pending_orders(
rest_id VARCHAR,
status VARCHAR
) WITH (
'connector' = 'kafka',
'topic' = 'live_order_status',
'properties.bootstrap.servers' = 'localhost:9092',
'properties.group.id' = 'rest_group',
'scan.startup.mode' = 'specific-offsets',
'scan.startup.specific-offsets' = 'partition:0,offset:0',
'json.fail-on-missing-field' = 'false',
'json.ignore-parse-errors' = 'true',
'format' = 'json'
)
"""
t_env.execute_sql(source_ddl)
sink_mysql = """
CREATE TABLE pending_orders_table (
rest_id VARCHAR,
pending_count INT,
PRIMARY KEY (rest_id) NOT ENFORCED
) WITH (
'connector' = 'jdbc',
'url' = 'jdbc:<mysql://localhost:3306/flink>',
'table-name' = 'pending_orders_table',
'username' = <USERNAME>,
'password' = <PASSWORD>,
'driver' = 'com.mysql.jdbc.Driver'
)
"""
t_env.execute_sql(sink_mysql)
query = """
INSERT INTO pending_orders_table
SELECT rest_id, SUM(CASE WHEN status = 'NEW' THEN 1 WHEN status = 'PROCESSED' THEN -1 ELSE 0 END) AS pending_count
FROM restuarant_live_pending_orders
GROUP BY rest_id
"""
t_env.execute_sql(query).wait()
if __name__ == '__main__':
log_processing()
This is the code and now its giving no errors.
All configs seem to be fine and I am able to connect to the mysql localhost server from a separate sql editor.
However the flink sink query is not creating any table in mysql server under the database schema 'flink'.
Since I am using .wait(), the code runs without giving any logs or any errors.
Can you provide any suggestions on how to make this run ?Dian Fu
06/16/2023, 8:12 AMHowever the flink sink query is not creating any table in mysql server under the database schema ‘flink’.This is by design. You need to create the table at mysql side yourself.
dp api
06/16/2023, 8:35 AMDian Fu
06/16/2023, 8:45 AMpipeline.operator-chaining: false to disable the operator chain. In this way, you could see how many records are each operator received / sent from Flink Web UI.dp api
06/16/2023, 12:03 PMdp api
06/16/2023, 2:36 PMDian Fu
06/17/2023, 1:05 AM