KSQL-插入流不会产生数据

时间:2018-11-09 13:38:07

标签: apache-kafka ksql

我在从使用INSERT INTO KSQL操作填充的流中读取消息时遇到问题。

我遵循的步骤是:

我有一个从kafka主题创建的流event_stream

CREATE STREAM event_stream (eventType varchar, eventTime varchar, 
sourceHostName varchar) WITH (kafka_topic='events', value_format='json');

SELECT * FROM event_stream;显示正确输入的消息。

我想将其中一些消息发送到我已经创建的kafka的另一个主题output_events中。

然后我在KSQL中创建第二个流:

CREATE STREAM output_stream (eventTime varchar, extraColumn varchar, 
sourceHostName varchar) WITH (kafka_topic='output_events', value_format='json');

最后,我使用以下命令将输入​​链接到输出:

INSERT INTO output_stream SELECT eventTime, 'Extra info' as extraColumn,    
sourceHostName FROM event_stream WHERE eventType = 'MatchingValue';

以上所有内容似乎都完整无误,但是如果我运行 SELECT * FROM output_stream;我没有数据。为什么会这样?

运行上述查询的SELECT部分​​工作正常,因此我可以看到与此主题匹配的结果。

奇怪的是,如果我运行DESCRIBE EXTENDED output_stream,消息计数的确表明消息正在到达流中。

Local runtime statistics                                                                        
------------------------                                                                        
messages-per-sec:      0.33   total-messages:        86     last-message: 11/9/18 1:15:43 PM UTC
 failed-messages:         0 failed-messages-per-sec:         0      last-failed:       n/a      
(Statistics of the local KSQL server interaction with the Kafka topic output_events) 

我还检查了ksql-server日志,但是在那里看不到任何错误。

1 个答案:

答案 0 :(得分:1)

这是a bug,是由于无意中误用了CREATE STREAM的错误语法。您正在使用该变体针对现有主题“注册” KSQL流。为了使INSERT INTO工作,它必须是CREATE STREAM target AS SELECT(“ CSAS”)。

让我们一直努力。在这里,我使用this docker-compose进行测试设置。

填充一些虚拟数据:

docker run --rm --interactive --network cos_default confluentinc/cp-kafkacat kafkacat -b kafka:29092 -t events -P <<EOF
{"eventType":"1", "eventTime" :"2018-11-13-06:34:57", "sourceHostName":"asgard"}
{"eventType":"2", "eventTime" :"2018-11-13-06:35:57", "sourceHostName":"asgard"}
{"eventType":"MatchingValue", "eventTime" :"2018-11-13-06:35:58", "sourceHostName":"asgard"}
EOF

使用KSQL注册源主题:

CREATE STREAM event_stream (eventType varchar, eventTime varchar, sourceHostName varchar) WITH (kafka_topic='events', value_format='json');

查询流:

ksql> SET 'auto.offset.reset' = 'earliest';
Successfully changed local property 'auto.offset.reset' from 'null' to 'earliest'
ksql> SELECT * FROM event_stream;
1542091084660 | null | 1 | 2018-11-13-06:34:57 | asgard
1542091084660 | null | 2 | 2018-11-13-06:35:57 | asgard
1542091785207 | null | MatchingValue | 2018-11-13-06:35:58 | asgard

因此,请看一下您引用的CREATE STREAM

CREATE STREAM output_stream (eventTime varchar, extraColumn varchar, sourceHostName varchar) WITH (kafka_topic='output_events', value_format='json');

我的猜测是,如果您运行LIST TOPICS;,您会发现该主题已经存在于您的Kafka经纪人上吗?

ksql> LIST TOPICS;

Kafka Topic            | Registered | Partitions | Partition Replicas | Consumers | ConsumerGroups
----------------------------------------------------------------------------------------------------
_confluent-metrics     | false      | 12         | 1                  | 0         | 0
_schemas               | false      | 1          | 1                  | 0         | 0
docker-connect-configs | false      | 1          | 1                  | 0         | 0
docker-connect-offsets | false      | 25         | 1                  | 0         | 0
docker-connect-status  | false      | 5          | 1                  | 0         | 0
events                 | true       | 1          | 1                  | 0         | 0
output_events          | false      | 4          | 1                  | 0         | 0
----------------------------------------------------------------------------------------------------
ksql>

