|
1 |
| -This is forked from https://github.com/mymarilyn/clickhouse-driver. Necessary changes are made to support https://github.com/timeplus-io/proton with date types and namespaces changes. |
| 1 | +Timeplus Proton Python Driver |
| 2 | +================ |
| 3 | + |
| 4 | +Introduction |
| 5 | +------------ |
| 6 | + |
| 7 | +`Proton <https://github.com/timeplus-io/proton>`_ is a unified streaming and historical data processing engine in a single binary. The historical store is built based on `ClickHouse <https://github.com/ClickHouse/ClickHouse>`_. |
| 8 | + |
| 9 | +This project provides python driver to interact with Proton, the code is based on https://github.com/mymarilyn/clickhouse-driver. |
| 10 | + |
| 11 | + |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +.. code-block:: shell |
| 16 | +
|
| 17 | + pip install proton-driver |
| 18 | +
|
| 19 | +
|
| 20 | +Quick Start |
| 21 | +------------ |
| 22 | + |
| 23 | +1. Run proton with docker, ``docker run -d -p 8463:8463 --pull always --name proton ghcr.io/timeplus-io/proton:develop`` |
| 24 | +2. Run following python code |
| 25 | + |
| 26 | +.. code-block:: python |
| 27 | +
|
| 28 | + from proton_driver import connect |
| 29 | + with connect("proton://default:@localhost:8463/default") as conn: |
| 30 | + with conn.cursor() as cursor: |
| 31 | + cursor.execute("select 1") |
| 32 | + print(cursor.fetchone()) |
| 33 | +
|
| 34 | +above code should return ``(1,)`` , which shows that everything is working fine now. |
| 35 | + |
| 36 | +Streaming Query |
| 37 | +------------ |
| 38 | + |
| 39 | +.. code-block:: python |
| 40 | +
|
| 41 | + from proton_driver import client |
| 42 | +
|
| 43 | + c = client.Client(host='127.0.0.1', port=8463) |
| 44 | +
|
| 45 | + # create a random stream if not exist |
| 46 | + c.execute("CREATE RANDOM STREAM IF NOT EXISTS" |
| 47 | + " devices(" |
| 48 | + " device string default 'device'||to_string(rand()%4), " |
| 49 | + " temperature float default rand()%1000/10" |
| 50 | + ")") |
| 51 | + # query the stream and return in a iterator |
| 52 | + rows = c.execute_iter( |
| 53 | + "SELECT device, count(*), min(temperature), max(temperature) " |
| 54 | + "FROM devices GROUP BY device", |
| 55 | + ) |
| 56 | + for row in rows: |
| 57 | + print(row) |
| 58 | +
|
| 59 | +
|
| 60 | +the output of the code will be something like following, as for streaming query is unbouded, you can add your flow control to terminate the loop. |
| 61 | + |
| 62 | +.. code-block:: shell |
| 63 | +
|
| 64 | + ('device0', 747, 0.0, 99.5999984741211) |
| 65 | + ('device1', 723, 0.10000000149011612, 99.30000305175781) |
| 66 | + ('device3', 768, 0.30000001192092896, 99.9000015258789) |
| 67 | + ('device2', 762, 0.20000000298023224, 99.80000305175781) |
| 68 | + ('device0', 1258, 0.0, 99.5999984741211) |
| 69 | + ('device1', 1216, 0.10000000149011612, 99.69999694824219) |
| 70 | + ('device3', 1276, 0.30000001192092896, 99.9000015258789) |
| 71 | + ('device2', 1250, 0.20000000298023224, 99.80000305175781) |
| 72 | +
|
0 commit comments