Hello, I'm trying to learn Flink SQL by following ...
# troubleshooting
k
Hello, I'm trying to learn Flink SQL by following the example with Flink 1.19. https://nightlies.apache.org/flink/flink-docs-release-1.19/docs/dev/table/sql/gettingstarted/. I am falling at the first hurdle.
I get the source table created okay (employee_information) and can query that
SELECT * from employee_information WHERE dept_id = 1;
and the continuous query
Copy code
SELECT
   dept_id,
   COUNT(*) as emp_count
FROM employee_information
GROUP BY dept_id;
. That all works as expected.
Copy code
Flink SQL>
>
> CREATE TABLE employee_information (
>     emp_id INT,
>     name VARCHAR,
>     dept_id INT
> ) WITH (
>     'connector' = 'filesystem',
>     'path' = '/tmp/employees.csv',
>     'format' = 'csv'
> );
✅ 1
Copy code
SELECT * from employee_information WHERE dept_id = 1;
✅ 1
Copy code
Flink SQL>
> INSERT INTO department_counts
> SELECT
>    dept_id,
>    COUNT(*) as emp_count
> FROM employee_information;
[ERROR] Could not execute SQL statement. Reason:
org.apache.calcite.sql.validate.SqlValidatorException: Expression 'dept_id' is not being grouped
so I'm presuming that's a typo in the example, and the group by clause is missing. so....
Copy code
Flink SQL> INSERT INTO department_counts
> SELECT
>    dept_id,
>    COUNT(*) as emp_count
> FROM employee_information
> GROUP BY dept_id;
[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.TableException: Cannot find table '`default_catalog`.`default_database`.`department_counts`' in any of the catalogs [default_catalog], nor as a temporary table.
I am supposed to create
department_counts
myself first? how?
As an experiment I tried:
Copy code
CREATE TABLE department_counts
> (dept_id INT, emp_count BIGINT NOT NULL)
> WITH ('connector' = 'filesystem',  'path' = '/tmp/department_counts.csv', 'format' = 'csv')
but that fails:
Copy code
[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.TableException: Table sink 'default_catalog.default_database.department_counts' doesn't support consuming update changes which is produced by node GroupAggregate(groupBy=[dept_id], select=[dept_id, COUNT(*) AS emp_count])
any guidance would be appreciated
r
Hmmm I don't know if that page is supposed to be run top to bottom - I read it as example statements for each feature
you're right about the missing
GROUP BY
instead of the
INSERT
(which as you say, won't work if you don't have the table) you could try
Copy code
CREATE TABLE department_counts AS 
SELECT 
   dept_id,
   COUNT(*) as emp_count 
FROM employee_information 
GROUP BY dept_id;
but you'd maybe need to include the
WITH
lemme try it
ah, yeah same error. @Martijn Visser shared the reason here: https://stackoverflow.com/a/77721820/350613
if you remove the aggregate, it works:
Copy code
Flink SQL> CREATE TABLE department_name with ('connector' = 'filesystem', 'path' = '/tmp/dept.csv','format' = 'csv') AS
> SELECT
>    dept_id,
>    name
> FROM employee_information ;
[INFO] Submitting SQL update statement to the cluster...
[INFO] SQL update statement has been successfully submitted to the cluster:
Job ID: b6349b836eed898004e8d8d5cfa3fe2c
Copy code
❯ cat /tmp/dept.csv/part-eb52363b-81c4-48d8-936c-e3c4c85f87e8-0-0
42,FOO
42,BAR
so my understanding is you can't use filesystem connector with csv as a sink if there's going to be updates (which there could be, with an aggregate)
k
ah, that does make sense. so if I use something like a kafka sink, I should be good? I'll give that a try
r
that would be my assumption, yes. There's a note in taht Stack Overflow answer that the
debezium
format would be valid for the filesystem connector too.
k
I'll give that a try too. I'll open a PR against the docs. I'm sure I won't be the last person to fall here
r
that'd be awesome. definitely a sharp edge!
k
thank you - indeed it works if I
Copy code
CREATE TABLE department_counts (
dept_id INT, emp_count BIGINT NOT NULL
) WITH (
     'connector' = 'filesystem',
     'path' = '/tmp/department_counts.csv',
     'format' = 'debezium-json'
);
then :
Copy code
INSERT INTO department_counts
 SELECT
    dept_id,
    COUNT(*) as emp_count
 FROM employee_information
 GROUP BY dept_id
✨ 1