简体   繁体   English

使用 ksqldb-server 在 kafka 中创建表

[英]Create table in kafka using ksqldb-server

I am trying to create a kafka table using the (Confluent) ksqldb-server via its REST interface using the following code (bash script):我正在尝试使用以下代码(bash 脚本)通过其 REST 接口使用(Confluent)ksqldb-server 创建一个 kafka 表:

KSQLDB_COMMAND="CREATE TABLE sample_table \
  (xkey VARCHAR, \
   xdata VARCHAR) \
  WITH (KAFKA_TOPIC=\'sample-topic\', \
        VALUE_FORMAT=\'JSON\', \
       KEY=\'xkey\'); "

COMMAND="curl -X 'POST' '$KSQLDB_SERVER' \
    -H 'Content-Type: application/vnd.ksql.v1+json; charset=utf-8' \
    -d '{ \"ksql\": \"$KSQLDB_COMMAND\" }' "
eval $COMMAND

The following error output message is returned:返回以下错误 output 消息:

{"@type":"statement_error","error_code":40001,"message":"Failed to prepare statement: Invalid config variable(s) in the WITH clause: KEY","statementText":"CREATE TABLE sample_table (xkey VARCHAR, xdata VARCHAR) WITH (KAFKA_TOPIC='sample-topic', VALUE_FORMAT='JSON', KEY='xkey');","entities":[]}%

The error suggests an error in the actual statement, in particular with the KEY attribute.该错误表明实际语句中存在错误,尤其是 KEY 属性。

I can get basic commands ("LIST STREAMS" etc) working using the REST interface but can not create tables, so I figure this is a problem in the KSQL statement or how I am create the bash command (in "COMMAND" variable).我可以使用 REST 接口获取基本命令(“LIST STREAMS”等),但无法创建表,因此我认为这是 KSQL 语句中的问题或我如何创建 bash 命令(在“COMMAND”变量中)。

Any help is appreciated.任何帮助表示赞赏。

I spent a fair bit of time experimenting and got this simple example working (my original attempt required too many bash variable substitutions to make it useful/maintainable, so this version is simplified quite a bit).我花了相当多的时间进行实验并让这个简单的示例工作(我最初的尝试需要太多的 bash 变量替换以使其有用/可维护,所以这个版本被简化了很多)。 I also found that KSQLDB table names must follow regular SQL naming conventions for table names (ie. alpha, underscores, etc... but no hyphens, which caused a bunch of errors in my original question... I should have read the documentation more carefully).我还发现 KSQLDB 表名必须遵循表名的常规 SQL 命名约定(即 alpha、下划线等......但没有连字符,这在我原来的问题中导致了一堆错误......我应该阅读文档更仔细)。

The following works (you may need to change your KSQLDB server address)... and with minimal changes, just about any KSQLDB command can be executed:以下工作(您可能需要更改您的 KSQLDB 服务器地址)......并且只需很少的更改,几乎可以执行任何 KSQLDB 命令:

####
# NOTE: table MUST be alpha (underscores are OK)... hyphens are not allowed
####
KSQLDB_SERVER="http://localhost:8088/ksql"
KSQLDB_TABLE="some_table"
KSQLDB_TOPIC="some_topic"
VALUE_FORMAT="JSON"

FMT="{ \"ksql\": \"CREATE TABLE %s (key VARCHAR PRIMARY KEY, data VARCHAR) WITH (KAFKA_TOPIC='%s', VALUE_FORMAT='%s');\" }"
JSON_DATA=$(printf "$FMT" "$KSQLDB_TABLE" "$KSQLDB_TOPIC" "$VALUE_FORMAT")

curl -X "POST" "$KSQLDB_SERVER" \
     -H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8" \
     -d "$JSON_DATA"

you can't specify KEY for table, KEY is used for streams.您不能为表指定 KEY,KEY 用于流。 you should use PRIMARY KEY for table in the type declaration.like:您应该在类型声明中对表使用 PRIMARY KEY。例如:

CREATE OR REPLACE TABLE TABLE_1 ( ID INT PRIMARY KEY, EMAILADDRESS VARCHAR, ISPRIMARY BOOLEAN, USERID INT, PARANT INT )WITH (KAFKA_TOPIC='test_1', VALUE_FORMAT='AVRO', KEY_FORMAT='AVRO');创建或替换表 TABLE_1 (ID INT PRIMARY KEY, EMAILADDRESS VARCHAR, ISPRIMARY BOOLEAN, USERID INT, PARANT INT)WITH (KAFKA_TOPIC='test_1', VALUE_FORMAT='AVRO', KEY_FORMAT='AVRO');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM