ksqldb restapi:以json形式返回查询结果

pb3s4cty  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(381)

我发出以下请求:

curl --http2 -X "POST" "http://localhost:8088/query-stream"
             -H "Content-Type: application/vnd.ksql.v1+json; charset=utf-8"
             -d $'{
                  "sql": "SELECT * FROM USERS EMIT CHANGES;",
                  "streamsProperties": {
                    "ksql.streams.auto.offset.reset": "earliest"
                  }
}

我得到的结果是:

{"queryId":"cdfb3ccc-0ab5-4186-a249-b279bfc09587","columnNames":["USERID","NAME"],"columnTypes":["STRING","STRING"]}
["1","Max"]
["2","Alex"]
["13","Andrew"]
...

有没有一种方法可以获得json格式的数据?

{"userid":"1","name":"Max"}
{"userid":"2","name":"Alex"}
{"userid":"13","name":"Andrew"}

如果poco对象是json格式的,那么将这些数据反序列化为poco对象比解析“row”格式更容易。

6tqwzwtp

6tqwzwtp1#

根据文档,您可以设置 Accept 标题。它默认为 application/vnd.ksqlapi.delimited.v1 但也可以设置为 application/json :

curl --show-error --silent \
         -H "application/vnd.ksqlapi.delimited.v1" \
         --http2 'http://localhost:8088/query-stream' \
         --data-raw '{"sql":"SELECT * FROM CUSTOMERS WHERE ID=42;"}'
{"queryId":null,"columnNames":["ID","FIRST_NAME","LAST_NAME","EMAIL","GENDER","COMMENTS"],"columnTypes":["INTEGER","STRING","STRING","STRING","STRING","STRING"]}
[42,"Rick","Astley","r.astley@example.com","Male",""]
curl --show-error --silent \
         -H "Accept:application/json" \
         --http2 'http://localhost:8088/query-stream' \
         --data-raw '{"sql":"SELECT * FROM CUSTOMERS WHERE ID=42;"}'
[{"queryId":null,"columnNames":["ID","FIRST_NAME","LAST_NAME","EMAIL","GENDER","COMMENTS"],"columnTypes":["INTEGER","STRING","STRING","STRING","STRING","STRING"]},[42,"Rick","Astley","r.astley@example.com","Male",""]]

相关问题