|
| 1 | +.. Copyright 2021 GridGain Systems, Inc. and Contributors. |
| 2 | +
|
| 3 | +.. Licensed under the GridGain Community Edition License (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | +.. https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license |
| 8 | +
|
| 9 | +.. Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +
|
| 15 | +.. _async_examples_of_usage: |
| 16 | + |
| 17 | +============================ |
| 18 | +Asynchronous client examples |
| 19 | +============================ |
| 20 | +File: `async_key_value.py`_. |
| 21 | + |
| 22 | +Basic usage |
| 23 | +----------- |
| 24 | +Asynchronous client and cache (:py:class:`~pygridgain.aio_client.AioClient` and :py:class:`~pygridgain.aio_cache.AioCache`) |
| 25 | +has mostly the same API as synchronous ones (:py:class:`~pygridgain.client.Client` and :py:class:`~pygridgain.cache.Cache`). |
| 26 | +But there is some peculiarities. |
| 27 | + |
| 28 | +Basic key-value |
| 29 | +=============== |
| 30 | +Firstly, import dependencies. |
| 31 | + |
| 32 | +.. literalinclude:: ../examples/async_key_value.py |
| 33 | + :language: python |
| 34 | + :lines: 18 |
| 35 | + |
| 36 | +Let's connect to cluster and perform key-value queries. |
| 37 | + |
| 38 | +.. literalinclude:: ../examples/async_key_value.py |
| 39 | + :language: python |
| 40 | + :dedent: 4 |
| 41 | + :lines: 23-38 |
| 42 | + |
| 43 | +Scan |
| 44 | +==== |
| 45 | +The :py:meth:`~pygridgain.aio_cache.AioСache.scan` method returns :py:class:`~pygridgain.cursors.AioScanCursor`, |
| 46 | +that yields the resulting rows. |
| 47 | + |
| 48 | +.. literalinclude:: ../examples/async_key_value.py |
| 49 | + :language: python |
| 50 | + :dedent: 4 |
| 51 | + :lines: 39-50 |
| 52 | + |
| 53 | + |
| 54 | +File: `async_sql.py`_. |
| 55 | + |
| 56 | +SQL |
| 57 | +--- |
| 58 | + |
| 59 | +First let us establish a connection. |
| 60 | + |
| 61 | +.. literalinclude:: ../examples/async_sql.py |
| 62 | + :language: python |
| 63 | + :dedent: 4 |
| 64 | + :lines: 197-198 |
| 65 | + |
| 66 | +Then create tables. Begin with `Country` table, than proceed with related |
| 67 | +tables `City` and `CountryLanguage`. |
| 68 | + |
| 69 | +.. literalinclude:: ../examples/async_sql.py |
| 70 | + :language: python |
| 71 | + :lines: 25-42, 51-59, 67-74 |
| 72 | + |
| 73 | +.. literalinclude:: ../examples/async_sql.py |
| 74 | + :language: python |
| 75 | + :dedent: 4 |
| 76 | + :lines: 199-205 |
| 77 | + |
| 78 | +Create indexes. |
| 79 | + |
| 80 | +.. literalinclude:: ../examples/async_sql.py |
| 81 | + :language: python |
| 82 | + :lines: 60-62, 75-77 |
| 83 | + |
| 84 | +.. literalinclude:: ../examples/async_sql.py |
| 85 | + :language: python |
| 86 | + :dedent: 8 |
| 87 | + :lines: 207-209 |
| 88 | + |
| 89 | +Fill tables with data. |
| 90 | + |
| 91 | +.. literalinclude:: ../examples/async_sql.py |
| 92 | + :language: python |
| 93 | + :lines: 43-50, 63-66, 78-81 |
| 94 | + |
| 95 | +.. literalinclude:: ../examples/async_sql.py |
| 96 | + :language: python |
| 97 | + :dedent: 8 |
| 98 | + :lines: 212-223 |
| 99 | + |
| 100 | +Now let us answer some questions. |
| 101 | + |
| 102 | +What are the 10 largest cities in our data sample (population-wise)? |
| 103 | +==================================================================== |
| 104 | + |
| 105 | +.. literalinclude:: ../examples/async_sql.py |
| 106 | + :language: python |
| 107 | + :dedent: 8 |
| 108 | + :lines: 225-243 |
| 109 | + |
| 110 | +The :py:meth:`~pygridgain.aio_client.AioClient.sql` method returns :py:class:`~pygridgain.cursors.AioSqlFieldsCursor`, |
| 111 | +that yields the resulting rows. |
| 112 | + |
| 113 | +What are the 10 most populated cities throughout the 3 chosen countries? |
| 114 | +======================================================================== |
| 115 | + |
| 116 | +If you set the `include_field_names` argument to `True`, the |
| 117 | +:py:meth:`~pygridgain.client.Client.sql` method will generate a list of |
| 118 | +column names as a first yield. Unfortunately, there is no async equivalent of `next` but |
| 119 | +you can await :py:meth:`__anext__()` |
| 120 | +of :py:class:`~pygridgain.cursors.AioSqlFieldsCursor` |
| 121 | + |
| 122 | +.. literalinclude:: ../examples/async_sql.py |
| 123 | + :language: python |
| 124 | + :dedent: 8 |
| 125 | + :lines: 246-271 |
| 126 | + |
| 127 | +Display all the information about a given city |
| 128 | +============================================== |
| 129 | + |
| 130 | +.. literalinclude:: ../examples/async_sql.py |
| 131 | + :language: python |
| 132 | + :dedent: 8 |
| 133 | + :lines: 273-288 |
| 134 | + |
| 135 | +Finally, delete the tables used in this example with the following queries: |
| 136 | + |
| 137 | +.. literalinclude:: ../examples/async_sql.py |
| 138 | + :language: python |
| 139 | + :lines: 83 |
| 140 | + |
| 141 | +.. literalinclude:: ../examples/async_sql.py |
| 142 | + :language: python |
| 143 | + :dedent: 8 |
| 144 | + :lines: 290-297 |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +.. _async_key_value.py: https://github.com/apache/ignite-python-thin-client/blob/master/examples/async_key_value.py |
| 150 | +.. _async_sql.py: https://github.com/apache/ignite-python-thin-client/blob/master/examples/async_sql.py |
0 commit comments