| 89 | |
| 90 | |
| 91 | def setup(options): |
| 92 | log.info("Using 'cassandra' package from %s", cassandra.__path__) |
| 93 | |
| 94 | cluster = Cluster(options.hosts, schema_metadata_enabled=False, token_metadata_enabled=False) |
| 95 | try: |
| 96 | session = cluster.connect() |
| 97 | |
| 98 | log.debug("Creating keyspace...") |
| 99 | try: |
| 100 | session.execute(""" |
| 101 | CREATE KEYSPACE %s |
| 102 | WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' } |
| 103 | """ % options.keyspace) |
| 104 | |
| 105 | log.debug("Setting keyspace...") |
| 106 | except cassandra.AlreadyExists: |
| 107 | log.debug("Keyspace already exists") |
| 108 | |
| 109 | session.set_keyspace(options.keyspace) |
| 110 | |
| 111 | log.debug("Creating table...") |
| 112 | create_table_query = """ |
| 113 | CREATE TABLE {0} ( |
| 114 | thekey text, |
| 115 | """ |
| 116 | for i in range(options.num_columns): |
| 117 | create_table_query += "col{0} {1},\n".format(i, options.column_type) |
| 118 | create_table_query += "PRIMARY KEY (thekey))" |
| 119 | |
| 120 | try: |
| 121 | session.execute(create_table_query.format(TABLE)) |
| 122 | except cassandra.AlreadyExists: |
| 123 | log.debug("Table already exists.") |
| 124 | |
| 125 | finally: |
| 126 | cluster.shutdown() |
| 127 | |
| 128 | |
| 129 | def teardown(options): |