项目作者: CharAct3

项目描述 :
Asyncio wrapper for cqlengine of cassandra python driver.
高级语言: Python
项目地址: git://github.com/CharAct3/aiocqlengine.git
创建时间: 2019-11-03T11:03:05Z
项目社区:https://github.com/CharAct3/aiocqlengine

开源协议:MIT License

下载


aiocqlengine

Async wrapper for cqlengine of cassandra python driver.

This project is built on cassandra-python-driver.

Actions Status

Installation

  1. $ pip install aiocqlengine

Change log

0.3.0

  • Due to aiocassandra is not maintained, removed the aiocassandra dependency.

0.2.0

  • Create new session wrapper for ResultSet, users need to wrap session by aiosession_for_cqlengine:
    1. from aiocqlengine.session import aiosession_for_cqlengine
  • Add new method of AioModel for paging:
    1. async for results in AioModel.async_iterate(fetch_size=100):
    2. # Do something with results
    3. pass

0.1.1

  • Add AioBatchQuery:
    1. batch_query = AioBatchQuery()
    2. for i in range(100):
    3. Model.batch(batch_query).create(id=uuid.uuid4())
    4. await batch_query.async_execute()

Example usage

  1. import asyncio
  2. import uuid
  3. import os
  4. from aiocqlengine.models import AioModel
  5. from aiocqlengine.query import AioBatchQuery
  6. from aiocqlengine.session import aiosession_for_cqlengine
  7. from cassandra.cluster import Cluster
  8. from cassandra.cqlengine import columns, connection, management
  9. class User(AioModel):
  10. user_id = columns.UUID(primary_key=True)
  11. username = columns.Text()
  12. async def run_aiocqlengine_example():
  13. # Model.objects.create() and Model.create() in async way:
  14. user_id = uuid.uuid4()
  15. await User.objects.async_create(user_id=user_id, username='user1')
  16. await User.async_create(user_id=uuid.uuid4(), username='user2')
  17. # Model.objects.all() and Model.all() in async way:
  18. print(list(await User.async_all()))
  19. print(list(await User.objects.filter(user_id=user_id).async_all()))
  20. # Model.object.update() in async way:
  21. await User.objects(user_id=user_id).async_update(username='updated-user1')
  22. # Model.objects.get() and Model.get() in async way:
  23. user = await User.objects.async_get(user_id=user_id)
  24. await User.async_get(user_id=user_id)
  25. print(user, user.username)
  26. # Model.save() in async way:
  27. user.username = 'saved-user1'
  28. await user.async_save()
  29. # Model.delete() in async way:
  30. await user.async_delete()
  31. # Batch Query in async way:
  32. batch_query = AioBatchQuery()
  33. User.batch(batch_query).create(user_id=uuid.uuid4(), username="user-1")
  34. User.batch(batch_query).create(user_id=uuid.uuid4(), username="user-2")
  35. User.batch(batch_query).create(user_id=uuid.uuid4(), username="user-3")
  36. await batch_query.async_execute()
  37. # Async iterator
  38. async for users in User.async_iterate(fetch_size=100):
  39. pass
  40. # The original cqlengine functions were still there
  41. print(len(User.objects.all()))
  42. def create_session():
  43. cluster = Cluster()
  44. session = cluster.connect()
  45. # Create keyspace, if already have keyspace your can skip this
  46. os.environ['CQLENG_ALLOW_SCHEMA_MANAGEMENT'] = 'true'
  47. connection.register_connection('cqlengine', session=session, default=True)
  48. management.create_keyspace_simple('example', replication_factor=1)
  49. management.sync_table(User, keyspaces=['example'])
  50. # Wrap cqlengine connection
  51. aiosession_for_cqlengine(session)
  52. session.set_keyspace('example')
  53. connection.set_session(session)
  54. return session
  55. def main():
  56. # Setup connection for cqlengine
  57. session = create_session()
  58. # Run the example function in asyncio loop
  59. loop = asyncio.get_event_loop()
  60. loop.run_until_complete(run_aiocqlengine_example())
  61. # Shutdown the connection and loop
  62. session.cluster.shutdown()
  63. loop.close()
  64. if __name__ == '__main__':
  65. main()

License

This project is under MIT license.