# Create a table from a stream with python client

**URL:** <https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787>\
**Category:** ksqlDB\
**Created:** [16 September 2021 08:33 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787 "2021-09-16T08:33:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiuseppeR](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@GiuseppeR](https://forum.confluent.io/u/GiuseppeR)\
**Post date:** [16 September 2021 08:33 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787/1 "2021-09-16T08:33:20Z")

</div>

I try to create a table with python client ksql. My query is:

```sql
CREATE TABLE AVG5MINTEMP WITH (KAFKA_TOPIC='AVG5MINTEMP', KEY_FORMAT='JSON', PARTITIONS=1, REPLICAS=1) AS SELECT
  METEO_RAW.TYPE TYPE,
  METEO_RAW.NOME_UNITA NOME_UNITA,
  AVG(METEO_RAW.VALORE) AVG_VALUE
FROM METEO_RAW METEO_RAW
WINDOW TUMBLING ( SIZE 5 MINUTES ) 
GROUP BY METEO_RAW.TYPE, METEO_RAW.NOME_UNITA
where METEO_RAW.NOME_UNITA = 'Unita 1' AND
METEO_RAW.ID_SENSORE = 2
EMIT CHANGES;

```

where METEO\_RAW is a stream from a Kafka topic. In the UI KSQLdb interface it works fine.  
So I try with the sample command to submit a query:

```python
client.create_stream_as(table_name="AVG5MINTEMP",
                        select_columns=["METEO_RAW.TYPE TYPE", "METEO_RAW.NOME_UNITA NOME_UNITA", "AVG(NOME_UNITA.VALORE) AVG_VALUE"],
                        src_table="METEO_RAW",
                        kafka_topic="AVG5MINTEMP",
                        value_format = 'JSON',
                        conditions="WINDOW TUMBLING ( SIZE 5 MINUTES ) GROUP BY TYPE \
                        WHERE NOME_UNITA = 'Unita 1' AND METEO_RAW.ID_SENSORE=2 EMIT CHANGES; ")

```

but I receive the error:

`DEBUG:root:content: {'@type': 'statement_error', 'error_code': 40001, 'message': "line 1:199: mismatched input 'WINDOW' expecting {'(', 'EMIT', 'CHANGES', 'FINAL', 'NOT', 'ESCAPE', 'NULL', 'TRUE', 'FALSE', 'INTEGER', 'DATE', 'TIME', 'TIMESTAMP', 'INTERVAL', 'YEAR', 'MONTH', 'DAY', 'HOUR', 'MINUTE', 'SECOND', 'ZONE', 'CASE', 'PARTITION', 'STRUCT', 'EXPLAIN', 'ANALYZE', 'TYPE', 'TYPES', 'CAST', 'SHOW', 'TABLES', 'COLUMNS', 'COLUMN', 'PARTITIONS', 'FUNCTIONS', 'FUNCTION', 'ARRAY', 'MAP', 'SET', 'RESET', 'SESSION', 'KEY', 'SINK', 'SOURCE', 'PRIMARY', 'REPLACE', 'ASSERT', 'ADD', 'ALTER', 'IF', '+', '-', STRING, INTEGER_VALUE, DECIMAL_VALUE, FLOATING_POINT_VALUE, IDENTIFIER, DIGIT_IDENTIFIER, QUOTED_IDENTIFIER, BACKQUOTED_IDENTIFIER, VARIABLE}", 'statementText': "CREATE stream AVG5MINTEMP WITH (kafka_topic='AVG5MINTEMP', value_format='JSON') AS SELECT METEO_RAW.TYPE TYPE, METEO_RAW.NOME_UNITA NOME_UNITA, AVG(NOME_UNITA.VALORE) AVG_VALUE FROM METEO_RAW where WINDOW TUMBLING ( SIZE 5 MINUTES ) GROUP BY TYPE WHERE NOME_UNITA = 'Unita 1' AND METEO_RAW.ID_SENSORE=2 EMIT CHANGES;", 'entities': []}`

I try with create\_stram\_as in this manner:

```python
client.create_stream_as(table_name="AVG5MINTEMP",
                        select_columns=["METEO_RAW.TYPE TYPE", "METEO_RAW.NOME_UNITA NOME_UNITA", "AVG(METEO_RAW.VALORE) AVG_VALUE"],
                        src_table="METEO_RAW METEO RAW",
                        kafka_topic="AVG5MINTEMP",
                        value_format = 'JSON',
                        conditions="WINDOW TUMBLING ( SIZE 5 MINUTES ) METEO_RAW.NOME_UNITA = 'Unita 1' AND METEO_RAW.ID_SENSORE=2 EMIT CHANGES; ")

```

but I receive the error:

`DEBUG:root:KSQL generated: CREATE stream AVG5MINTEMP WITH (kafka_topic='AVG5MINTEMP', value_format='JSON') AS SELECT METEO_RAW.TYPE TYPE, METEO_RAW.NOME_UNITA NOME_UNITA, AVG(METEO_RAW.VALORE) AVG_VALUE FROM METEO_RAW METEO RAW where WINDOW TUMBLING ( SIZE 5 MINUTES ) METEO_RAW.NOME_UNITA = 'Unita 1' AND METEO_RAW.ID_SENSORE=2 EMIT CHANGES;`  
`DEBUG:root:content: {'@type': 'statement_error', 'error_code': 40001, 'message': "line 1:198: mismatched input 'RAW' expecting {';', 'EMIT', 'WHERE', 'WINDOW', 'GROUP', 'HAVING', 'LIMIT', 'PARTITION'}", 'statementText': "CREATE stream AVG5MINTEMP WITH (kafka_topic='AVG5MINTEMP', value_format='JSON') AS SELECT METEO_RAW.TYPE TYPE, METEO_RAW.NOME_UNITA NOME_UNITA, AVG(METEO_RAW.VALORE) AVG_VALUE FROM METEO_RAW METEO RAW where WINDOW TUMBLING ( SIZE 5 MINUTES ) METEO_RAW.NOME_UNITA = 'Unita 1' AND METEO_RAW.ID_SENSORE=2 EMIT CHANGES;", 'entities': []}`

What is wrong?  
Any help is apreciated.

---

<div class="post-metadata">

**Author:** ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)\
**Post date:** [16 September 2021 09:15 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787/2 "2021-09-16T09:15:03Z")

