|
1 | 1 | Upgrading |
2 | 2 | ========= |
3 | 3 |
|
| 4 | +.. toctree:: |
| 5 | + :maxdepth: 1 |
| 6 | + |
| 7 | +Upgrading to 2.1 from 2.0 |
| 8 | +------------------------- |
| 9 | +Version 2.1 of the Datastax python driver for Apache Cassandra |
| 10 | +adds support for Cassandra 2.1 and version 3 of the native protocol. |
| 11 | + |
| 12 | +Cassandra 1.2, 2.0, and 2.1 are all supported. However, 1.2 only |
| 13 | +supports protocol version 1, and 2.0 only supports versions 1 and |
| 14 | +2, so some features may not be available. |
| 15 | + |
| 16 | +Using the v3 Native Protocol |
| 17 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 18 | +By default, the driver will attempt to use version 2 of the |
| 19 | +native protocol. To use version 3, you must explicitly |
| 20 | +set the :attr:`~.Cluster.protocol_version`: |
| 21 | + |
| 22 | +.. code-block:: python |
| 23 | +
|
| 24 | + from cassandra.cluster import Cluster |
| 25 | +
|
| 26 | + cluster = Cluster(protocol_version=3) |
| 27 | +
|
| 28 | +Note that protocol version 3 is only supported by Cassandra 2.1+. |
| 29 | + |
| 30 | +In future releases, the driver may default to using protocol version |
| 31 | +3. |
| 32 | + |
| 33 | +Working with User-Defined Types |
| 34 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 35 | +Cassandra 2.1 introduced the ability to define new types:: |
| 36 | + |
| 37 | + USE KEYSPACE mykeyspace; |
| 38 | + |
| 39 | + CREATE TYPE address (street text, city text, zip int); |
| 40 | + |
| 41 | +The driver generally expects you to use instances of a specific |
| 42 | +class to represent column values of this type. You can let the |
| 43 | +driver know what class to use with :meth:`.Cluster.register_user_type`: |
| 44 | + |
| 45 | +.. code-block:: python |
| 46 | +
|
| 47 | + cluster = Cluster() |
| 48 | +
|
| 49 | + class Address(object): |
| 50 | +
|
| 51 | + def __init__(self, street, city, zipcode): |
| 52 | + self.street = street |
| 53 | + self.city = text |
| 54 | + self.zipcode = zipcode |
| 55 | +
|
| 56 | + cluster.register_user_type('mykeyspace', 'address', Address) |
| 57 | +
|
| 58 | +When inserting data for ``address`` columns, you should pass in |
| 59 | +instances of ``Address``. When querying data, ``address`` column |
| 60 | +values will be instances of ``Address``. |
| 61 | + |
| 62 | +If no class is registered for a user-defined type, query results |
| 63 | +will use a ``namedtuple`` class and data may only be inserted |
| 64 | +though prepared statements. |
| 65 | + |
| 66 | +Customizing Encoders for Non-prepared Statements |
| 67 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 68 | +Starting with version 2.1 of the driver, it is possible to customize |
| 69 | +how python types are converted to CQL literals when working with |
| 70 | +non-prepared statements. This is done on a per-:class:`~.Session` |
| 71 | +basis through :attr:`.Session.encoder`: |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + cluster = Cluster() |
| 76 | + session = cluster.connect() |
| 77 | + session.encoder.mapping[tuple] = session.encoder.cql_encode_tuple |
| 78 | +
|
| 79 | +See :ref:`type-conversions` for the table of default CQL literal conversions. |
| 80 | + |
| 81 | +Using Client-Side Protocol-Level Timestamps |
| 82 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 83 | +With version 3 of the native protocol, timestamps may be supplied by the |
| 84 | +client at the protocol level. (Normally, if they are not specified within |
| 85 | +the CQL query itself, a timestamp is generated server-side.) |
| 86 | + |
| 87 | +When :attr:`~.Cluster.protocol_version` is set to 3 or higher, the driver |
| 88 | +will automatically use client-side timestamps with microsecond precision |
| 89 | +unless :attr:`.Session.use_client_timestamp` is changed to :const:`False`. |
| 90 | +If a timestamp is specified within the CQL query, it will override the |
| 91 | +timestamp generated by the driver. |
| 92 | + |
4 | 93 | Upgrading to 2.0 from 1.x |
5 | 94 | ------------------------- |
6 | 95 | Version 2.0 of the DataStax python driver for Apache Cassandra |
|
0 commit comments