如果不是,则此CREATE STREAM会失败:

ksql> CREATE STREAM output_stream (eventTime varchar, extraColumn varchar, sourceHostName varchar) WITH (kafka_topic='output_events', value_format='json');
Kafka topic does not exist: output_events
ksql>

因此,我在测试集群上也创建了这个主题:

$ docker-compose exec kafka bash -c "kafka-topics --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 4 --topic output_events"

然后创建流:

ksql> CREATE STREAM output_stream (eventTime varchar, extraColumn varchar, sourceHostName varchar) WITH (kafka_topic='output_events', value_format='json');

Message
----------------
Stream created
----------------

请注意,它说的是Stream created,而不是Stream created and running

现在让我们运行INSERT INTO

ksql> INSERT INTO output_stream SELECT eventTime, 'Extra info' as extraColumn, sourceHostName FROM event_stream WHERE eventType = 'MatchingValue';

Message
-------------------------------
Insert Into query is running.
-------------------------------

DESCRIBE EXTENDED的输出确实显示出正在处理的消息:

ksql> DESCRIBE EXTENDED output_stream;

Name                 : OUTPUT_STREAM
Type                 : STREAM
Key field            :
Key format           : STRING
Timestamp field      : Not set - using <ROWTIME>
Value format         : JSON
Kafka topic          : output_events (partitions: 4, replication: 1)

Field          | Type
--------------------------------------------
ROWTIME        | BIGINT           (system)
ROWKEY         | VARCHAR(STRING)  (system)
EVENTTIME      | VARCHAR(STRING)
EXTRACOLUMN    | VARCHAR(STRING)
SOURCEHOSTNAME | VARCHAR(STRING)
--------------------------------------------

Queries that write into this STREAM
-----------------------------------
InsertQuery_0 : INSERT INTO output_stream SELECT eventTime, 'Extra info' as extraColumn, sourceHostName FROM event_stream WHERE eventType = 'MatchingValue';

For query topology and execution plan please run: EXPLAIN <QueryId>

Local runtime statistics
------------------------
messages-per-sec:      0.01   total-messages:         1     last-message: 11/13/18 6:49:46 AM UTC
failed-messages:         0 failed-messages-per-sec:         0      last-failed:       n/a
(Statistics of the local KSQL server interaction with the Kafka topic output_events)

但是主题本身没有消息:

ksql> print 'output_events' from beginning;
^C

也不是KSQL流:

ksql> SELECT * FROM OUTPUT_STREAM;
^CQuery terminated

因此INSERT INTO命令旨在针对现有的CSAS / CTAS 目标流运行,而不是针对现有主题注册的源STREAM。 < / p>

让我们这样尝试。首先,我们需要删除现有的流定义,并终止INSERT INTO查询:

ksql> DROP STREAM OUTPUT_STREAM;
Cannot drop OUTPUT_STREAM.
The following queries read from this source: [].
The following queries write into this source: [InsertQuery_0].
You need to terminate them before dropping OUTPUT_STREAM.
ksql> TERMINATE InsertQuery_0;

Message
-------------------
Query terminated.
-------------------
ksql> DROP STREAM OUTPUT_STREAM;

Message
------------------------------------
Source OUTPUT_STREAM was dropped.
------------------------------------

现在创建目标流:

ksql> CREATE STREAM output_stream WITH (kafka_topic='output_events') AS SELECT eventTime, 'Extra info' as extraColumn, sourceHostName FROM event_stream WHERE eventType = 'MatchingValue';

Message
----------------------------
Stream created and running
----------------------------

请注意,在创建流时,它也是running(与之前的created相比)。现在查询流:

ksql> SELECT * FROM OUTPUT_STREAM;
1542091785207 | null | 2018-11-13-06:35:58 | Extra info | asgard

并检查基础主题:

ksql> PRINT 'output_events' FROM BEGINNING;
Format:JSON
{"ROWTIME":1542091785207,"ROWKEY":"null","EVENTTIME":"2018-11-13-06:35:58","EXTRACOLUMN":"Extra info","SOURCEHOSTNAME":"asgard"}

因此,您遇到了KSQL(raised here)中的错误,但幸运的是,可以通过完全使用更简单的KSQL语法(将CREATE STREAMINSERT INTO查询合并为一。