</div>

This looks like something specific to the Python client library. Is it this one? [GitHub - bryanyang0528/ksql-python: A python wrapper for the KSQL REST API.](https://github.com/bryanyang0528/ksql-python) You might be best raising an issue on the github repo there.

---

<div class="post-metadata">

**Author:** ![GiuseppeR](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@GiuseppeR](https://forum.confluent.io/u/GiuseppeR)\
**Post date:** [16 September 2021 09:24 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787/3 "2021-09-16T09:24:33Z")

</div>

Yes @rmoff ! it is the client you mentioned. I try to open an issue on the github because it seems there is no manner to make a this type of query.

I try with python library request to make a http call:

```python

import requests

url = 'http://192.XX.XX.XX:8088/ksql'

headers = {'accept': 'application/vnd.ksql.v1+json', 'content-type': 'application/json' }

payload = '{"query": "CREATE TABLE AVG5MINTEMP WITH (KAFKA_TOPIC="AVG5MINTEMP", KEY_FORMAT="JSON", PARTITIONS=1, REPLICAS=1) AS SELECT \

METEO_RAW.TYPE TYPE,\

METEO_RAW.NOME_UNITA NOME_UNITA, \

AVG(METEO_RAW.VALORE) AVG_VALUE \

FROM METEO_RAW METEO_RAW \

WINDOW TUMBLING ( SIZE 5 MINUTES ) \

GROUP BY METEO_RAW.TYPE, METEO_RAW.NOME_UNITA \

where METEO_RAW.NOME_UNITA = "Unita 1" AND \

METEO_RAW.ID_SENSORE = 2 \

EMIT CHANGES;", "streamsProperties": {}}'

r = requests.post(url, headers=headers, data=payload)

```

but I receive a 400 error:

```auto

DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 192.XX.XX.XX:8088

DEBUG:urllib3.connectionpool:http://192.XX.XX.XX:8088 "POST /ksql HTTP/1.1" 400 656

```

---

<div class="post-metadata">

**Author:** ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)\
**Post date:** [16 September 2021 12:38 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787/4 "2021-09-16T12:38:20Z")

</div>

What’s the full value of `r.text` ? The two `DEBUG` messages you’ve quoted don’t show the actual error. If I had to guess it’ll be the `"` in your SQL which are also closing the JSON, so you need to fix the escaping of those characters.

---

<div class="post-metadata">

**Author:** ![GiuseppeR](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@GiuseppeR](https://forum.confluent.io/u/GiuseppeR)\
**Post date:** [16 September 2021 13:10 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787/5 "2021-09-16T13:10:15Z")

</div>

@rmoff as you suggested I try to escape ":

> url = ‘[http://192.20.102.18:8088/query](http://192.20.102.18:8088/query)’  
> headers = {‘accept’: ‘application/vnd.ksqlapi.delimited.v1’, ‘content-type’: ‘application/json’ }  
> payload = ‘{“query”: "CREATE TABLE AVG5MINTEMP WITH (KAFKA\_TOPIC='\AVG5MINTEMP\’, KEY\_FORMAT=\‘JSON\’, PARTITIONS=1, REPLICAS=1) AS SELECT   
> METEO\_RAW.TYPE TYPE,  
> METEO\_RAW.NOME\_UNITA NOME\_UNITA,   
> AVG(METEO\_RAW.VALORE) AVG\_VALUE   
> FROM METEO\_RAW METEO\_RAW   
> WINDOW TUMBLING ( SIZE 5 MINUTES )   
> GROUP BY METEO\_RAW.TYPE, METEO\_RAW.NOME\_UNITA   
> where METEO\_RAW.NOME\_UNITA =\‘Unita 1\’ AND   
> METEO\_RAW.ID\_SENSORE = 2   
> EMIT CHANGES;", “streamsProperties”: {}}’
> 
> r = requests.post(url, headers=headers, data=payload)

and the debug responses:

> DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 192.20.102.18:8088  
> DEBUG:urllib3.connectionpool:[http://192.20.102.18:8088](http://192.20.102.18:8088) “POST /query HTTP/1.1” 406 0

if I print r.text is doesn’t give any information because is empty:

> print(r.text)  
> ‘’

Some other idea?  
Ths.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [16 October 2021 08:33 UTC](https://forum.confluent.io/t/create-a-table-from-a-stream-with-python-client/2787/6 "2021-10-16T08:33:28Z")

</div>

This topic was automatically closed after 30 days. New replies are no longer allowed.
