|
| 1 | +cqlengine |
| 2 | +=============== |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +cqlengine is a Cassandra CQL 3 Object Mapper for Python |
| 9 | + |
| 10 | +**Users of versions < 0.16, the default keyspace 'cqlengine' has been removed. Please read this before upgrading:** [Breaking Changes](https://cqlengine.readthedocs.org/en/latest/topics/models.html#keyspace-change) |
| 11 | + |
| 12 | +[Documentation](https://cqlengine.readthedocs.org/en/latest/) |
| 13 | + |
| 14 | +[Report a Bug](https://github.com/cqlengine/cqlengine/issues) |
| 15 | + |
| 16 | +[Users Mailing List](https://groups.google.com/forum/?fromgroups#!forum/cqlengine-users) |
| 17 | + |
| 18 | +## Installation |
| 19 | +``` |
| 20 | +pip install cqlengine |
| 21 | +``` |
| 22 | + |
| 23 | +## Getting Started on your local machine |
| 24 | + |
| 25 | +```python |
| 26 | +#first, define a model |
| 27 | +import uuid |
| 28 | +from cqlengine import columns |
| 29 | +from cqlengine.models import Model |
| 30 | + |
| 31 | +class ExampleModel(Model): |
| 32 | + read_repair_chance = 0.05 # optional - defaults to 0.1 |
| 33 | + example_id = columns.UUID(primary_key=True, default=uuid.uuid4) |
| 34 | + example_type = columns.Integer(index=True) |
| 35 | + created_at = columns.DateTime() |
| 36 | + description = columns.Text(required=False) |
| 37 | + |
| 38 | +#next, setup the connection to your cassandra server(s) and the default keyspace... |
| 39 | +>>> from cqlengine import connection |
| 40 | +>>> connection.setup(['127.0.0.1'], "cqlengine") |
| 41 | + |
| 42 | +# or if you're still on cassandra 1.2 |
| 43 | +>>> connection.setup(['127.0.0.1'], "cqlengine", protocol_version=1) |
| 44 | + |
| 45 | +# create your keyspace. This is, in general, not what you want in production |
| 46 | +# see https://cassandra.apache.org/doc/cql3/CQL.html#createKeyspaceStmt for options |
| 47 | +>>> create_keyspace("cqlengine", "SimpleStrategy", 1) |
| 48 | + |
| 49 | +#...and create your CQL table |
| 50 | +>>> from cqlengine.management import sync_table |
| 51 | +>>> sync_table(ExampleModel) |
| 52 | + |
| 53 | +#now we can create some rows: |
| 54 | +>>> em1 = ExampleModel.create(example_type=0, description="example1", created_at=datetime.now()) |
| 55 | +>>> em2 = ExampleModel.create(example_type=0, description="example2", created_at=datetime.now()) |
| 56 | +>>> em3 = ExampleModel.create(example_type=0, description="example3", created_at=datetime.now()) |
| 57 | +>>> em4 = ExampleModel.create(example_type=0, description="example4", created_at=datetime.now()) |
| 58 | +>>> em5 = ExampleModel.create(example_type=1, description="example5", created_at=datetime.now()) |
| 59 | +>>> em6 = ExampleModel.create(example_type=1, description="example6", created_at=datetime.now()) |
| 60 | +>>> em7 = ExampleModel.create(example_type=1, description="example7", created_at=datetime.now()) |
| 61 | +>>> em8 = ExampleModel.create(example_type=1, description="example8", created_at=datetime.now()) |
| 62 | + |
| 63 | +#and now we can run some queries against our table |
| 64 | +>>> ExampleModel.objects.count() |
| 65 | +8 |
| 66 | +>>> q = ExampleModel.objects(example_type=1) |
| 67 | +>>> q.count() |
| 68 | +4 |
| 69 | +>>> for instance in q: |
| 70 | +>>> print instance.description |
| 71 | +example5 |
| 72 | +example6 |
| 73 | +example7 |
| 74 | +example8 |
| 75 | + |
| 76 | +#here we are applying additional filtering to an existing query |
| 77 | +#query objects are immutable, so calling filter returns a new |
| 78 | +#query object |
| 79 | +>>> q2 = q.filter(example_id=em5.example_id) |
| 80 | + |
| 81 | +>>> q2.count() |
| 82 | +1 |
| 83 | +>>> for instance in q2: |
| 84 | +>>> print instance.description |
| 85 | +example5 |
| 86 | +``` |
| 87 | + |
| 88 | +## Contributing |
| 89 | + |
| 90 | +If you'd like to contribute to cqlengine, please read the [contributor guidelines](https://github.com/bdeggleston/cqlengine/blob/master/CONTRIBUTING.md) |
| 91 | + |
| 92 | + |
| 93 | +## Authors |
| 94 | + |
| 95 | +cqlengine was developed primarily by (Blake Eggleston)[blakeeggleston](https://twitter.com/blakeeggleston) and [Jon Haddad](https://twitter.com/rustyrazorblade), with contributions from several others in the community. |
| 96 | + |
0 commit comments