A python wrapper for the KSQL REST API. Easily interact with the KSQL REST API using this library.
pip install ksqlOr
git clone https://github.com/bryanyang0528/ksql-python
cd ksql-python
python setup.py installThis is the GITHUB page of KSQL. https://github.com/confluentinc/ksql
- Setup for the KSql API:
from ksql import KSQLAPI
client = KSQLAPI('http://ksql-server:8080')| Option | Type | Required | Description |
|---|---|---|---|
url |
string | yes | Your ksql-server url. Example: http://ksql-server:8080 |
timeout |
integer | no | Timout for Requests. Default: 5 |
client.ksql('show tables')- Example Response
[{'tables': {'statementText': 'show tables;', 'tables': []}}]
It will execute sql query and keep listening streaming data.
client.query('select * from table1')Example Response
{"row":{"columns":[1512787743388,"key1",1,2,3]},"errorMessage":null} {"row":{"columns":[1512787753200,"key1",1,2,3]},"errorMessage":null} {"row":{"columns":[1512787753488,"key1",1,2,3]},"errorMessage":null} {"row":{"columns":[1512787753888,"key1",1,2,3]},"errorMessage":null}
client.create_stream(table_name=table_name,
columns_type=columns_type,
topic=topic,
value_format=value_format)| Option | Type | Required | Description |
|---|---|---|---|
table_name |
string | yes | name of stream/table |
columns_type |
list | yes | ex:['viewtime bigint','userid varchar','pageid varchar'] |
topic |
string | yes | Kafka topic |
value_format |
string | no | DELIMITED``(Default) or ``JSON |
- Responses
| If create table/stream succeed: | return True |
|---|---|
| If failed: | raise a CreatError(respose_from_ksql_server) |
a simplified api for creating stream as select
client.create_stream_as(table_name=table_name,
select_columns=select_columns,
src_table=src_table,
kafka_topic=kafka_topic,
value_format=value_format,
conditions=conditions,
partition_by=partition_by,
**kwargs)CREATE STREAM <table_name>
[WITH ( kafka_topip=<kafka_topic>, value_format=<value_format>, property_name=expression ... )]
AS SELECT <select_columns>
FROM <src_table>
[WHERE <conditions>]
PARTITION BY <partition_by>];| Option | Type | Required | Description |
|---|---|---|---|
table_name |
string | yes | name of stream/table |
select_columns |
list | yes | you can select [*] or ['columnA', 'columnB'] |
src_table |
string | yes | name of source table |
kafka_topic |
string | no | The name of the Kafka topic of this new stream(table). |
value_format |
string | no | DELIMITED``(Default), ``JSON or AVRO |
conditions |
string | no | The conditions in the where clause. |
partition_by |
string | no | Data will be distributed across partitions by this column. |
kwargs |
pair | no | please provide key=value pairs. Please see more options. |
There are more properties (partitions, replicas, etc...) in the official document.
- Responses
| If create table/stream succeed: | return True |
|---|---|
| If failed: | raise a CreatError(respose_from_ksql_server) |