def alter_keyspace(self, keyspace, replication_factor=None, replication_strategy=None, strategy_options=None): """ Alters an existing keyspace. .. warning:: Don't use this unless you know what you are doing. Parameters are the same as for :meth:`create_keyspace()`. """ old_ksdef = self._conn.describe_keyspace(keyspace) ksdef = KsDef(name=old_ksdef.name, strategy_class=old_ksdef.strategy_class, strategy_options=old_ksdef.strategy_options, replication_factor=old_ksdef.replication_factor, cf_defs=[]) if replication_strategy is not None: if replication_strategy.find('.') == -1: ksdef.strategy_class = 'org.apache.cassandra.locator.%s' % replication_strategy else: ksdef.strategy_class = replication_strategy if strategy_options is not None: ksdef.strategy_options = strategy_options if replication_factor is not None: ksdef.replication_factor = replication_factor if self._conn.version == CASSANDRA_08: ksdef = ksdef.to08() else: ksdef = ksdef.to07() self._system_update_keyspace(ksdef)
def create_keyspace(self, name, replication_factor=None, replication_strategy=SIMPLE_STRATEGY, strategy_options=None): """ Creates a new keyspace. Column families may be added to this keyspace after it is created using :meth:`create_column_family()`. `replication_factor` determines how many nodes hold a copy of a given row. Note that for Cassandra 0.8, `replication_factor` should be an entry in the strategy_options :class:`dict`. For example: .. code-block:: python >>> sys.create_keyspace('KS', None, SIMPLE_STRATEGY, {'replication_factor': '1'}) However, pycassa will copy the value from `replication_factor` or `strategy_options` to the other as needed, depending on the version of Cassandra. `replication_strategy` determines how replicas are chosen for this keyspace. The strategies that Cassandra provides by default are available as :const:`SIMPLE_STRATEGY`, :const:`NETWORK_TOPOLOGY_STRATEGY`, and :const:`OLD_NETWORK_TOPOLOGY_STRATEGY`. `NETWORK_TOPOLOGY_STRATEGY` requires `strategy_options` to be present. `strategy_options` is an optional dictionary of strategy options. By default, these are only used by NetworkTopologyStrategy; in this case, the dictionary should look like: ``{'Datacenter1': '2', 'Datacenter2': '1'}``. This maps each datacenter (as defined in a Cassandra property file) to a replica count. Example Usage: .. code-block:: python >>> from pycassa.system_manager import * >>> sys = SystemManager('192.168.10.2:9160') >>> # Create a SimpleStrategy keyspace >>> sys.create_keyspace('SimpleKS', 1) >>> # Create a NetworkTopologyStrategy keyspace >>> sys.create_keyspace('NTS_KS', 3, NETWORK_TOPOLOGY_STRATEGY, {'DC1': '2', 'DC2': '1'}) >>> sys.close() """ if replication_strategy.find('.') == -1: strategy_class = 'org.apache.cassandra.locator.%s' % replication_strategy else: strategy_class = replication_strategy ksdef = KsDef(name, strategy_class, strategy_options, replication_factor, []) if self._conn.version == CASSANDRA_08: ksdef = ksdef.to08() else: ksdef = ksdef.to07() self._system_add_keyspace(ksdef)