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