@@ -171,6 +171,135 @@ As Pandas DataFrame:
171171 df = as_pandas(cursor)
172172 print (df.describe())
173173
174+ Asynchronous Cursor
175+ ~~~~~~~~~~~~~~~~~~~
176+
177+ Asynchronous cursor is a simple implementation using the concurrent.futures package.
178+ Python 2.7 uses `backport of the concurrent.futures `_ package.
179+ This cursor is not `DB API 2.0 (PEP 249) `_ compliant.
180+
181+ You can use the asynchronous cursor by specifying the ``cursor_class ``
182+ with the connect method or connection object.
183+
184+ .. code :: python
185+
186+ from pyathena import connect
187+ from pyathena.async_cursor import AsyncCursor
188+
189+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
190+ region_name = ' us-west-2' ,
191+ cursor_class = AsyncCursor).cursor()
192+
193+ .. code :: python
194+
195+ from pyathena import connect
196+ from pyathena.async_cursor import AsyncCursor
197+
198+ cursor = Connection(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
199+ region_name = ' us-west-2' ,
200+ cursor_class = AsyncCursor).cursor()
201+
202+ It can also be used by specifying the cursor class when calling the connection object's cursor method.
203+
204+ .. code :: python
205+
206+ from pyathena import connect
207+ from pyathena.async_cursor import AsyncCursor
208+
209+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
210+ region_name = ' us-west-2' ).cursor(AsyncCursor)
211+
212+ .. code :: python
213+
214+ from pyathena import connect
215+ from pyathena.async_cursor import AsyncCursor
216+
217+ cursor = Connection(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
218+ region_name = ' us-west-2' ).cursor(AsyncCursor)
219+
220+ The default number of workers is 5 or cpu number * 5.
221+ If you want to change the number of workers you can specify like the following.
222+
223+ .. code :: python
224+
225+ from pyathena import connect
226+ from pyathena.async_cursor import AsyncCursor
227+
228+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
229+ region_name = ' us-west-2' ,
230+ cursor_class = AsyncCursor).cursor(max_workers = 10 )
231+
232+ The execute method of the asynchronous cursor returns the tuple of the query ID and the `future object `_.
233+
234+ .. code :: python
235+
236+ from pyathena import connect
237+ from pyathena.async_cursor import AsyncCursor
238+
239+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
240+ region_name = ' us-west-2' ,
241+ cursor_class = AsyncCursor).cursor()
242+
243+ query_id, future = cursor.execute(" SELECT * FROM many_rows" )
244+
245+ The return value of the `future object `_ is an ``AthenaResultSet `` object.
246+ This object has an interface that can fetch and iterate query results similar to synchronous cursors.
247+ It also has information on the result of query execution.
248+
249+ .. code :: python
250+
251+ from pyathena import connect
252+ from pyathena.async_cursor import AsyncCursor
253+
254+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
255+ region_name = ' us-west-2' ,
256+ cursor_class = AsyncCursor).cursor()
257+
258+ query_id, future = cursor.execute(" SELECT * FROM many_rows" )
259+ result_set = future.result()
260+ print (result_set.state)
261+ print (result_set.state_change_reason)
262+ print (result_set.completion_date_time)
263+ print (result_set.submission_date_time)
264+ print (result_set.data_scanned_in_bytes)
265+ print (result_set.execution_time_in_millis)
266+ print (result_set.output_location)
267+ print (result_set.description)
268+ for row in result_set:
269+ print (row)
270+
271+ .. code :: python
272+
273+ from pyathena import connect
274+ from pyathena.async_cursor import AsyncCursor
275+
276+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
277+ region_name = ' us-west-2' ,
278+ cursor_class = AsyncCursor).cursor()
279+
280+ query_id, future = cursor.execute(" SELECT * FROM many_rows" )
281+ result_set = future.result()
282+ print (result_set.fetchall())
283+
284+ A query ID is required to cancel a query with the asynchronous cursor.
285+
286+ .. code :: python
287+
288+ from pyathena import connect
289+ from pyathena.async_cursor import AsyncCursor
290+
291+ cursor = connect(s3_staging_dir = ' s3://YOUR_S3_BUCKET/path/to/' ,
292+ region_name = ' us-west-2' ,
293+ cursor_class = AsyncCursor).cursor()
294+
295+ query_id, future = cursor.execute(" SELECT * FROM many_rows" )
296+ cursor.cancel(query_id)
297+
298+ NOTE: The cancel method of the `future object `_ does not cancel the query.
299+
300+ .. _`backport of the concurrent.futures` : https://pypi.python.org/pypi/futures
301+ .. _`future object` : https://docs.python.org/3/library/concurrent.futures.html#future-objects
302+
174303Credentials
175304-----------
176305
0 commit comments